BrowsableAttribute Runtime Value

I wanted to set a value BrowsableAttributefor some properties of the instance MyClassat runtime:

 public class MyClass
{
     [Browsable(true)]
     public int P1 { get; set } 
     ...
}

Please advise how this can be done, as well as how to add BrowsableAttributeto the instance property MyClassat run time if this attribute does not exist.

+3
source share
3 answers

You can implement a custom type descriptor - which intercepts an attribute on the path to using it. Thus, an effect that looks like an attribute changes.

See Part 1 , Part2

+2
source

- . , ( BrowsableAttribute).

.

+5

You seem like you can. At the moment, I'm trying to do something like this, and I managed to work with CategoryAttribute. Currently, however, this stops the propertiesgrid property from working together, although although this is called in the button constructor:

Dim PC As PropertyDescriptorCollection = TypeDescriptor.GetProperties(Me)
For i As Integer = 0 To PC.Count - 1
        Dim att As BrowsableAttribute = DirectCast(PC(i).Attributes(GetType(BrowsableAttribute)), BrowsableAttribute)
        If Not att Is Nothing Then
            If att.Browsable = True Then
                Dim cat As Reflection.FieldInfo = att.GetType.GetField("Browsable", Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance Or Reflection.BindingFlags.Public Or Reflection.BindingFlags.Static Or Reflection.BindingFlags.IgnoreCase)
                If Not cat Is Nothing Then
                    cat.SetValue(att, False)
                End If
            End If
        End If
Next

Hope this helps

+1
source

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


All Articles