Reflecting Inherited Internal Methods - Bug??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paul Lyons

    #1

    Reflecting Inherited Internal Methods - Bug??

    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.Reflecti on;

    class TestReflection
    {
    [STAThread]
    static void Main(string[] args)
    {
    Type t = typeof (B);

    BindingFlags flags = BindingFlags.In stance | BindingFlags.No nPublic ;

    B b = new B();

    MethodInfo hidden = t.GetMethod("cM ethod", flags);
    if (hidden != null)
    Console.WriteLi ne("Found cMethod!"); // Never gets here :(

    b.cMethod(); // ...but the method *IS* inherited because this line
    succeeds!

    MethodInfo notHidden = t.GetMethod("dM ethod", flags);
    if (notHidden != null)
    Console.WriteLi ne("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++;
    }
    }
Working...