Is it possible to conditionally hide properties at compile time in .Net?

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

I was thinking of creating a custom attribute derived from EditorBrowsableAttribute, but unfortunately this class is sealed.

I looked at ICustomTypeDescriptor, but in the GetProperties method I can get each property descriptor, but the collection of attributes is read-only.

Any ideas?

+3
source share
2 answers

Recently, I again ran into this problem, and this time the answer came to me very quickly; just set 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 the element as follows:

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

#if

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

Source: https://habr.com/ru/post/1769568/


All Articles