I have several models that includes nested classes and lists. Many of the class members have attributes that I also have to read in.
I’m looking for a dynamic way (possibly through Linq or Reflection) to retrieve the values and attributes of all generic objects within the specified model.
Any suggestions are welcome.
Edit:
Using the ObjectManager as per x0r’s suggestion, I can see all of the data. The remaining part of this issue requires member annotation. Is there some way to copy over the PropertyInfo of each class member?
ObjectWalker objectWalker = new ObjectWalker(objectToValidate);
foreach (Object o in objectWalker)
{
if (isGeneric(o.GetType()))
{
PropertyInfo property = o.GetType().GetProperty(o); // <-- This does not work... Need to obtain annotations somehow
object[] Attributes = property.GetCustomAttributes(typeof(Attribute), true);
foreach (Attribute attribute in Attributes)
{
// Annotations processing goes here
}
}
}
Have a look at the ObjectWalker
It helps you to parse an object graph and visit all unique elements.
As you can see it stores only a stack of objects and Current returns an Object.
You could modify this so the Walker would save a class that contains all the data you need for each property (Like a propertyinfo object or a list of attributes).