472,353 Members | 1,565 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

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 5002
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Steve | last post by:
Hello, I'm encountering an unexpected behavior when using the "new" modifier in a derived class to hide an inherited base class property. I use...
3
by: Tomislav Bartolin | last post by:
Hi I'm trying do create a generic base class for all of my data objects. One of the main requirements for this class is to be able to handle all...
3
by: trevorelbourne | last post by:
Hi, I am having trouble accessing the elements of an array using reflection. This is the code I am having trouble with: FieldInfo Fields =...
10
by: Abelardo Vacca | last post by:
Hi, The title sums up the question pretty much. I would like to access all private members of a class including the private members of its base...
16
by: Richard Brown | last post by:
Ok, now I am truely going nuts... probably why I didn't use the Class Builder in VB6 extensively. But, being the 'proper programmer' that I...
6
by: Philip Warner | last post by:
I have a set of classes: C (base) C1 (inherits C) C2 (inherits C) ... Cn (inherits C) (the C1..Cn also have subclasses, but thats not...
4
by: RedHair | last post by:
I have a base page class A which inherits System.Web.UI.Page class then a aspx page B inherits class A and have a master page C. There is a...
1
by: lactaseman | last post by:
While I know this is not the correct venue... I realize this is of little to no importance to most out there... however, if I had found this in my...
10
by: Rotsey | last post by:
Hi, I have a class that exist in my UI assembly that inherits from a class in a referenced assembly like this. class EmployeesDC { private...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.