Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are facing two separate problems:</p> <ul> <li>Invoking chained methods, and</li> <li>Invoking methods that take lambdas as parameters</li> </ul> <p>Let's deal with the two separately.</p> <p>Let's say that you have the following information available:</p> <ul> <li>A <code>ConstructorInfo</code> representing the first item in the chain (the constructor)</li> <li>An array of objects representing constructor's parameters</li> <li>An array of <code>MethodInfo</code> objects - one for each chained function</li> <li>An array of object arrays representing parameters of each chained function</li> </ul> <p>Then the process of constructing the result would look like this:</p> <pre><code>ConstructorInfo constr = ... object[] constrArgs = ... MethodInfo[] chainedMethods = ... object[][] chainedArgs = ... object res = constr.Invoke(constrArgs); for (int i = 0 ; i != chainedMethods.Length ; i++) { // The chaining magic happens here: res = chainedMethods[i].Invoke(res, chainedArgs[i]); } </code></pre> <p>Once the loop is over, your <code>res</code> contains a configured object.</p> <p>The code above assumes that there are no generic methods among the chained methods; if some of the methods happen to be generic, you would need an additional step in making a callable instance of a generic method before calling <code>Invoke</code>.</p> <p>Now let's look at the lambdas. Depending on the type of the lambda that is passed to the method, you need to pass a delegate with a particular signature. You should be able to use <a href="http://msdn.microsoft.com/en-us/library/system.delegate.aspx" rel="nofollow"><code>System.Delegate</code></a> class to convert methods into callable delegates. You may need to create support methods that implement the delegates that you need. It is hard to say how without seeing the exact methods that you must be able to call through reflection. You may need to go for expression trees, and get <code>Func&lt;...&gt;</code> instances after compiling them. The call of <code>x.Color("Red")</code> would look like this:</p> <pre><code>Expression arg = Expression.Parameter(typeof(MyCarType)); Expression red = Expression.Constant("Red"); MethodInfo color = typeof(MyCarType).GetMethod("Color"); Expression call = Expression.Call(arg, color, new[] {red}); var lambda = Expression.Lambda(call, new[] {arg}); Action&lt;MyCarType&gt; makeRed = (Action&lt;MyCarType&gt;)lambda.Compile(); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload