Skip to main content
 首页 » 编程设计

C# 在属性类上使用 [System.Diagnostics.Conditional] 属性实际上做了什么

2024年02月06日17lvdongjie

我已经知道在方法上使用条件属性有一段时间了,但我刚刚发现它也可以用在属性类上,所以我写了一些代码来测试它,但它并没有像预期的那样执行。

此 MSDN 页面显示了如何在页面底部的属性类上使用条件属性:https://msdn.microsoft.com/en-us/library/4xssyw96%28v=vs.90%29.aspx .

顺便说一下,我正在使用 Unity 引擎。我认为这无关紧要,但我猜可能是这样。

这是我写的测试代码:

using System.Reflection; 
using UnityEngine; 
 
[System.Diagnostics.Conditional("UNITY_EDITOR")] 
public class TestAttribute : System.Attribute 
{ 
    public string text; 
 
    public TestAttribute(string text) 
    { 
        this.text = text; 
    } 
} 
 
public class NewBehaviourScript : MonoBehaviour 
{ 
    [Test("This shouldn't exist on android")] 
    public void Awake() 
    { 
#if UNITY_EDITOR 
        Debug.Log("This only gets logged in the Unity Editor, not in an Android build"); 
#endif 
 
        Debug.Log("Begin Attribute Test"); 
        { 
            object[] attributes = typeof(NewBehaviourScript).GetMethod("Awake").GetCustomAttributes(true); 
            for (int i = 0; i < attributes.Length; i++) 
            { 
                Debug.Log(attributes[i]);// This logs TestAttribute both in the editor and on android. 
            } 
 
            TestAttribute att = attributes[0] as TestAttribute; 
            Debug.Log(att.text);// This logs "This shouldn't exist on android" both in the editor and on android. 
        } 
        Debug.Log("End Attribute Test"); 
        Debug.Log(""); 
        Debug.Log("Begin Method Test"); 
        { 
            Method();// This only gets called in the Unity Editor, as expected from the conditional attribute. 
 
            MethodInfo methodInfo = typeof(NewBehaviourScript).GetMethod("Method"); 
            Debug.Log(methodInfo);// this logs "void Method()" both in the editor and on android. 
        } 
        Debug.Log("End Method Test"); 
    } 
 
    [System.Diagnostics.Conditional("UNITY_EDITOR")] 
    public void Method() 
    { 
        Debug.Log("This shouldn't exist on android either"); 
    } 
} 

如果条件属性不阻止 GetCustomAttributes() 获取测试属性,它实际上做了什么?

请您参考如下方法:

让我简化您的示例:

[Conditional("DEBUG")] 
class MyAttribute : Attribute {} 
 
[MyAttribute] 
class MyClass {} 

当 Conditional 属性应用于 Attribute 派生类型时,如果Conditional 属性的条件不成立。因此在上面的示例中,MyClass 仅在调试版本中被标记为 MyAttribute;在 Release 版本中,DEBUG 符号未定义(通常,至少),因此编译器从 MyClass 的声明中删除了 [MyAttribute] .可以看到在使用反射的时候,尝试在debug和release build中运行如下代码:

private static void Main(string[] args) 
{ 
    var attribute = typeof(MyClass).GetCustomAttribute<MyAttribute>(); 
    Console.WriteLine(attribute == null ? "missing" : "exists"); 
} 

这将在调试版本中打印“存在”,在发布版本中打印“缺失”。但是,只有属性的应用被删除;属性类本身仍然存在于已编译的程序集中。这类似于 Conditional 对方法的作用:只有方法调用被删除,方法仍然存在并且可以通过反射调用,例如。

为什么这会有用? JetBrains 描述了一个用例在博客文章中(参见“JetBrains.Annotations NuGet 包”部分):他们有一个名为 JetBrains.Annotations 的 NuGet 包,其中包含各种属性,可帮助他们的工具 Resharper 分析 C# 代码。然而,将这个 NuGet 包添加到您的项目中,将需要您将该程序集与您的产品一起发送,即使您实际上只是将它用于编码;不是在运行时。所以他们所做的是:他们用 Conditional 属性注释程序集中的所有属性。这会导致编译器在编译期间去除属性;然后它注意到 JetBrains.Annotations 从未被引用,从已编译的程序集中删除了引用。因此,您不必将 JetBrains 的组件与您的产品一起运送。

但是,我完全不知道这在 Unity 中是否有效。众所周知,Unity 使用旧版本的 Mono 和 C# 编译器,因此可能存在阻止所有这些工作的错误。实际上,您的代码似乎表明这在 Unity 中确实无法正常工作。但是,如果您在独立的 .NET 应用程序中运行我的或您的代码,它就可以工作。顺便说一句,您还可以使用较新版本的 C# 编译器来编译 Unity 然后可以引用的程序集;这应该可以让您在 Unity 中解决这个问题。

2018-03-17 更新:最新版本的 Unity 现在具有更新的 Mono 运行时和更新的 C# 编译器(并将很快更新到 Microsoft 的 C# 编译器)。因此,在最新版本的 Unity 中,应修复有关条件编译属性的任何非标准行为。