Reflection and Abstract classes

  • Thread starter Thread starter shiry
  • Start date Start date
S

shiry

Hi,

Can anyone tell me how can I get a protected method of an abstract
class using reflection?

I need to invoke a method like this and I was trying invokeMember on
the type of the derived class with an object of the derived class. This
doesn't work.
I cannot perform this on the abstract class since I cannot instantiate
it.

What am I doing wrong?

Thanks.
 
Hi,

// create an instance on which the method will be invoked
DerivedClass obj = new DerivedClass();

// get the Type that defines the method (AbstractClass)
Type type = typeof(AbstractClass);

// get the method
MethodInfo m = type.GetMethod("AMethod",
BindingFlags.NonPublic | BindingFlags.Instance);

// invoke the method on the instance of DerivedClass
m.Invoke(obj, null);
 
Thanks! It works great.

Dave said:
Hi,

// create an instance on which the method will be invoked
DerivedClass obj = new DerivedClass();

// get the Type that defines the method (AbstractClass)
Type type = typeof(AbstractClass);

// get the method
MethodInfo m = type.GetMethod("AMethod",
BindingFlags.NonPublic | BindingFlags.Instance);

// invoke the method on the instance of DerivedClass
m.Invoke(obj, null);
 
Back
Top