简体   繁体   English

C#Reflection - 更改变量字段的值

[英]C# Reflection - changing the value of a field of a variable

I have an instance of a class, I want to change an object data member of this instance only with another object of the same type (swap), due to my system constraints i can't use =,new or setter operators. 我有一个类的实例,我想改变这个实例的目的数据成员只能用相同类型(交换)时,由于我的系统约束我不能使用=,新的或设定器运营商的另一目的。

Basically I would like to change the value of a field of a variable, the field is an object contained inside another object - the variable which its instance I have. 基本上我想改变一个变量字段的值,该字段是另一个对象中包含的对象 - 我的实例所拥有的变量。

is it possible using reflection? 是否有可能使用反射? if so can someone please give me basic direction? 若有人可以请给我基本指示?

Thanks Yoav 谢谢Yoav

Yes, its possible. 是的,它可能。

In short, do something like 简而言之,做点什么

Type typeInQuestion = typeof(TypeHidingTheField);
FieldInfo field = typeInQuestion.GetField("FieldName", BindingFlags.NonPublic | BindingFlags.Instance);
field.SetValue(instanceOfObject, newValue);

to change the value of a hidden (private/protected/internal) field. 更改隐藏(私有/受保护/内部)字段的值。 Use the corresponding FieldInfo.GetValue(...) to read; 使用相应的FieldInfo.GetValue(...)来读取; combine the two trivially to get your desired swapping operation. 将这两者简单地结合起来,以获得所需的交换操作。

Don't hold me to the BindingFlags (I always seem to get those wrong on the first attempt) or the exact syntax, but that's basically it. 不要抱我BindingFlags(我似乎总是在第一次尝试时弄错了)或确切的语法,但基本上就是这样。

Look over System.Reflection for reference. 查看System.Reflection以供参考。

If you use .NET 3.5, you can use my open-source library, Fasterflect , to address that with the following code: 如果您使用.NET 3.5,则可以使用我的开源库Fasterflect通过以下代码解决该问题:

typeof(MyType).SetField("MyField", anotherObject);

When using Fasterflect, you don't have to bother with the right BindingFlags specification and the performance implication (as when using reflection). 使用Fasterflect时,您不必费心使用正确的BindingFlags规范和性能含义(如使用反射时)。

in vb with generics, but rudimentary error handling: 在vb中使用泛型,但是基本的错误处理:

Module somereflectionops
    Function GetFieldValue(Of OBTYPE, FIELDTYPE)(instance As OBTYPE, fieldname As String,  Optional specbindingflags As BindingFlags = BindingFlags.NonPublic Or BindingFlags.Instance) As FIELDTYPE
        Dim ot As Type = GetType(OBTYPE)
        Dim fi As FieldInfo
        Try
            fi = ot.GetField(fieldname, BindingFlags.Default Or specbindingflags)
            If fi Is Nothing Then Return Nothing
            Return fi.GetValue(instance)
        Catch ex As Exception
            Return Nothing
        End Try
    End Function

    Function SetFieldValue(Of OBTYPE, FIELDTYPE)(instance As OBTYPE, fieldname As String, value As FIELDTYPE, Optional specbindingflags As BindingFlags = BindingFlags.NonPublic Or BindingFlags.Instance) As Boolean
        Dim ot As Type = GetType(OBTYPE)
        Dim fi As FieldInfo
        Try
            fi = ot.GetField(fieldname,  BindingFlags.Default Or specbindingflags)
            If fi Is Nothing Then Return false
            fi.SetValue(instance, value)
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function
End Module

use: SetFieldValue(Of cartonclass, Integer)(cartonyoudropped, "survivingeggcount", 3) use:SetFieldValue(of cartonclass,Integer)(cartonyoudropped,“survivingeggcount”,3)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM