Reflection and setters & getters

S

Shmuel

Hi,

I need to get public fields that are set as follows:

private string _name;
public string name
{
set { _name = value; }
get { return _name; }
}

I've tried something like this:

Language l = Language.createEmptyInstance();
l.name = "ADSFAFDADSF";

FieldInfo field = l.GetType().GetField("_name", BindingFlags.GetField |
BindingFlags.NonPublic | BindingFlags.Instance |
BindingFlags.Public);
Object val = field.GetValue(l);

MessageBox.Show(l.ToString() + "\n\n" + val.ToString());

It works for normal variables, but not those that are set by setters and
getters.

Any idea how to get those ones too?


Thanks,

Shmuel
 
J

Jon Skeet [C# MVP]

Shmuel said:
I need to get public fields that are set as follows:

private string _name;
public string name
{
set { _name = value; }
get { return _name; }
}

There's no public field there. There's a private field, and a public
property. You can get at either of them (assuming you have the right
permission) - but your code is trying to get at a public field, and
that doesn't exist.
 
S

Shmuel

Thanks for your replies.
I didn't know that it was a property and not a field.

Shmuel
 

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