This may be a scenario in which generics help. It is possible to create a general class, but, unfortunately, the designer hates generics; do not do this, but:
class SampleControl<T> where T : IEntity { ... }
SampleControl<Entity> works, but SampleControl<NonEntity> does not.
Similarly, if this was not necessary during development, you could have something like:
public Type EntityType {get;private set;} public void SetEntityType<T>() where T : IEntity { EntityType = typeof(T); }
But this does not help the designer. You may just have to use validation:
private Type entityType; public Type EntityType { get {return entityType;} set { if(!typeof(IEntity).IsAssignableFrom(value)) { throw new ArgumentException("EntityType must implement IEntity"); } entityType = value; } }
source share