3

I have a customer class with a sub-class address

internal class Customer    
{
    public int id { get; set; }
    public string name { get; set; }

    [ObjectDefRelation(isSubClass = true)]
    public Addressinformation Addressinformation { get; set; }
}

internal class Addressinformation 
{
    public string street { get; set; }
}

I have a Method to fill this object with data from a xml. Now I want to call this method recursive when its arrive the sub-class Addressinformation. How can I call my generic method with informations from PropertyInfo?

public static T ConvertXmlToClass<T>(XmlDocument xmlDocumentObjectDef, XmlNode xmlNode, ObjectDefRelationAttribute parentClass = null) where T : new()
{
    ObjectDefRelationAttribute defRelationAttribute;
    T xmlToClass = new T();

    foreach (PropertyInfo field in xmlToClass.GetType().GetProperties())
    {
        foreach (Attribute attr in field.GetCustomAttributes(true))
        {
            defRelationAttribute = attr as ObjectDefRelationAttribute;
            if (null != defRelationAttribute)
            {
                if (defRelationAttribute.isSubClass)
                {
                    // 
                    // here I need help to call the recursive method (XXX)
                    //
                    var subClass = Helper.ConvertXmlToClass<XXX>(xmlDocumentObjectDef, xmlNode, defRelationAttribute);
                }
            }
        }
    }
}

I used the best answer with some modification:

Type typeArguments = GetType(field.PropertyType.Namespace + "." + field.PropertyType.Name);
object value = typeof(Helper).GetMethod("ConvertXmlToClass").MakeGenericMethod(typeArguments).Invoke(null, new object[] {xmlDocumentObjectDef, xmlNode, defRelationAttribute});
jww
  • 97,681
  • 90
  • 411
  • 885
masterchris_99
  • 2,683
  • 7
  • 34
  • 55
  • how can I call my generic method with informations from PropertyInfo? – masterchris_99 May 16 '11 at 09:24
  • possible duplicate of [How to use reflection to call generic Method?](http://stackoverflow.com/questions/232535/how-to-use-reflection-to-call-generic-method) – nawfal Jan 17 '14 at 08:27

2 Answers2

2

It seems that you've got a function that converts Type names to Types, something like this:

Type GetType(string typeName)
{
    return Type.GetType(typeName);
}

then you can call this method as:

object value = typeof(owningType).GetMethod("ConvertXmlToClass").MakeGenericMethod(GetType(typeName)).Invoke(xmlDocumentObjectDef, xmlNode, xmlToClass);

and Use PropertyInfo.SetValue() to set it on the property

Alireza
  • 5,421
  • 5
  • 34
  • 67
1

If you want to stick with your current approach then you need to use reflection to built the generic method call from the field.PropertyType as described here: Reflection and generic types

However you could also consider changing your method to accept a Type as parameter instead of making a generic method (hint you can use Activator.CreateInstance(type) to instantiate an object).

Community
  • 1
  • 1
ChrisWue
  • 18,612
  • 4
  • 58
  • 83
  • I don't want to set the value. how to do that is clear. I need to call this method recursive because there are lots of functions in it for getting the informations from xml – masterchris_99 May 16 '11 at 09:31
  • That's what I said: You need to built your method call with reflection at runtime, like this: `typeof(Helper).GetMethod("ConvertXmlToClass").MakeGenericMethod(field.PropertyType);` - it's described in the answer to the question I linked to. – ChrisWue May 16 '11 at 09:34