相关疑难解决方法(0)

类型限制

我们可以将类的Type属性限制为特定类型吗?

例如:

public interface IEntity { }

public class Entity : IEntity {}

public class NonEntity{}

class SampleControl {
    public Type EntityType{get;set;}
}
Run Code Online (Sandbox Code Playgroud)

假设sampleControl是UI类(可能是Control,Form,..),其EntityType属性的值应该只接受typeof(Entity)的值,而不是typeof(NonEntity)我们如何限制用户赋予特定的在设计时键入(bcause - Sample是我们可以在设计时设置其属性的控件或表单),这在C#.net中是可行的

我们怎样才能使用C#3.0实现这一目标?

在我上面的类中,我需要Type属性,对此必须是IEntity之一.

c#

5
推荐指数
1
解决办法
936
查看次数

标签 统计

c# ×1