gpt4 book ai didi

c# - 无法在运行时访问 SuppressMessage 属性

转载 作者:行者123 更新时间:2023-11-30 17:50:53 25 4
gpt4 key购买 nike

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

最佳答案

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

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

关于c# - 无法在运行时访问 SuppressMessage 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20199119/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com