문제

Depending on a preprocessor directive, I want to set all properties in a class to EditorBrowsableAttribute.Never.

I thought about creating a custom attribute, derived from EditorBrowsableAttribute, but unfortunately that class is sealed.

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.

Any ideas?

도움이 되었습니까?

해결책 2

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

Then in your code, decorate a member like so:

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

다른 팁

One approach is to explicitly use the #if syntax

#if SOMECONDITION
[EditorBrowsable(EditorBrowsableState.Never)]
#endif
public int SomeProperty { get; set; }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top