lambda - Is it possible to change the Target of an Action at runtime in C#? -


I am trying to do something similar to the function of JavaScript.

I have an action:

  var action = new action ((=) = & gt; {this.SomeProperty = 123;});   

At runtime, I want to execute a different completion function so that the example "this" is a dynamic object.

Something like this:

  var action = new action ((=) => gt; {this.SomeProperty = 123;}); Dynamic myDynamic = new ExpandoObject (); MyDynamic.SomeProperty = 321; Action.Bind (myDynamic); & Lt; - Of course this does not work ... action.DynamicInvoke (); Console.WriteLine (myDynamic.SomeProperty); // 123 should write ... I think it is not possible that what I am learning is how the runtime works in relation to lambda and mobility. Perhaps the way to use some reflection?  

Thanks Lance

Any advice is highly appreciated.

You do not really need to do anything, you need to take action by taking a dynamic parameter is.

  Action & gt; Dynamic & gt; Method = new action & lt; Dynamic & gt; (Obj) => obj.SomeProperty = 123);   

You can call action with any object (obviously throw some exceptions if they can not set that property)

 < Code> method (new expando object)); Method (new object ()); // Exception   

If you want to call it as your example, then it will be as easy to create an extension method:

  public static Square DynamicActionExtensions {public static zero dynamic invocation & lt; T & gt; (This is t real) {dynamic obj = real; Obj.SomeProperty = 123; }}   

Edit: How about:

  Public class Invoker {secure list & gt; Dynamic & gt; _objects = null; Safe action & lt; Dynamic & gt; _method = null; Public Inviter () {_objects = New List & gt; Dynamic & gt; (); } Public Zero Bind (dynamic real) {_objects.Add (real); } Public Zero SetDeliget & lt; T & gt; (Action & lt; T & gt; Action) {_method = action; // should work due to constitutional type assignment} Public Zero Dynamic Invoke () {_objects.ForEach (x = & gt; _method (x)); }} Invoker x = new applicant (); X.SetDelegate & lt; SomeTypeForIntellisense & gt; (Obj) => obj.SomeProperty = 123); X. Bind (new Expando object ()); X.DynamicInvoke ();    

Comments