jeudi 16 juillet 2015

How to use a superclass method using reflections C#

I have a bunch of classes that inherit from a single class. I'm using reflections to access the classes, since the ones that will be accessed will change in runtime.

But I am having some trouble when trying to invoke a method declared at superclass.

Here is my code:

Type childType = Type.GetType(className[i]);
ConstructorInfo childConstructor = childType.GetConstructor(new Type[0]);
object childObject = null;
childObject = childConstructor.Invoke(childObject, new object[0]);
MethodInfo parentStringNoArgumentsMethod = childType.GetMethod("Method0Arg");
MethodInfo parentVoidOneArgumentMethod = childType.GetMethod("Method1Arg");

parentVoidOneArgumentMethod.Invoke(childObject, new object[]{argString});
object finalString = parentStringNoArgumentsMethod.Invoke(childObject, new object[0]);

Basically, I just need to invoke a super method using the child as the dynamic object. How can I achieve this?





Aucun commentaire:

Enregistrer un commentaire