all groups > vj# > may 2004 >
You're in the

vj#

group:

GetMethods function returns Protected even if BindingFlags.Public is given



GetMethods function returns Protected even if BindingFlags.Public is given Reshma
5/11/2004 10:51:03 PM
vj#: GetMethods function returns Protected members in the following statemen

MethodInfo myArrayMethodInfo[] = myType.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)

The whole code is

import System .*
import System.Reflection .*
import System.Reflection.Emit .*

// Create a class having two public methods and one protected method
public class MyTypeClas

public void MyMethods(

}

public int MyMethods1(

return 3
}

protected String MyMethods2(

return "hello"
}
}

public class TypeMai

public static void main(String[] args

Type myType = MyTypeClass.class.ToType()
// Get the public methods
MethodInfo myArrayMethodInfo[] = myType.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
Console.WriteLine("\nThe number of public methods is {0}.", (Int32)(myArrayMethodInfo.length))
// Display all the methods
DisplayMethodInfo(myArrayMethodInfo)
// Get the nonpublic methods
MethodInfo myArrayMethodInfo1[] = myType.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly)
Console.WriteLine("\nThe number of protected methods is {0}.", (Int32)(myArrayMethodInfo1.length))
// Display information for all methods
DisplayMethodInfo(myArrayMethodInfo1)
}

public static void DisplayMethodInfo(MethodInfo myArrayMethodInfo[]

// Display information for all methods
for (int i = 0; i < myArrayMethodInfo.length; i++)
MethodInfo myMethodInfo = ( MethodInfo)myArrayMethodInfo.get_Item(i)
Console.WriteLine("\nThe name of the method is {0}.", myMethodInfo.get_Name())

}

RE: GetMethods function returns Protected even if BindingFlags.Public is given diganta.vjcr NO[at]SPAM online.microsoft.com
9/3/2004 11:44:08 AM
If you use Java reflection you should get the proper results.

In the present case where you have used .Net Reflection, you need to
compile your code using the '/ss' option of the J# compiler to get the
results that you expect.

For details, please visit the link
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vjsample
/html/vjsamscopingsampleusingsecurescoping.asp

Thank you,
Diganta Roy
Microsoft Visual J# .NET Product Team

--------------------
[quoted text, click to view]

MethodInfo myArrayMethodInfo[] = myType.GetMethods(BindingFlags.Public |
BindingFlags.Instance | BindingFlags.DeclaredOnly);

The whole code is

import System .* ;
import System.Reflection .* ;
import System.Reflection.Emit .* ;

// Create a class having two public methods and one protected method.
public class MyTypeClass
{
public void MyMethods()
{
}

public int MyMethods1()
{
return 3;
}

protected String MyMethods2()
{
return "hello";
}
}

public class TypeMain
{
public static void main(String[] args)
{
Type myType = MyTypeClass.class.ToType();
// Get the public methods.
MethodInfo myArrayMethodInfo[] = myType.GetMethods(BindingFlags.Public |
BindingFlags.Instance | BindingFlags.DeclaredOnly);
Console.WriteLine("\nThe number of public methods is {0}.",
(Int32)(myArrayMethodInfo.length));
// Display all the methods.
DisplayMethodInfo(myArrayMethodInfo);
// Get the nonpublic methods.
MethodInfo myArrayMethodInfo1[] =
myType.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance |
BindingFlags.DeclaredOnly);
Console.WriteLine("\nThe number of protected methods is {0}.",
(Int32)(myArrayMethodInfo1.length));
// Display information for all methods.
DisplayMethodInfo(myArrayMethodInfo1);
}

public static void DisplayMethodInfo(MethodInfo myArrayMethodInfo[])
{
// Display information for all methods.
for (int i = 0; i < myArrayMethodInfo.length; i++) {
MethodInfo myMethodInfo = ( MethodInfo)myArrayMethodInfo.get_Item(i);
Console.WriteLine("\nThe name of the method is {0}.",
myMethodInfo.get_Name());
}
}
}

[quoted text, click to view]
AddThis Social Bookmark Button