C# Reflection - changing the value of a field of a variable

后端 未结 3 444
轮回少年
轮回少年 2021-01-01 01:40

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 c

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-01 02:12

    If you use .NET 3.5, you can use my open-source library, Fasterflect, to address that with the following code:

    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).

提交回复
热议问题