469,628 Members | 1,231 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,628 developers. It's quick & easy.

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(IAttributeMain attr, string Name, string
Value)
{
if (attr!= null)
{
Type type = attr.GetType();
fieldInfo = type.GetField(Name, BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.Public);
if (fieldInfo != null)
{
fieldInfo.SetValue(attr, Value);
}
}
}
}

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

/J E E
Nov 15 '05 #1
3 4906
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]
- mv*@spam.guard.caspershouse.com

"J E E" <jo***********@six.se> wrote in message
news:a1**************************@posting.google.c om...
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(IAttributeMain attr, string Name, string
Value)
{
if (attr!= null)
{
Type type = attr.GetType();
fieldInfo = type.GetField(Name, BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.Public);
if (fieldInfo != null)
{
fieldInfo.SetValue(attr, Value);
}
}
}
}

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

/J E E

Nov 15 '05 #2
Solved it using:

FieldInfo fieldInfo = type.BaseType.GetField(Name,
BindingFlags.Instance | BindingFlags.GetField);
Nov 15 '05 #3
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]" <mv*@spam.guard.caspershouse.com> wrote in message news:<#w**************@TK2MSFTNGP12.phx.gbl>...
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]
- mv*@spam.guard.caspershouse.com

Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Tomislav Bartolin | last post: by
3 posts views Thread by trevorelbourne | last post: by
10 posts views Thread by Abelardo Vacca | last post: by
16 posts views Thread by Richard Brown | last post: by
4 posts views Thread by RedHair | last post: by
1 post views Thread by lactaseman | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.