BindingFlags.Internal, where is that?

T

TEK

When using reflection, the Type.GetProperty method has the following
signature:
Type.GetProperty(String, BindingFlags)

The doc says:
a.. Specify BindingFlags.Public to include public properties in the search.
a.. Specify BindingFlags.NonPublic to include non-public properties (that
is, private and protected properties) in the search.

But how the heck do I get access to the Internal methods???
Specifying BindingFlags.Public | BindingFlags.NonPublic causes the internal
methods to not be found.

Does anyone have any hint about what to do to get the internal methods as
well?

Regards, TEK
 
I

Ian Griffiths [C# MVP]

TEK said:
But how the heck do I get access to the Internal methods???
Specifying BindingFlags.Public | BindingFlags.NonPublic causes the
internal methods to not be found.

NonPublic gets you all the non-public members (hence the name). This
includes internal members.

The documentation neglects to include internal members in its list of what
constitutes non-public, but in practice, NonPublic gets you all the
non-public members.
 
W

Willy Denoyette [MVP]

(BindingFlags.Static | BindingFlags.NonPublic);
Returns all static internal, private and protected.

(BindingFlags.Static | BindingFlags.Public);
Returns only static public.

(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
Returns all static methods.

Substitute BindingFlags.Static with BindingFlags.Instance to return all
non-static methods.

Willy.
 
T

TEK

Hello

Thanks for the reply.

Futher investigation shows that the problem only shows itself if the
internal member is declared in a base class.

Meaning that using:

And having a base class that declares a property as internal causes the call
below (not actual code) to return null instead of the property.

myType.GetProperty(myObject, "MyProperty", BindingFlags.NonPublic |
BindingFlags.Public | BindingFlags.Instance)

Changing the property to public or making the call directly on the base
class type makes it return the property.

(I have not tested changing the type to protected or private)

Is this a bug?

Regards, TEK

"Ian Griffiths [C# MVP]" <[email protected]> skrev i melding
NonPublic gets you all the non-public members (hence the name). This
includes internal members.

The documentation neglects to include internal members in its list of what
constitutes non-public, but in practice, NonPublic gets you all the
 
I

Ian Griffiths [C# MVP]

TEK said:
having a base class that declares a property as internal causes the call
below (not actual code) to return null instead of the property.

myType.GetProperty(myObject, "MyProperty", BindingFlags.NonPublic |
BindingFlags.Public | BindingFlags.Instance)

Is this a bug?

Sure looks that way.

Particularly since one the Beta of V2.0 of the .NET Framework it behaves as
you'd expect.

I'm using this:

public class Base
{
internal int BaseInternProp { get { return 42; } }
protected int BaseProtProp { get { return 42; } }
}
public class Foo : Base
{
internal int InternProp { get { return 42; } }
protected int ProtProp { get { return 42; } }
}

and this:

Type t = typeof(Foo);
foreach (MemberInfo fi in t.GetProperties(BindingFlags.Instance |
BindingFlags.NonPublic))
{
Console.WriteLine(fi.Name);
}


Output from VS.NET 2005 Beta 1:

InternProp
ProtProp
BaseInternProp
BaseProtProp

(i.e. what you'd expect.) Output from V1.1 of .NET (i.e. current version):

InternProp
ProtProp
BaseProtProp

Of the two, the first looks like right and the second looks wrong to me.

As far as I can tell this problem only applies to properties. If you use
fields, it all appears to work as you'd expect on both versions of .NET.
 
T

TEK

Hay again

Thanks a lot for the input.
Then I assume it's a bug and uses a workaround instead.
If I get time I'll see if I'll convert my methods to be using Field instead.

Quite annoying!

Best regards, TEK
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top