472,096 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,096 software developers and data experts.

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 1042

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
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.