C# 是否可以在.Net中编译时有条件地隐藏属性?

C# 是否可以在.Net中编译时有条件地隐藏属性?,c#,.net,vb.net,typedescriptor,C#,.net,Vb.net,Typedescriptor,根据预处理器指令,我希望将类中的所有属性设置为EditorRowsableAttribute.Never 我曾考虑创建一个自定义属性,该属性派生自EditorBrowsableAttribute,但不幸的是,该类是密封的 我已经看过ICustomTypeDescriptor,但是在GetProperties方法中,我可以获得每个属性描述符,但是attributes集合是只读的 有什么想法吗?一种方法是显式使用\if语法 #if SOMECONDITION [EditorBrowsable(Edi

根据预处理器指令,我希望将类中的所有属性设置为EditorRowsableAttribute.Never

我曾考虑创建一个自定义属性,该属性派生自EditorBrowsableAttribute,但不幸的是,该类是密封的

我已经看过ICustomTypeDescriptor,但是在GetProperties方法中,我可以获得每个属性描述符,但是attributes集合是只读的


有什么想法吗?

一种方法是显式使用
\if
语法

#if SOMECONDITION
[EditorBrowsable(EditorBrowsableState.Never)]
#endif
public int SomeProperty { get; set; }

我最近又遇到了这个问题,这一次我很快找到了答案;只需设置几个常量:

Friend Class CompilerUtils

#If HideCode Then
    Public Const Browsable As EditorBrowsableState = EditorBrowsableState.Never 
    Public Const BrowsableAdvanced As EditorBrowsableState = EditorBrowsableState.Never 
#Else
    Public Const Browsable As EditorBrowsableState = EditorBrowsableState.Always
    Public Const BrowsableAdvanced As EditorBrowsableState = EditorBrowsableState.Advanced
#End If

End Class
然后在您的代码中,像这样装饰一个成员:

<EditorBrowsable(CompilerUtils.Browsable)> _
<EditorBrowsable(CompilerUtils.BrowsableAdvanced)> _
_
_

我知道我可以做到这一点,但我尽量避免为每个成员都这样做。它看起来也很混乱,这就是为什么能够创建一个从EditorBrowsableAttribute派生的类,其中包含#if语句的原因。然后我会用自定义属性装饰每个成员。这没有多大意义,只是每个.NET程序员都有反射器。这不是完全保护一个类,我知道这是不可能的。我有一个程序集的运行时版本,我至少可以部分隐藏成员。