How to Cast dynamically?
-
I'm working on a form editor a.k.a like in Visual Studio and sharpdevelop. I save the forms out as xml mainly using to the
.ToString
methods and then try to convert them back on load. I thought this would be the way to do it:-Dim PInfo As PropertyInfo = ty.GetProperty(Nde.Name) If Not PInfo Is Nothing Then Dim t As Type = PInfo.GetType() PInfo.SetValue(ctrl, (CType(Nde.Value, PInfo.GetType())), Nothing) End If
but CType and DirectCast do not acceptPInfo.GetType()
. They demand a hand coded type e.g.CType(Value, Integer)
. Does anybody know of a way to do this? I look for the TagName via reflection :-Dim PInfo As PropertyInfo = ty.GetProperty(Nde.Name)
and it returns fine, so it's found the property. But how do I then convert the value to the correct Type so I can set the property value? Demo XML:<VISIBLE>True</VISIBLE> <Text>Caption :</Text> <TextAlign>1</TextAlign>