【问题标题】:Can't access SuppressMessage attribute at runtime在运行时无法访问 SuppressMessage 属性
【发布时间】:2013-11-25 17:14:40
【问题描述】:

我正在尝试创建一个单元测试,该单元测试只会对不抑制相应消息的类型失败。但是,我无法在单元测试中访问任何类型的 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
}

【问题讨论】:

    标签: c# reflection attributes suppressmessage


    【解决方案1】:

    使用条件符号 CODE_ANALYSIS 构建您的程序集(其中定义了 IMyInterface)

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-15
      • 1970-01-01
      • 1970-01-01
      • 2021-10-26
      • 2014-09-19
      相关资源
      最近更新 更多