.net - C# Converting a DynamicObject to arbitrary type -


I'm writing a javascript & lt; - & gt; Participate in the C # bridge and the following problem:

A class is JSObject:

  Public class JSObject: DynamicObject {public JSEngineAPI wrappedObject {get; Set; } Public JSObject (JSEngineAPI wrappedObject); Public Override Balls Trail Convert (ConvertBinder Binder, Out Object Result); Public Override Bull TryGetMember (GetMemberBinder Binder, Out Object Result); ...}   

And suppose that is a simple test case like

  public class test class {public string message = "this is a # Is string "; } Public class TestApp {public string testComplexObject (TestClass obj) {obj.message return; }}   

Now I want to be able to

  JSObject jsObj = ...; String message = testComplexObject (jsObj);   

To do obj.message should call TryGetMember () . Effectively, JSOBJ should pretend to be a test class example. Note that the call complex object is just an example to test, later I need to be able to call arbitrary tasks with arbitrary arguments.

I have tried many ways to do this work, but none of them does the work. That's why I'm a good way to get this thing.

I have thought about creating a class at runtime that comes from the test class, members of this dynamic class will be included who surcharge their base class pendants. To do the actual work, each of these methods will proceed to JSObject / JSEngineAPI. So I could pass an example of this dynamic class in the testComplexObject method.

Though it seems that it is really involved and I would love to know if there are any other easy / other approaches on this.

Edit # 1: I think if you remove part of "Dynamic object" then this question is a bit like how can I create a proxy for type T on runtime Am I

Edit # 2: I've also noticed RealProxy and IDynamicMetaObjectProvider too, and I wonder if they're with any help.

Thank you for your time, Mathias

The signature of the law does not change Can be done, so you can create a proxy standing for your dynamic object. This will only be effective if objects are POD or you paste on virtual methods that you can override. Otherwise your methods can not be used. This will apply the desired type but will pass all the access to your actual object. If you add some conversion methods to your dynamic objects, it will be easy to use. Something like this: Public class JSObject: DynamicObject {class TestClassProxy: TestClass {Private dynamic wrapper; Public TestClassProxy (dynamic obj) {wrapper = obj; // Fields assigns copies of message = obj.message; } // Override all necessary methods and properties, public override zero sample method () {wrapper.SampleMethod (); } Public override int SomeValue {get {return wrapper.SomeValue; } Set {wrapper.SomeValue = value; }} // etc ... Public Override Balls Trail Convert (Convertbinder Binder, Out Object Result) {If (Binder. Type == Type (test class)) {Results = New test class proxy (this); Back true; } // Your other conversion returns to base basis Tri convert (binder, out result); } // etc ...}

If you need to do more with your objects, then I do not know how to get this apart from changing the method's signature. You can.

Comments