mardi 26 janvier 2016

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. So when calling PropertyInfo.getValue(...) on a value type property I want to not get a copy, but the original object and manipulate it.

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.

The scene graph might have the following structure

  • GameObject 1
    • Vector 1.2
  • GameObject 2
    • Struct 2.1
    • Vector 2.2

A client can query the following URI:

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

This works, as it is as simple as traversing the hierarchy via reflection, searching for the property by name (e.g. Struct or Vector) and calling getValue on the corresponding PropertyInfo, returning this to the client.

But a client can also query:

POST http://.../GameObject2/VectorProperty/xProperty with e.g. 5.4 as the entity body. The x property of the Vector should then be set to 5.4

What I am doing at the moment is traversing the graph forward (like with GET) till I find the Vector object. Then I am doing a recursive setValue UNTIL I am doing the setValue on a reference type e.g.

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

(for simplicity I am omitting the PropertyInfo part. Assume it is there)

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?





Aucun commentaire:

Enregistrer un commentaire