如果它具有特定的自定义属性,有什么方法可以强制类实现接口?
如果具有特定属性的类未实现特定接口,我希望出现编译时错误。
[myAttrib]
public MyClass:IMyInterface
{
}
如果 myClass 不是 typeof(IMyInterface) ,我会在编译时出错。
谢谢,
如果它具有特定的自定义属性,有什么方法可以强制类实现接口?
如果具有特定属性的类未实现特定接口,我希望出现编译时错误。
[myAttrib]
public MyClass:IMyInterface
{
}
如果 myClass 不是 typeof(IMyInterface) ,我会在编译时出错。
谢谢,
在属性的情况下,您可以创建一个继承接口的抽象类并从该抽象类获取最终类驱动器。
看一下
public interface Test
{
string Name { get; set; }
}
public abstract class Test1 : Test
{
public abstract string Name { get; set; }
}
public class Test2 : Test1
{
}
对于自定义属性,你可以做
public class Alias : System.Attribute
{
string[] _types;
public Alias(params string[] types)
{
this.Types = types;
}
public Alias()
{
this.Types = null;
}
public string[] Types
{
get { return _types; }
set { _types = value; }
}
}
public interface Test
{
Alias Attrib{ get;}
}
public abstract class Test1 : Test
{
public abstract Alias Attrib { get; }
}
public class Test2 : Test1
{
}
希望我回答你的问题。