简体   繁体   English

C#嵌套结构体属性的反射

[英]c# Reflection of a nested struct property

I'm trying to do some dynamic text to object parsing, however, I have run into a snag when creating and setting nested property values for structs. 我正在尝试对对象进行一些动态文本分析,但是,在创建和设置结构的嵌套属性值时遇到了麻烦。

If I have a property in an object that is a struct, whenever I use reflection to obtain the struct object and set any of it's properties/fields, the value on the object is not changed. 如果我在作为结构的对象中有一个属性,则每当我使用反射获取该结构对象并设置其任何属性/字段时,该对象上的值都不会更改。 Take the object graph below. 拿下面的对象图。

public struct MyStruct
{
    public int MyIntProp {get;set;}
}

public class MyObj
{
    public MyStruct NestedStruct {get;set;}
}

PropertyInfo pInfo = (myObj.GetType()).GetProperty("NestedStruct");
object nestedStruct = pInfo.GetValue(myObj); // This is the nested struct however it is only a copy not the actual object
PropertyInfo intInfo = (pInfo.PropertyType).GetProperty("MyIntProp");
intInfo.SetValue(nestedStruct, 23); // this sets the MyIntProp on the nestedStruct, but it is not set on the myObj.NestedStruct.  My goal is to set object on that NestedStruct via reflection.

When I use reflection to obtain the NestedStruct property and then set the MyIntProp on that struct, the original MyObj.NestedStruct.MyIntProp doesn't change. 当我使用反射获取NestedStruct属性,然后在该结构上设置MyIntProp时,原始MyObj.NestedStruct.MyIntProp不会更改。 Naturally I attribute this to the fact that the struct is a value type and not a reference type. 自然,我将其归因于struct是值类型而不是引用类型的事实。

So the real question is how I can use reflection to obtain the reference to a value type. 因此,真正的问题是我如何使用反射来获取对值类型的引用。

If I have a property in an object that is a struct, whenever I use reflection to obtain the struct object and set any of it's properties/fields, the value on the object is not changed. 如果我在作为结构的对象中有一个属性,则每当我使用反射获取该结构对象并设置其任何属性/字段时,该对象上的值都不会更改。

Well, no - because on this line: 好吧,不-因为在这一行:

object nestedStruct = pInfo.GetValue(myObj);

... you're copying the value, as it's a value type. ...您正在复制值,因为它是值类型。 You're also boxing it - which is fine, because it means when you mutate it in the intInfo.SetValue call, it's the boxed copy which is modified. 您还可以将其装箱-很好,因为这意味着当您在intInfo.SetValue调用中对其进行突变时,它是被装箱的副本。 (If you were boxing it at the point of calling intInfo.SetValue , that would really be hopeless.) (如果要在调用intInfo.SetValue时将其intInfo.SetValue ,那将是毫无希望的。)

After you've made the change within the boxed copy, you then need to set it back into the property of the object: 在盒装副本中进行更改后,需要将其重新设置为对象的属性:

pInfo.SetValue(myObject, nestedStruct);

It would be instructive to try doing what you're doing not using reflection: 尝试使用反射来做您正在做的事情将是有益的:

MyObj myObj = new MyObj();
myObj.NestedStruct.MyIntProp = 23;

The compiler will give you an error on the last line, explaining that it's a silly thing to do: 编译器将在最后一行给您一个错误,解释这是一件很愚蠢的事情:

Test.cs(20,9): error CS1612: Cannot modify the return value of 'MyObj.NestedStruct' because it is not a variable Test.cs(20,9):错误CS1612:无法修改'MyObj.NestedStruct'的返回值,因为它不是变量

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

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