Access base class fields using reflection

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • J E E

    Access base class fields using reflection

    Hi!

    Is it possible to access fields in a derived class using reflection?

    Code below works fine when I access it as a private member in the Page
    class, but not when accessing base class member through an interface
    reference.

    I have tried to change the snd argement to SetAttribute method from
    'Name', 'set_Name' to '_name'. That doesn't seem to be the problem. I
    have also tried using different binding flags, without luck.

    Any ideas on what I am doing wrong?

    public interface IAttributeMain
    {
    string ID { get; set; }
    string Name { get; set; }
    string Type { get; set; }
    }

    public class AttributeMain : IAttributeMain
    {
    private string _id;
    private string _name;
    private string _type;

    // Set/Get Implementation
    ...

    }

    public class Page : AttributeMain
    {
    }

    private void SetAttribute(IA ttributeMain attr, string Name, string
    Value)
    {
    if (attr!= null)
    {
    Type type = attr.GetType();
    fieldInfo = type.GetField(N ame, BindingFlags.No nPublic |
    BindingFlags.In stance | BindingFlags.Pu blic);
    if (fieldInfo != null)
    {
    fieldInfo.SetVa lue(attr, Value);
    }
    }
    }
    }

    static Main()
    {
    IAttributeMain attr = new Page();
    SetAttribute(at tr, "Name", "10");
    }

    /J E E
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Access base class fields using reflection

    J E E,

    Interfaces don't have fields, so that is why this is failing. What you
    have to do is get the implementation of the interface, and then you can
    perform what you are doing.

    However, this is a very, very bad design. If you want to get at the
    fields, then you should expose them in some manner that fits into the
    overall design of your application. Depending on reflection to perform
    operations on fields/methods/properties that are hidden because of
    accessiblity is a bad, bad idea. There is a reason these things are marked
    private/internal/protected.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "J E E" <jonas.ekstrom@ six.se> wrote in message
    news:a1a164a8.0 311040743.1a804 e13@posting.goo gle.com...[color=blue]
    > Hi!
    >
    > Is it possible to access fields in a derived class using reflection?
    >
    > Code below works fine when I access it as a private member in the Page
    > class, but not when accessing base class member through an interface
    > reference.
    >
    > I have tried to change the snd argement to SetAttribute method from
    > 'Name', 'set_Name' to '_name'. That doesn't seem to be the problem. I
    > have also tried using different binding flags, without luck.
    >
    > Any ideas on what I am doing wrong?
    >
    > public interface IAttributeMain
    > {
    > string ID { get; set; }
    > string Name { get; set; }
    > string Type { get; set; }
    > }
    >
    > public class AttributeMain : IAttributeMain
    > {
    > private string _id;
    > private string _name;
    > private string _type;
    >
    > // Set/Get Implementation
    > ..
    >
    > }
    >
    > public class Page : AttributeMain
    > {
    > }
    >
    > private void SetAttribute(IA ttributeMain attr, string Name, string
    > Value)
    > {
    > if (attr!= null)
    > {
    > Type type = attr.GetType();
    > fieldInfo = type.GetField(N ame, BindingFlags.No nPublic |
    > BindingFlags.In stance | BindingFlags.Pu blic);
    > if (fieldInfo != null)
    > {
    > fieldInfo.SetVa lue(attr, Value);
    > }
    > }
    > }
    > }
    >
    > static Main()
    > {
    > IAttributeMain attr = new Page();
    > SetAttribute(at tr, "Name", "10");
    > }
    >
    > /J E E[/color]


    Comment

    • J E E

      #3
      Re: Access base class fields using reflection

      Solved it using:

      FieldInfo fieldInfo = type.BaseType.G etField(Name,
      BindingFlags.In stance | BindingFlags.Ge tField);

      Comment

      • J E E

        #4
        Re: Access base class fields using reflection

        Nicholas,

        You're right since I solved the problem using Type.BaseType.

        There is a reason using reflection to access private members in my
        application, but that's another story. Thank you for sharing your
        ideas of proper design.

        J E E

        "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in message news:<#w7RqzuoD HA.1960@TK2MSFT NGP12.phx.gbl>. ..[color=blue]
        > J E E,
        >
        > Interfaces don't have fields, so that is why this is failing. What you
        > have to do is get the implementation of the interface, and then you can
        > perform what you are doing.
        >
        > However, this is a very, very bad design. If you want to get at the
        > fields, then you should expose them in some manner that fits into the
        > overall design of your application. Depending on reflection to perform
        > operations on fields/methods/properties that are hidden because of
        > accessiblity is a bad, bad idea. There is a reason these things are marked
        > private/internal/protected.
        >
        > Hope this helps.
        >
        >
        > --
        > - Nicholas Paldino [.NET/C# MVP]
        > - mvp@spam.guard. caspershouse.co m
        >[/color]

        Comment

        Working...