Hi,
I've been having an issue using reflection and custom attributes to create
objects on the fly. I was wondering if this was expected behaviour or a
known bug?
When using Type.GetMembers() or Type.GetMethods() on a sub class it will not
return the methods marked 'internal' on the parent class. After much
experimentation with BindingFlags, I discovered that in order for the methods
to become visible they needed to be 'virtual' too. The demostration below
shows the issue.
Thanks..Paul
using System;
using System.Reflection;
class TestReflection
{
[STAThread]
static void Main(string[] args)
{
Type t = typeof (B);
BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic ;
B b = new B();
MethodInfo hidden = t.GetMethod("cMethod", flags);
if (hidden != null)
Console.WriteLine("Found cMethod!"); // Never gets here :(
b.cMethod(); // ...but the method *IS* inherited because this line
succeeds!
MethodInfo notHidden = t.GetMethod("dMethod", flags);
if (notHidden != null)
Console.WriteLine("Found dMethod!"); // Curiously though it's happy with
the 'virtual' one...
b.dMethod();
}
}
public abstract class A
{
public abstract void aMethod();
public void bMethod()
{
int i = 1;
i++;
}
internal void cMethod()
{
int i = 2;
i++;
}
internal virtual void dMethod()
{
int i = 3;
i++;
}
}
public class B : A
{
public override void aMethod()
{
int i = 0;
i++;
}
}