Generically creating objects in C#

Posted by DrLazer on Stack Overflow See other posts from Stack Overflow or by DrLazer
Published on 2010-04-19T14:40:03Z Indexed on 2010/04/19 14:43 UTC
Read the original article Hit count: 182

Filed under:

What I am trying to do is load in objects from an XML save file. The problem is those objects are configurable by the user at runtime, meaning i had to use reflection to get the names and attributes of those objects stored in an XML file.

I am in the middle of a recursive loop through the XML and up to the part where I need to create an object then thought ..... ah - no idea how to do that :(

I have an array stuffed with empty objects (m_MenuDataTypes), one of each possible type. My recursive loading function looks like this

private void LoadMenuData(XmlNode menuDataNode)
{
   foreach (object menuDataObject in m_MenuDataTypes)
   {
       Type menuDataObjectType = menuDataObject.GetType();
       if (menuDataObjectType.Name == menuDataNode.Name)
       {
          //create object
       }
   }
}

I need to put some code where my comment is but I can't have a big switch statement or anything. The objects in my array can change depending on how the user has configured the app.

© Stack Overflow or respective owner

Related posts about c#