简体   繁体   English

C#反思:“指针”到值类型

[英]C# Reflection: “Pointer” to a value-type

Short description 简短的介绍

I want to know if there is a .NET feature letting me manipulate a value type obtained by reflection. 我想知道是否有.NET功能,让我可以操纵通过反射获得的值类型。 So when calling PropertyInfo.getValue(...) on a value type property I want to not get a copy, but the original object and manipulate it. 因此,当在值类型属性上调用PropertyInfo.getValue(...)时,我不想获取副本,而是要获取原始对象并对其进行操作。

I am NOT allowed to use unsafe pointers. 我不允许使用不安全的指针。

Long description 详细描述

This requirement arose because I am implementing a webservice letting me manipulate a Unity3d scene graph. 之所以出现此要求,是因为我正在实现一个Web服务,让我可以操纵Unity3d场景图。

The scene graph might have the following structure 场景图可能具有以下结构

  • GameObject 1 游戏对象1
    • Vector 1.2 矢量1.2
  • GameObject 2 游戏对象2
    • Struct 2.1 结构2.1
    • Vector 2.2 矢量2.2

A client can query the following URI: 客户端可以查询以下URI:

GET http://.../GameObject2/StructProperty/someProperty 获取http://.../GameObject2/StructProperty/someProperty

This works, as it is as simple as traversing the hierarchy via reflection, searching for the property by name (eg Struct or Vector) and calling getValue on the corresponding PropertyInfo, returning this to the client. 这很简单,就像通过反射遍历层次结构,按名称搜索属性(例如Struct或Vector)并在相应的PropertyInfo上调用getValue并将其返回给客户端一样简单。

But a client can also query: 但是客户端也可以查询:

POST http://.../GameObject2/VectorProperty/xProperty with eg 5.4 as the entity body. POST http://.../GameObject2/VectorProperty/xProperty ,例如5.4作为实体。 The x property of the Vector should then be set to 5.4 然后应将Vector的x属性设置为5.4。

What I am doing at the moment is traversing the graph forward (like with GET) till I find the Vector object. 目前,我正在做的事情是向前遍历图形(例如使用GET),直到找到Vector对象。 Then I am doing a recursive setValue UNTIL I am doing the setValue on a reference type eg 然后我做一个递归setValue直到我对引用类型例如setValue

object2.setValue(Vector.setValue(5.4)); object2.setValue(Vector.setValue(5.4));

(for simplicity I am omitting the PropertyInfo part. Assume it is there) (为简单起见,我省略了PropertyInfo部分。假设它在那里)

So I must be able to query an arbitrary object hierarchy containing both value types and reference types. 因此,我必须能够查询包含值类型和引用类型的任意对象层次结构。 Is there a better way for what I am doing? 有什么更好的方式来做我的事情吗?

So when calling PropertyInfo.getValue(...) on a value type property I want to not get a copy, but the original object and manipulate it. 因此,当在值类型属性上调用PropertyInfo.getValue(...)时,我不想获取副本,而是要获取原始对象并对其进行操作。

Then you need access to the field, not the property. 然后,您需要访问字段,而不是属性。 Alternatively, you need to call the getter to retrieve a copy of the value, modify the copy, and then call the setter to change the state of the containing object. 或者,您需要调用getter来获取值的副本,修改副本,然后调用setter来更改包含对象的状态。

That's not just true when you use reflection - it's true in general. 当您使用反射时,不仅如此,而且通常如此。 You can't do something like: 您不能执行以下操作:

foo.Position.X = 10;

where Position is a property whose type is a value type. 其中Position是属性,其类型是值类型。 You'll get a compile-time error. 您会收到一个编译时错误。 Instead, you'd need something like: 相反,您将需要以下内容:

var position = foo.Position;
position.X = 10;
foo.Position = position;

You can do that with reflection - assuming that there is a setter, of course. 你可以做到这一点与反思-假设一个二传手,当然。

Note that when you say "the original object" here, for a value type (stored in a field of that type, ie not boxed) there is no object. 请注意,当你说“原始对象”在这里,一个值类型(存储在该类型的字段,即不盒装)有没有对象。 There's just the value, as a field as part of another object. 作为字段的另一个对象的一部分,只有值。

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

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