Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In general, everything in your answer is possible, except for this line:</p> <pre><code>result = svcClient.serviceRefMethod; </code></pre> <p>Which is obviously a crucial call... In order to dynamically invoke a function on an object you can do a few things. An easy one is to change your function signature to:</p> <pre><code>public T CallServiceReference&lt;T&gt;(ServiceReference svcClient, method serviceRefMethod) </code></pre> <p>but then calling code needs to new up the <code>ServiceReference</code> and pass in a reference to <code>svcClient.[desiredFunction]</code> as the <code>serviceRefMethod</code>.</p> <p>An alternative is to change your signature to:</p> <pre><code>public T CallServiceReference&lt;T&gt;(string serviceRefMethodName) </code></pre> <p>and then use Reflection to find the method and invoke it. You will get no compile time validation (so if you have a typo it'll crash at runtime) but you'll get dynamic invokes. For example:</p> <pre><code>svcClient.GetType().InvokeMember( methodName, /* what you want to call */ /* Specifies what kinds of actions you are going to do and where / how to look for the member that you are going to invoke */ System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.InvokeMethod, null, /* Binder that is used for binding */ svcClient, /* the object to call the method on */ null /* argument list */ ); </code></pre> <hr> <h2>Extra info based on your update (P.S. That probably could have been a separate question)</h2> <p>You now want to pass in not just a method but also the invocation of the method. Since not every method is invoked the same you are trying to do this at the call site, but that is before you actually want to invoke the method. Essentially what you're trying to do is shuttle around code that will only be executed later on (in the context of <code>GetDataFromService</code>).</p> <p>You can either go the reflection route (in which case you pass in an <code>object[]</code> of parameters that you pass to the <code>InvokeMember</code> call or you look into <a href="http://msdn.microsoft.com/en-us/library/bb549151.aspx" rel="nofollow"><code>Func</code></a> which allows you to create some code that you run whenever you invoke the <code>Func</code>. For example:</p> <pre><code> GetDataFromService(new Func&lt;object&gt;(() =&gt; { return client.DataCall(username); })); </code></pre>
    singulars
    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