'How to change the TypeConverter attribute of a property at runtime?

I have a class created for use with the PropertyGrid control in a VB.Net application. One of the properties of the class is:

    Private _someProp As String

    <Browsable(True), _
     BindableAttribute(False), _
     DesignOnly(False), _
     DescriptionAttribute("Some Property Description"), _
     TypeConverter(GetType(propList1)), _
     DisplayName("Some Property")> _
    Public Property someProp() As String
        Get
            Return _someProp
        End Get
        Set(ByVal Value As String)
            _someProp = Value                
        End Set
    End Property

I want to change the TypeConverter attribute of this property at run-time. I am currently changing other attributes(e.g. Read-only, Browsable) like this:

Dim descriptor As PropertyDescriptor = TypeDescriptor.GetProperties(Me.GetType)("someProp")
Dim attrib_r As ReadOnlyAttribute = descriptor.Attributes(GetType(ReadOnlyAttribute))
Dim isReadOnly As System.Reflection.FieldInfo = attrib_r.GetType.GetField("isReadOnly", Reflection.BindingFlags.NonPublic + Reflection.BindingFlags.Instance)
isReadOnly.SetValue(attrib_r, True)

However, I am not able to apply a similar technique to change the TypeConverter attribute. The typeConv variable is 'Nothing' when I try this:

Dim descriptor As PropertyDescriptor = TypeDescriptor.GetProperties(Me.GetType)("someProp")
Dim attrib As TypeConverterAttribute = descriptor.Attributes(GetType(TypeConverterAttribute))
Dim typeConv As System.Reflection.FieldInfo = attrib.GetType.GetField("typeConverter", Reflection.BindingFlags.NonPublic + Reflection.BindingFlags.Instance)
typeConv.SetValue(attrib, IIf(someCondition, GetType(propList1), GetType(propList2)))

ETA: The TypeConverter in this case is used to display a dropdown in the PropertyGrid, so that users can choose an appropriate value, instead of typing in one. I want to change this list at runtime.

ETA2: With the generous help of a contributor (who decided to delete the answer for some reason), my current position is that I do get a field in the typeConv variable, but I cannot change it. I get an error "Object of type 'System.RuntimeType' cannot be converted to type 'System.String'" on the typeConv.SetValue statement irrespective of the value I provide - GetType(propeList2) / GetType(propeList2).AssemblyQualifiedName / a random string

Dim typeConv As System.Reflection.FieldInfo = attrib.GetType.GetField("typeName", BindingFlags.Instance Or _
                                                       BindingFlags.Public Or _
                                                       BindingFlags.NonPublic Or _
                                                       BindingFlags.FlattenHierarchy)
typeConv.SetValue(attrib, value)


Solution 1:[1]

The only way that you could change the ConverterTypeName at runtime is to try to use reflection to access the private string member typeName and set this to the AssemblyQualifiedName of the Type.

This is what the constructor of the TypeConverterAttribute does internally.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 competent_tech