469,898 Members | 1,779 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,898 developers. It's quick & easy.

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.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++;
}
}
Nov 17 '05 #1
0 998

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Carlos J. Quintero | last post: by
1 post views Thread by Robert | last post: by
4 posts views Thread by Dan | last post: by
9 posts views Thread by Reuben | last post: by
1 post views Thread by Hans Ruck | last post: by
5 posts views Thread by Shak | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.