我正在使用 VS2008,并希望根据属性的自定义属性创建编译时警告/错误(如果可能的话)。
目前我感兴趣的有两个案例:
[MyAttribute (typeof(MyClass)]
MyClass
必须实现一个接口(interface)。目前我在属性的构造函数中声明了这一点,但是由于堆栈跟踪的性质,这并不容易追踪:
public MyAttribute (Type MyClassType)
{
System.Diagnostics.Debug.Assert(
typeof(MyInterface).IsAssignableFrom(MyClassType),
"Editor must implement interface: " + typeof(MyInterface).Name);
}
我感兴趣的第二种情况是我在属性中定义了一个类型,如果该类型实现了一个接口(interface),那么如果另一个属性不存在则应该显示警告。
即if (MyClass.Implements(SomeInterface) && !Exists(SomeAttibute)) { 生成警告 }
[MyAttribute(typeof(MyClass)]
// Comment next line to generate warning
[Foo ("Bar")]
谢谢!
最佳答案
关于c# - 生成自定义编译时警告 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1420143/