I have the following code:
public class EntitySchema
{
}
public class ContactEntitySchema : EntitySchema
{
}
public class ProductEntitySchema : EntitySchema
{
}
public class Entity<TEntitySchema>
where TEntitySchema : EntitySchema
{
public string Id { get; set; }
}
public class Contact : Entity<ContactEntitySchema>
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class Product : Entity<ProductEntitySchema>
{
public string Name { get; set; }
}
public class ViewBase<TEntity>
where TEntity : Entity<EntitySchema>
{
public TEntity Entity { get; set; }
}
public class ContactView : ViewBase<Contact>
{
public ContactView()
{
}
}
public class ProductView : ViewBase<Entity<EntitySchema>>
{
public ProductView()
{
}
}
I get the following error for ContactView class:
Error 1 The type
'Generics.Contact'
cannot be used as type parameter'TEntity'
in the generic type or method'Generics.ViewBase<TEntity>'
. There is no implicit reference conversion from'Generics.Contact'
to'Generics.Entity<Generics.EntitySchema>'
.
Then I define ProductView a bit different and it compiles successfully. However, in ProductView I should explicitly cast the Entity property to Product if I want to use some of the Product specific fields. Is there a way to rework the declaration of ContactView so I can specify the actual Entity type (in this particular case - Contact)?