473,378 Members | 1,138 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 3107
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: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.