I have a lot of problems with Reflection in C # at the moment. The application that I am writing allows the user to change the attributes of certain objects using a configuration file. I want to save an object model (user project) in XML. The function below is called in the middle of the foreach loop, iterating over the list of objects that contain all the other objects in the project inside them. The idea is that it recursively works to translate the object model into XML.
Don’t worry about calling “Unreal,” which slightly changes the name of objects if they contain certain words.
private void ReflectToXML(object anObject, XmlElement parentElement) { Type aType = anObject.GetType(); XmlElement anXmlElement = m_xml.CreateElement(Unreal(aType.Name)); parentElement.AppendChild(anXmlElement); PropertyInfo[] pinfos = aType.GetProperties(); //loop through this objects public attributes foreach (PropertyInfo aInfo in pinfos) { //if the attribute is a list Type propertyType = aInfo.PropertyType; if ((propertyType.IsGenericType)&&(propertyType.GetGenericTypeDefinition() == typeof(List<>))) { List<object> listObjects = (aInfo.GetValue(anObject,null) as List<object>); foreach (object aListObject in listObjects) { ReflectToXML(aListObject, anXmlElement); } } //attribute is not a list else anXmlElement.SetAttribute(aInfo.Name, ""); } }
If the attributes of the object are just strings, then they should write them as string attributes in XML. If the attributes of the objects are lists, then it must recursively call "ReflectToXML", passing itself as a parameter, thereby creating the nested structure that I need, which reflects the model of the object in memory well.
I have a problem with the line
List<object> listObjects = (aInfo.GetValue(anObject,null) as List<object>);
The cast does not work, and it just returns null. During debugging, I changed the line to
object temp = aInfo.GetValue(anObject,null);
hit a breakpoint on it to find out what GetValue returns. It returns a "General list of objects." Of course, should I be able to do this? It is annoying that temp becomes a general list of objects, but since I declared temp as a special object, I cannot skip it because it does not have an Enumerator.
How can I cycle through a list of objects when I only have the property property property of the class?
I know that at this moment I will keep a list of empty lines, but everything is in order. I would be glad to see that the structure is being maintained at the moment.
Thanks in advance