By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,200 Members | 1,761 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,200 IT Pros & Developers. It's quick & easy.

Reflection question

P: n/a
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<MethodInfomethodList = new List<MethodInfo>();
List<PropertyInfopropertyList = 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[i].IsPublic && methods[i].IsStatic &&
!methods[i].Name.StartsWith("Can"))
{
methodList.Add(methods[i]);
}
}
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*");
}
}

Jun 28 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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|BindingFl ags.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" wrote:
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<MethodInfomethodList = new List<MethodInfo>();
List<PropertyInfopropertyList = 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[i].IsPublic && methods[i].IsStatic &&
!methods[i].Name.StartsWith("Can"))
{
methodList.Add(methods[i]);
}
}
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*");
}
}
Jun 28 '07 #2

P: n/a
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#" <Ma****@discussions.microsoft.comwrote in message
news:35**********************************@microsof t.com...
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|BindingFl ags.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" wrote:
>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<MethodInfomethodList = new List<MethodInfo>();
List<PropertyInfopropertyList = 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[i].IsPublic && methods[i].IsStatic &&
!methods[i].Name.StartsWith("Can"))
{
methodList.Add(methods[i]);
}
}
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*");
}
}
Jun 28 '07 #3

P: n/a
Hi,

"Le Minh" <mi****@fsoft.com.vnwrote in message
news:13**********************************@microsof t.com...
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.
Jun 28 '07 #4

P: n/a
So, what do you expect it to do?
Is it really a problem?

"Le Minh" wrote:
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#" <Ma****@discussions.microsoft.comwrote in message
news:35**********************************@microsof t.com...
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|BindingFl ags.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" wrote:
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<MethodInfomethodList = new List<MethodInfo>();
List<PropertyInfopropertyList = 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[i].IsPublic && methods[i].IsStatic &&
!methods[i].Name.StartsWith("Can"))
{
methodList.Add(methods[i]);
}
}
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*");
}
}
Jun 29 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.