473,465 Members | 1,917 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Reflection question

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
4 3118
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: A. Wiebenga | last post by:
Hi all! I am a student at the Hogeschool van Arnhem en Nijmegen in Holland. I am currently involved in a research project regarding Reflection. Purpose of the research project is to document...
10
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a...
1
by: Mike Malter | last post by:
I am just starting to work with reflection and I want to create a log that saves relevant information if a method call fails so I can call that method again later using reflection. I am...
2
by: Dan | last post by:
Let's say I have a class like: class Dummy { public const string CONE = "one"; public const string CTWO = "two"; ... other stuff .... }
7
by: John | last post by:
I have a class the reads in a file and sets the values of the file into its properties. This class is used to populate the data onto a form. This form has controls created at runtime based on...
1
by: Pieter Breed | last post by:
Hi All, I am trying to use the attribute/reflection system to as much potential as I can think of, but I've run into a snag. I would appreciate it if someone would point me in the right...
5
by: heddy | last post by:
I understand that reflection allows me to discover the metadata of a class at runtime (properties, methods etc). What I don't understand is where this is useful. For example: If I am the sole...
5
by: =?Utf-8?B?Q2hyaXN0aWFuIEhhdmVs?= | last post by:
Hi, is it faster to access the fields from an object directly by using reflection than accessing them by their properties using reflection? Thanks Christian
17
by: raylopez99 | last post by:
What good is C# Reflection, other than to find out what types are in an assembly? And to dynamically invoke methods in an assembly (.dll or .exe)? Also, bonus question, can you use Reflection...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.