VB.NetアプリケーションのPropertyGridコントロールで使用するためのクラスが作成されています。クラスの特性の1つは、次のとおりです。実行時にプロパティのTypeConverter属性を変更するにはどうすればよいですか?
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
は、私は、実行時で、このプロパティのにTypeConverter属性を変更したいです。私は現在、他の属性を変更しています。このように(例えば、読み取り専用、ブラウズ可能。):
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)
は、しかし、私はにTypeConverter属性を変更するには、同様の手法を適用することはできませんよ。私はこれをしようとするとtypeConv変数は「何を」ではありません:
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:この場合にTypeConverterは、ユーザーが適切な値を選択し、代わりの中に入力することができるように、PropertyGridの中にドロップダウンを表示するために使用されます1。私は実行時にこのリストを変更したい。
ETA2:寄稿者(何らかの理由で回答を削除することを決めた)の寛大な助けを借りて、私の現在のポジションはtypeConv変数にフィールドを取得することですが、変更することはできません。 GetType(propeList2)/ GetType(propeList2).AssemblyQualifiedName/a "という値に関係なく、typeConv.SetValueステートメントの" System.RuntimeType '型のオブジェクトを' System.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)
[Convert.ChangeType](http://msdn.microsoft.com/en-us/library/dtb69x08.aspx)メソッドを探している可能性がありますが、おそらく私はあなたの要件を誤解しています。 –
ありがとう@TimSchmelter。私はそれが後者だと思う。この場合のTypeConverterは、PropertyGridにドロップダウンを表示するために使用されるため、ユーザは適切な値を入力することができます。私は実行時にこのリストを変更したい。 – Apeksha