mardi 29 décembre 2015

How to invoke method which accepts parameter of type base class using reflection?

I have classes like below

public Class A
{    
    protected A()
    {
    }
}

Public class B : A
{
    public B()
    {
    }
}

I have method like below

public class C{
    private void Method1(A obj)
    {
    }
}

method call using reflection

MethodInfo method1= typeof(C).GetMethod("Method1", BindingFlags.NonPublic | BindingFlags.Instance);

var result = method1.Invoke(cObject, new object[] { objectB });

I am trying to call this private method using reflection but I am getting Argument exception

An exception of type 'System.ArgumentException' occurred in mscorlib.dll but was not handled in user code

Object of type 'B' cannot be converted to type 'A'.

Could any one advise me in this?





Aucun commentaire:

Enregistrer un commentaire