2013-11-25 81 views
3

我正在嘗試創建一個單元測試,該類測試只對不禁止相應消息的類型失敗。但是,我無法在單元測試中訪問我的任何類型的SuppressMessage屬性。運行時是否可以訪問SuppressMessage屬性?我已經包含了我的單元測試的簡化版本。運行時無法訪問SuppressMessage屬性

[System.Diagnostics.CodeAnalysis.SuppressMessage("Foo", "Bar")] 
public interface IMyInterface { } 

public void UnitTest() 
{ 
    var getCustomAttributes = typeof(IMyInterface).GetCustomAttributes(); //Returns an empty array 
    //Skip check if message should be suppressed 
} 

回答

1

建立您的組件(其中IMyInterface的定義)與條件符號CODE_ANALYSIS

[Conditional("CODE_ANALYSIS")] 
[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)] 
public sealed class SuppressMessageAttribute : Attribute {