A
Alan Foxmore
Hi everyone,
I hope you can comment on this -- I'm pretty darned sure that either
some of the MethodInfo properties are wrong or the Microsoft
documentation is wrong.
Please have a look at the program below. It attempts to print out the
accessibility (i.e., "visibility") of each method in the class. I've
included the program along with its output.
The Microsoft documentation for MethodInfo tells us that the IsAssembly
property "Gets a value indicating whether this method can be called by
other classes in the same assembly." Okay, great. Public methods can
certainly be called from other classes in the same assembly, right? Not
according to the program below. The output indicates a public method
cannot be called from other classes within its assembly because the
IsAssembly property is false.
There are other inconsistencies in the output as well - for example, an
internal method cannot be called from a subclass in the same assembly.
Read the Microsoft documentation very carefully. You'll see that the
IsPrivate and IsPublic properties refer to the accessibility of the
member itself. For example, m.IsPublic indicates whether or not m is
public.
In contrast, IsFamily, IsAssembly, IsFamilyAndAssembly and
IsFamilyOrAssembly indicate whether the method in question can be
called from a method with that accessibility. For example, m.IsAssembly
indicates whether m can "be called by other classes in the same
assembly". This is distinctly different from saying that m has internal
accessibility. So, certainly any public method can "be called by other
classes in the same assembly", right? Not according to the output
below.
It's very inconsistent and the property values are inconsistent with
what the documentation says they should be.
I guess you can look at this as either the MethodInfo properties are
wrong or the Microsoft documentation is wrong. I guess I'm just a
stickler.
---------------------
using System;
using System.Reflection;
class Program {
private static void Main(string[] args) {
Type t = typeof(Program);
MemberInfo[] members = t.GetMembers(BindingFlags.Public |
BindingFlags.Instance |
BindingFlags.NonPublic |
BindingFlags.DeclaredOnly);
foreach (MemberInfo mi in members) {
Console.WriteLine(mi);
MethodInfo methodInfo = mi as MethodInfo;
if (methodInfo != null) {
Console.WriteLine(" Private: " + methodInfo.IsPrivate);
Console.WriteLine(" Public: " + methodInfo.IsPublic);
Console.WriteLine(" Assm: " + methodInfo.IsAssembly);
Console.WriteLine(" Fam: " + methodInfo.IsFamily);
Console.WriteLine(" Fam & Assm: " +
methodInfo.IsFamilyAndAssembly);
Console.WriteLine(" Fam | Assm: " +
methodInfo.IsFamilyOrAssembly);
}
Console.WriteLine();
}
} // main()
protected void ProtMethod() {}
internal void InternalMethod() {}
public void PublicMethod() {}
protected internal void ProtInternalMethod() {}
} // Program()
------ OUTPUT ----------
Void ProtMethod()
Private: False
Public: False
Assm: False
Fam: True
Fam & Assm: False
Fam | Assm: False
Void InternalMethod()
Private: False
Public: False
Assm: True
Fam: False
Fam & Assm: False
Fam | Assm: False
Void PublicMethod()
Private: False
Public: True
Assm: False
Fam: False
Fam & Assm: False
Fam | Assm: False
Void ProtInternalMethod()
Private: False
Public: False
Assm: False
Fam: False
Fam & Assm: False
Fam | Assm: True
Void .ctor()
I hope you can comment on this -- I'm pretty darned sure that either
some of the MethodInfo properties are wrong or the Microsoft
documentation is wrong.
Please have a look at the program below. It attempts to print out the
accessibility (i.e., "visibility") of each method in the class. I've
included the program along with its output.
The Microsoft documentation for MethodInfo tells us that the IsAssembly
property "Gets a value indicating whether this method can be called by
other classes in the same assembly." Okay, great. Public methods can
certainly be called from other classes in the same assembly, right? Not
according to the program below. The output indicates a public method
cannot be called from other classes within its assembly because the
IsAssembly property is false.
There are other inconsistencies in the output as well - for example, an
internal method cannot be called from a subclass in the same assembly.
Read the Microsoft documentation very carefully. You'll see that the
IsPrivate and IsPublic properties refer to the accessibility of the
member itself. For example, m.IsPublic indicates whether or not m is
public.
In contrast, IsFamily, IsAssembly, IsFamilyAndAssembly and
IsFamilyOrAssembly indicate whether the method in question can be
called from a method with that accessibility. For example, m.IsAssembly
indicates whether m can "be called by other classes in the same
assembly". This is distinctly different from saying that m has internal
accessibility. So, certainly any public method can "be called by other
classes in the same assembly", right? Not according to the output
below.
It's very inconsistent and the property values are inconsistent with
what the documentation says they should be.
I guess you can look at this as either the MethodInfo properties are
wrong or the Microsoft documentation is wrong. I guess I'm just a
stickler.
---------------------
using System;
using System.Reflection;
class Program {
private static void Main(string[] args) {
Type t = typeof(Program);
MemberInfo[] members = t.GetMembers(BindingFlags.Public |
BindingFlags.Instance |
BindingFlags.NonPublic |
BindingFlags.DeclaredOnly);
foreach (MemberInfo mi in members) {
Console.WriteLine(mi);
MethodInfo methodInfo = mi as MethodInfo;
if (methodInfo != null) {
Console.WriteLine(" Private: " + methodInfo.IsPrivate);
Console.WriteLine(" Public: " + methodInfo.IsPublic);
Console.WriteLine(" Assm: " + methodInfo.IsAssembly);
Console.WriteLine(" Fam: " + methodInfo.IsFamily);
Console.WriteLine(" Fam & Assm: " +
methodInfo.IsFamilyAndAssembly);
Console.WriteLine(" Fam | Assm: " +
methodInfo.IsFamilyOrAssembly);
}
Console.WriteLine();
}
} // main()
protected void ProtMethod() {}
internal void InternalMethod() {}
public void PublicMethod() {}
protected internal void ProtInternalMethod() {}
} // Program()
------ OUTPUT ----------
Void ProtMethod()
Private: False
Public: False
Assm: False
Fam: True
Fam & Assm: False
Fam | Assm: False
Void InternalMethod()
Private: False
Public: False
Assm: True
Fam: False
Fam & Assm: False
Fam | Assm: False
Void PublicMethod()
Private: False
Public: True
Assm: False
Fam: False
Fam & Assm: False
Fam | Assm: False
Void ProtInternalMethod()
Private: False
Public: False
Assm: False
Fam: False
Fam & Assm: False
Fam | Assm: True
Void .ctor()