Can I change a private readonly inherited field in C# using reflection?

巧了我就是萌 提交于 2019-12-01 17:23:45

问题


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.


回答1:


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.




回答2:


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"
}



回答3:


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;



回答4:


Like JaredPar suggests, I did the follow:

//to discover the object type
Type groupType = _group.GetType();
//to discover the parent object type
Type bType = groupType.BaseType;
//now I get all field to make sure that I can retrieve the field.
FieldInfo[] idFromBaseType = bType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);

//And finally I set the values. (for me, the ID is the first element)
idFromBaseType[0].SetValue(_group, 1);

Thanks to all.



来源:https://stackoverflow.com/questions/1401458/can-i-change-a-private-readonly-inherited-field-in-c-sharp-using-reflection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!