2009-08-07 53 views

回答

2
+0

確實,我們不能使用AttrubuteUsageAttribute達到此目的。 – klashar 2009-08-10 10:19:33

0

你總是可以編寫使用反射來驗證這一點......誠然有些生成後事件,它可能不是最優雅的解決方案....

要這樣設置,你會進入項目屬性,然後是「生成事件」選項卡。然後你會進入基於反射的工具來執行這個驗證

+0

我有這種能力可以將自定義規則添加到代碼分析工具中以自動執行它。 – klashar 2009-08-10 10:21:02

0

這可能不是你要找的東西,但是可以用PostSharp來做出這樣的屬性,你可能會有東西像這樣:

[Serializable] 
public sealed class StaticAttribute : OnMethodBoundaryAspect 
{ 
    public override bool CompileTimeValidate(System.Reflection.MethodBase method) 
    { 
     return method.IsStatic; 
    } 

的OnMethodBoundaryAspect基本上wrapps您的方法的try/catch塊內部,並且CompileTimeValidate方法確定屬性是否是在運行時調用。