Some doubt about reflection

J

jibesh.vp

Hi all,

I have a doubt in using C# Reflection mechanism .

What i need is to get method info alone from a class type.

Following code gets all the Properties inside the class too and there is no
way to
differentiate a method from property.

MemberType propertie of the MethodInfo class always have the value " Method
" for all the methods and properites

Any Help


sample:

public class MyTypeClass

{

public void MyMethods() { }

private int data;

public int Data

{

get{ return this.data; }

set { this.data = value; }

}

}

public class TypeMain

{

public static void Main()

{

Type myType =(typeof(MyTypeClass));

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

- Jibesh.V.P
 
C

Clive Dixon

Check the MemberType property of the MemberInfo base class of the MethodInfo
class?
 
L

Lee Alexander

Following code gets all the Properties inside the class too and there is
no way to
differentiate a method from property.

That's because properties are implemented using methods. So in your example
for property 'Data' you will see methods:

Int32 get_Data()
Void set_Data(Int32)

You have two choices as I see it if you want to differentiate between
methods that aren't being used for properties. The first is to look for get_
and set_ patterns in the methods and hide them or alternatively iterate over
the properties first for a class getting their associated get and set method
infos and then remove them from your methods list.

I prefer the last approach as this guarantees what you want.

HTH

Regards
Lee

jibesh.vp said:
Hi all,

I have a doubt in using C# Reflection mechanism .

What i need is to get method info alone from a class type.

Following code gets all the Properties inside the class too and there is
no way to
differentiate a method from property.

MemberType propertie of the MethodInfo class always have the value "
Method " for all the methods and properites

Any Help


sample:

public class MyTypeClass

{

public void MyMethods() { }

private int data;

public int Data

{

get{ return this.data; }

set { this.data = value; }

}

}

public class TypeMain

{

public static void Main()

{

Type myType =(typeof(MyTypeClass));

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

- Jibesh.V.P
 
J

jibesh

Yes.. Thank you Lee thats what i did last.

But its preferable that if we can identify propertie using MemberType.

Why did they miss this checking ? then whats the need of a MemberType enum
object
in the MethodInfo class where Type.GetMethods(....) always returns methods
only
no property or constructor etc ( i mean its not possible to differentiate
them)..

Thanks
Jibesh.V.P

Lee Alexander said:
Following code gets all the Properties inside the class too and there is
no way to
differentiate a method from property.

That's because properties are implemented using methods. So in your example
for property 'Data' you will see methods:

Int32 get_Data()
Void set_Data(Int32)

You have two choices as I see it if you want to differentiate between
methods that aren't being used for properties. The first is to look for get_
and set_ patterns in the methods and hide them or alternatively iterate over
the properties first for a class getting their associated get and set method
infos and then remove them from your methods list.

I prefer the last approach as this guarantees what you want.

HTH

Regards
Lee

jibesh.vp said:
Hi all,

I have a doubt in using C# Reflection mechanism .

What i need is to get method info alone from a class type.

Following code gets all the Properties inside the class too and there is
no way to
differentiate a method from property.

MemberType propertie of the MethodInfo class always have the value "
Method " for all the methods and properites

Any Help


sample:

public class MyTypeClass

{

public void MyMethods() { }

private int data;

public int Data

{

get{ return this.data; }

set { this.data = value; }

}

}

public class TypeMain

{

public static void Main()

{

Type myType =(typeof(MyTypeClass));

MethodInfo[] myArrayMethodInfo =
myType.GetMethods(BindingFlags.Public|BindingFlags.Instance|BindingFlags.Dec
laredOnly);
- Jibesh.V.P
 

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