Re: Reflection and setters & getters

Tech-Archive recommends: Fix windows errors by optimizing your registry



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
.



Relevant Pages

  • Re: non-static method cannot be referenced from a static context
    ... >The sources for TaalKeuze and the class Language are at the bottom of this ... > private JPanel createLanguagePanel() { ... > public void setDefaultLocales (String inputLanguage, ...
    (comp.lang.java.programmer)
  • Re: Separate interface and implemenation problem..
    ... inside an interface ... 'private' means "that which the outside world should not ... Every computer language has its little syntactic quirks. ... There's no question that Java is not quite a perfect language. ...
    (comp.lang.java.programmer)
  • Re: Proposal: Renaming of the "private" Keyword
    ... This is a programming language - learn it, master it, and move on. ... > makes quickly scanning for public members among private ones, ... > concealed SomeType Bleh() ... > concealed void Fewbar() ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Will python never intend to support private, protected and public?
    ... > language ... ... Well I have the following reasons not to like the current python way: ... Beginning all your private variables with an underscore is like ...
    (comp.lang.python)
  • Re: is python Object oriented??
    ... We don't even need no stinkin programming languages, ... unduly complicating the language. ... Suppose a library developer ... change a "private" attribute to public. ...
    (comp.lang.python)