vendredi 27 février 2015

Instantiate all properties and sub properties in an object (C#.net)

I have an object (x) with about 100 properties. Most of these properties are reference types that will need to be instantiated before I can do anything with x. Also, many of the properties on x will have properties that will also need to be instantiated.


I've thought to use reflection and recursion, but I'm still a little bit stuck on how the implementation would work. My current implementation involves looping through the PropertyInfo Array, and using SetValue from Activator.CreateInstance. As I drill down through x, I'm getting a lot of exceptions:



  • No parameterless constructor defined for this object.

  • Cannot create an abstract class.

  • Property set method not found.


Should I just account for these cases, or is there a better way to be doing this? Ultimately, it's not assigning values to everything I need still. Thanks for your help.






Aucun commentaire:

Enregistrer un commentaire