【发布时间】:2010-11-28 01:18:57
【问题描述】:
我正在使用 VS2008,并希望根据属性的自定义属性创建编译时警告/错误(如果可能的话)。
目前我感兴趣的有两种情况:
[MyAttribute (typeof(MyClass)]
MyClass 必须实现一个接口。目前我在属性的构造函数中声明了这一点,但是由于堆栈跟踪的性质,这并不容易追踪:
public MyAttribute (Type MyClassType)
{
System.Diagnostics.Debug.Assert(typeof(MyInterface).IsAssignableFrom(MyClassType),
"Editor must implement interface: " + typeof(MyInterface).Name);
}
我感兴趣的第二种情况是我在属性中定义了一个类型,如果该类型实现了一个接口,那么如果另一个属性不存在,则应该显示警告。
I.E. if (MyClass.Implements(SomeInterface) && !Exists(SomeAttibute)) { 生成警告 }
[MyAttribute(typeof(MyClass)]
// Comment next line to generate warning
[Foo ("Bar")]
谢谢!
【问题讨论】:
-
有趣的问题,我很好奇这样的事情是否可能。 +1
标签: c# compiler-construction custom-attributes postsharp