是否有可能有条件地隐藏在编译时在.NET属性? [英] Is it possible to conditionally hide properties at compile time in .Net?
问题描述
根据一个preprocessor指令,我想在一个类来EditorBrowsableAttribute.Never设置的所有属性。
Depending on a preprocessor directive, I want to set all properties in a class to EditorBrowsableAttribute.Never.
我想到了创建一个自定义属性,从EditorBrowsableAttribute来源,但不幸的是这个类是密封的。
I thought about creating a custom attribute, derived from EditorBrowsableAttribute, but unfortunately that class is sealed.
我看了一下ICustomTypeDescriptor,但在getProperties方法,我可以掌握每个属性描述的,但属性集合是只读的。
I've had a look at ICustomTypeDescriptor, but in the GetProperties method, I can get hold of each property descriptor, but the attributes collection is readonly.
任何想法?
推荐答案
我发生过这个问题,最近又和这一次的答案来找我非常迅速;简单地设置了一些常量:
I happened across this issue again recently and this time the answer came to me very quickly; simply set up a couple of constants:
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
然后在你的code,装点一员,像这样:
Then in your code, decorate a member like so:
<EditorBrowsable(CompilerUtils.Browsable)> _
<EditorBrowsable(CompilerUtils.BrowsableAdvanced)> _
这篇关于是否有可能有条件地隐藏在编译时在.NET属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!