Reflection question

L

Le Minh

Hi all
I'm writing an application using reflection feature of .NET Framework.
My applicaiton need to accept a assembly name as input parameter and then
must populate all methods and property from all class in this.
I'm trying to use BindingFlags with GetMethods and GetProperties from
System.Type class but i can't apply correct BindingFlags.
I only need to get public property and public and static method.

Anyone can help me?
Many thanks.
------
Here is my sample code:
public static void GetAssembly(string sAssemblyName)
{
List<MethodInfo> methodList = new List<MethodInfo>();
List<PropertyInfo> propertyList = new List<PropertyInfo>();

Assembly asm = Assembly.LoadFrom(Environment.CurrentDirectory +
"\\" +sAssemblyName);

Type[] types = asm.GetTypes();
foreach (Type type in types)
{
MethodInfo[] methods =
type.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Public);
for (int i = 0; i < methods.Length; i++)
{
if (methods.IsPublic && methods.IsStatic &&
!methods.Name.StartsWith("Can"))
{
methodList.Add(methods);
}
}
PropertyInfo[] properties =
type.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public);
for (int i = 0; i < properties.Length; i++)
{

}
MemberInfo[] members = type.FindMembers(MemberTypes.Method,
BindingFlags.Default, Type.FilterName, "Get*");
}
}
 
G

Guest

Hello,

Are you using Environment.CurrentDirectory an demand, cause this is going to
change if you are using an OpenFileDialog for example.

For the Usage of BindingFlags you could just read the docu:
For the publicProperties:
myType.GetProperties(BindingFlags.Public|BindingFlags.Instance);
For the static methods
myType.GetProperties(BindingFlags.Static);


The problem was that you have to use one of the two BindingFlags ( Instance
or Static) to get a result back.

All the best,

Martin
 
L

Le Minh

Thanks for your reply!
But i have a problem while try to get public method. All get and set from
property is also returned.
How i can fix this problem?
Regards!

Martin# said:
Hello,

Are you using Environment.CurrentDirectory an demand, cause this is going
to
change if you are using an OpenFileDialog for example.

For the Usage of BindingFlags you could just read the docu:
For the publicProperties:
myType.GetProperties(BindingFlags.Public|BindingFlags.Instance);
For the static methods
myType.GetProperties(BindingFlags.Static);


The problem was that you have to use one of the two BindingFlags (
Instance
or Static) to get a result back.

All the best,

Martin

Le Minh said:
Hi all
I'm writing an application using reflection feature of .NET Framework.
My applicaiton need to accept a assembly name as input parameter and then
must populate all methods and property from all class in this.
I'm trying to use BindingFlags with GetMethods and GetProperties from
System.Type class but i can't apply correct BindingFlags.
I only need to get public property and public and static method.

Anyone can help me?
Many thanks.
------
Here is my sample code:
public static void GetAssembly(string sAssemblyName)
{
List<MethodInfo> methodList = new List<MethodInfo>();
List<PropertyInfo> propertyList = new List<PropertyInfo>();

Assembly asm = Assembly.LoadFrom(Environment.CurrentDirectory
+
"\\" +sAssemblyName);

Type[] types = asm.GetTypes();
foreach (Type type in types)
{
MethodInfo[] methods =
type.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Public);
for (int i = 0; i < methods.Length; i++)
{
if (methods.IsPublic && methods.IsStatic &&
!methods.Name.StartsWith("Can"))
{
methodList.Add(methods);
}
}
PropertyInfo[] properties =
type.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public);
for (int i = 0; i < properties.Length; i++)
{

}
MemberInfo[] members =
type.FindMembers(MemberTypes.Method,
BindingFlags.Default, Type.FilterName, "Get*");
}
}
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,



Le Minh said:
Hi all
I'm writing an application using reflection feature of .NET Framework.
My applicaiton need to accept a assembly name as input parameter and then
must populate all methods and property from all class in this.
I'm trying to use BindingFlags with GetMethods and GetProperties from
System.Type class but i can't apply correct BindingFlags.
I only need to get public property and public and static method.

Anyone can help me?
Many thanks.
------

What is the error you are getting?

At eye inspection the flags seems correct:
type.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Public);

Get the method declared in that type that are public.
 
G

Guest

So, what do you expect it to do?
Is it really a problem?

Le Minh said:
Thanks for your reply!
But i have a problem while try to get public method. All get and set from
property is also returned.
How i can fix this problem?
Regards!

Martin# said:
Hello,

Are you using Environment.CurrentDirectory an demand, cause this is going
to
change if you are using an OpenFileDialog for example.

For the Usage of BindingFlags you could just read the docu:
For the publicProperties:
myType.GetProperties(BindingFlags.Public|BindingFlags.Instance);
For the static methods
myType.GetProperties(BindingFlags.Static);


The problem was that you have to use one of the two BindingFlags (
Instance
or Static) to get a result back.

All the best,

Martin

Le Minh said:
Hi all
I'm writing an application using reflection feature of .NET Framework.
My applicaiton need to accept a assembly name as input parameter and then
must populate all methods and property from all class in this.
I'm trying to use BindingFlags with GetMethods and GetProperties from
System.Type class but i can't apply correct BindingFlags.
I only need to get public property and public and static method.

Anyone can help me?
Many thanks.
------
Here is my sample code:
public static void GetAssembly(string sAssemblyName)
{
List<MethodInfo> methodList = new List<MethodInfo>();
List<PropertyInfo> propertyList = new List<PropertyInfo>();

Assembly asm = Assembly.LoadFrom(Environment.CurrentDirectory
+
"\\" +sAssemblyName);

Type[] types = asm.GetTypes();
foreach (Type type in types)
{
MethodInfo[] methods =
type.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Public);
for (int i = 0; i < methods.Length; i++)
{
if (methods.IsPublic && methods.IsStatic &&
!methods.Name.StartsWith("Can"))
{
methodList.Add(methods);
}
}
PropertyInfo[] properties =
type.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public);
for (int i = 0; i < properties.Length; i++)
{

}
MemberInfo[] members =
type.FindMembers(MemberTypes.Method,
BindingFlags.Default, Type.FilterName, "Get*");
}
}

 

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