Reflection and setters & getters

  • Thread starter Thread starter Shmuel
  • Start date Start date
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
 
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.
 
Back
Top