Can I change a private readonly inherited field in C# using reflection?
like in java I have:
Class.getSuperClass().getDeclaredFields()
how I can know and set private field from a superclass?
I know this is strongly not recommended, but I am testing my application and I need simulate a wrong situation where the id is correct and the name not. But this Id is private.
Yes, it is possible to use reflection to set the value of a readonly field after the constructor has run
var fi = this.GetType()
.BaseType
.GetField("_someField", BindingFlags.Instance | BindingFlags.NonPublic);
fi.SetValue(this, 1);
EDIT
Updated to look in the direct parent type. This solution will likely have issues if the types are generic.
Yes, you can.
For fields, use the FieldInfo
class. The BindingFlags.NonPublic
parameter allows you to see private fields.
public class Base
{
private string _id = "hi";
public string Id { get { return _id; } }
}
public class Derived : Base
{
public void changeParentVariable()
{
FieldInfo fld = typeof(Base).GetField("_id", BindingFlags.Instance | BindingFlags.NonPublic);
fld.SetValue(this, "sup");
}
}
and a small test to prove it works:
public static void Run()
{
var derived = new Derived();
Console.WriteLine(derived.Id); // prints "hi"
derived.changeParentVariable();
Console.WriteLine(derived.Id); // prints "sup"
}
This class will let you do it:
http://csharptest.net/browse/src/Library/Reflection/PropertyType.cs
Usage:
new PropertyType(this.GetType(), "_myParentField").SetValue(this, newValue);
BTW, It will work on public/non-public fields or properties. For ease of use you can use the derived class PropertyValue like this:
new PropertyValue<int>(this, "_myParentField").Value = newValue;
链接地址: http://www.djcxy.com/p/3612.html