A question about reflection

T

Tony Johansson

Hello!

This is a question about Reflection.
I have a small program below where the class are called Mymemberinfo.
This class Mymemberinfo is used in GetMembers to get all the member for this
class using Reflection.
When I run this small program I get this result.

Members in class : Mymemberinfo

There are 9 members in Mymemberinfo
0. GetHashCode Member type - Method
1. Equals Member type - Method
2. ToString Member type - Method
3. THIS_IS_A_METHOD Member type - Method
4. set_myValue Member type - Method
5. Main Member type - Method
6. GetType Member type - Method
7. .ctor Member type - Constructor
8. myValue Member type - Property
Press any key to continue

What I don't understand of this is rownumber 4 which is set_myValue Member
type - Method
I know that rownumber 4 and rownumber 8 is connected because if I remove
property myValue from the program and run the program again then both
rownumber 4 and rownumber 8 is removed.

So can somebody explain why rownumber 4 is shown just because I have a
property that is called myValue.

So the result is saying that class Mymemberinfo is having 9 member that is
wrong the right answer i 8
becuse of rownumber 4 which should'n be there.

According to the list row number 4 should there be a method that is called
set_myValue but there exist
only a propery that is called myValue.


using System;
using System.Reflection;

public class Mymemberinfo
{
int MYVALUE;
public void THIS_IS_A_METHOD() {}
public int myValue { set {MYVALUE = value;} }

[STAThread]
public static void Main(string[] args)
{
string testclass = "Mymemberinfo";

Console.WriteLine("\nMembers in class : {0}",testclass);

Type MyType = Type.GetType(testclass);

MemberInfo[] Mymemberinfoarray = MyType.GetMembers();

Console.WriteLine("\nThere are {0} members in {1}",
Mymemberinfoarray.Length,MyType.FullName);

for (int counter = 0;counter < Mymemberinfoarray.Length;counter++)
{
Console.WriteLine("{0}. {1} Member type - {2}",
counter,
Mymemberinfoarray[counter].Name,
Mymemberinfoarray[counter].MemberType.ToString());
}
}
}

//Tony
 
J

Jon Skeet [C# MVP]

Tony Johansson said:
What I don't understand of this is rownumber 4 which is set_myValue Member
type - Method
I know that rownumber 4 and rownumber 8 is connected because if I remove
property myValue from the program and run the program again then both
rownumber 4 and rownumber 8 is removed.

So can somebody explain why rownumber 4 is shown just because I have a
property that is called myValue.

Properties are just methods (either one or two, depending on whether or
not you have both a getter and a setter) which are decorated in a way
which lets the compiler know what you mean.

If you use ildasm, you'll find that all property access is done by
method calls.
 

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