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