lundi 2 mai 2016

Reflection: invoke a method with generic parameter

I everyone,

I have some issues with invoking a method with reflection.

The method sign is

public T Create<T, TK>(TK parent, T newItem, bool updateStatistics = true, bool silent = false)
        where T : class
        where TK : class;
    public T Create<T, TK>(TK parent, string newName, Language language = null, bool updateStatistics = true, bool silent = false)
        where T : class
        where TK : class;

I want to use the second overload.

My code is

typeof(ObjectType).GetMethod("Create")
            .MakeGenericMethod(new Type[] { typeof(Item), typeof(TKparent) })
            .Invoke(_objectInstance, new object[] { parent, name, _language, true, false });

where Item is a class, TKparent is a type variable and parent is a TKparent instance.

I get a System.Reflection.AmbiguousMatchException.

I think the problem is related to generics

I tried also this:

typeof(ObjectType).GetMethod("Create", new Type[] { typeof(TKparent), typeof(string), typeof(Globalization.Language), typeof(bool), typeof(bool) })
            .MakeGenericMethod(new Type[] { typeof(Item), typeof(Tparent) })
            .Invoke(_objectInstance, new object[] { parent, name, _language, true, false });

but in this case I get a System.NullReferenceException (no method found)

Can anyone help with this, I'm getting mad!

thank you





Aucun commentaire:

Enregistrer un commentaire