Setting value of BrowsableAttribute at runtime

后端 未结 3 508
情深已故
情深已故 2021-01-20 01:46

I wanted to set a value of BrowsableAttribute for some of MyClass instance\'s properties at runtime:

 public class MyClass
{
     [         


        
3条回答
  •  长发绾君心
    2021-01-20 02:28

    You sort of can. I'm trying something like this at the moment, which I managed to get working with CategoryAttribute. Currently though, this stops the propertiesgrid from working all together though, even though this is being called in the constructor of a button:

    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

提交回复
热议问题