Re: Reflection and setters & getters
- From: Shmuel <matti@xxxxxxxxxxxxxxx>
- Date: Tue, 23 Sep 2008 09:55:47 +0300
Thanks for your replies.
I didn't know that it was a property and not a field.
Shmuel
Peter Duniho wrote:
On Mon, 22 Sep 2008 12:51:32 -0700, Shmuel <matti@xxxxxxxxxxxxxxx> wrote:.
Hi,
I need to get public fields that are set as follows:
private string _name;
In your example, "_name" is not a public field and "name" is a property, not a field. So, your question is already self-contradicting.
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.
What isn't working for you? The fact that a field, private or otherwise, is accessed in a property doesn't change anything about how the _field_ itself might be accessed via reflection. In your example, you do appear to be allowing for the possibility of a non-public field, so the code you posted doesn't seem to have any obvious problem with it. It should work fine.
Of course, without a concise-but-complete code sample, it's not possible to know for sure. For example, to assert that the code you posted works fine, I need to make the assumption that the field and property described are in the class "Language". But that seems like a reasonable assumption here.
Pete
- References:
- Reflection and setters & getters
- From: Shmuel
- Re: Reflection and setters & getters
- From: Peter Duniho
- Reflection and setters & getters
- Prev by Date: Chopard Happy Sport Diamond Moon/Stars Steel Mother-of-pearl Ladies Watch 27/8250-23
- Next by Date: WPF - Max window size for hardware acceleration?
- Previous by thread: Re: Reflection and setters & getters
- Next by thread: Re: Reflection and setters & getters
- Index(es):
Relevant Pages
|