I am trying to create a general XML to object converter. In other words, this is my XML
<setting> <name>testing</name> <type>System.String</type> <defaultObj>TTTT</defaultObj> </setting>
the type field contains the type of the object into which it is loaded. This is only the structure of the object in which I was programmed. Regardless, I'm having a conversion problem
System.String
into a variable of the actual type. So, for example, to convert, I have the following code:
foreach (XNode node in document.Element(root).Nodes()) { T variable = new T(); //where T : new() foreach (FieldInfo field in fields) { field.SetValue(variable, Convert.ChangeType(((XElement)node).Element(field.Name).Value, field.FieldType)); } retainedList.Add(variable); }
which returns objects in general. The algorithm works fine until it gets into the "Type" field. I get:
Invalid cast from 'System.String' to 'System.Type'.
runtime error. From what I can tell, a problem arises that directly converts the type identifier (string) directly into type. I'm not sure how to get around this problem, at least when it comes to keeping things generic and clean. Any ideas? I'm sorry if the problem is a little vague, if you do not quite understand, I will try to clarify further. Any help is much appreciated!
source share