Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6677455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:07:20+00:00 2026-05-26T04:07:20+00:00

Lets say i have the following code private Func<T> _method; public void SetExecutableMethod<T>(Func<T> methodParam)

  • 0

Lets say i have the following code

private Func<T> _method;

public void SetExecutableMethod<T>(Func<T> methodParam)
{
    _method = methodParam;
}

public T ExecuteMethod(object[] parameterValues)
{
    //get the number of parameters _method has;
    var methodCallExpression = _method.Body as MethodCallExpression;
    var method = methodCallExpression.Method;
    ParameterInfo[] methodParams = method.GetParameters();

    //So i now have a list of parameters for the method call,
    //How can i update the parameter values for each of these?
    for (int i = 0; i < parameters.Count(); i++ )
    {
        methodParams[i] = ???''
    }

    return _method.Compile()();
}

public void InitAndTest()
{
    SetExecutableMethod( () => _service.SomeMethod1("param1 placeholder", "param2 placeholder") );

    T result1 = ExecuteMethod(new object[]{"Test1", "Test2"});
    T result2 = ExecuteMethod(new object[]{"Test3", "Test4"}););
}

In the above code, i want to set a private variable to some Func that points to an anonymoust function and never have to set it again.
I then would like to be able to call ExecuteMethod(…) with different parameters. This method should update the parameter values of the variable _method and then invoke the method.
I can read the number of parameters and their values fine, i just am not sure how to set the values for those parameter? Any thoughts on this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:07:21+00:00Added an answer on May 26, 2026 at 4:07 am

    This is not the way to do it. Right now, your _method field is a delegate of type Func<T>, and you expect that its body contains yet another method which is actually executed. That is a lot to expect from your callers. I would forget about this approach and look for something different.

    One way would be to supply a method which takes an array of objects as its parameter (Func<object[], T>), and then invoke it directly with appropriate parameters (but never a method in its body). Even this is less common for a strongly typed language like C# since you lose all type safety (but then again, you do want to be pretty flexible with this framework you are designing).

    The other way would be to get a MethodInfo instance, and then use its Invoke method. In a way, this might even express your intents better, because it will be obvious that the executable method is capable of virtually anything.

    Next, you could use generics to get some type safety, and require that all your input parameters are wrapped inside a single parameter class. In that case, you might have a strongly typed Func<Tparam, Tresult> method, and your Execute method would accept a Tparam instance as its parameter. This would void the need for any reflection.

    [Edit]

    As I wrote, I would try to avoid reflection. Since you wrote you basically need a cache of method results, a simple approach might be something like:

    1. Create a wrapper for your list of parameters so that you can compare them “by value”. I added an example class, but you might even want to allow passing an IEqualityComparer explicitly, so that you don’t have to override Equals for each partial parameter.

      // implements `IEquatable` for a list of parameters
      class Parameters : IEquatable<Parameters>
      {
          private readonly object[] _parameters;
          public Parameters(object[] parms)
          {
              _parameters = parms;
          }
      
          #region IEquatable<Parameters> Members
      
          public bool Equals(Parameters other)
          {
              if (other == null)
                  return false;
      
              if (_parameters.Length != other._parameters.Length)
                  return false;
      
              // check each parameter to see if it's equal
              // ...     
          }
      
          public override bool Equals(object obj)
          {
              return Equals(obj as Parameters);
          }
      
          public override int GetHashCode()
          { ... }
      
          #endregion
      }
      
    2. Create a cache for a single service. Using the wrapper class above, it should simply check if a cached result exists:

      // contains cached results for a single service
      class CachedCallInfo
      {
          private readonly Func<object[], object> _method;
          private readonly Dictionary<Parameters, object> _cache
              = new Dictionary<Parameters, object>();
      
          public CachedCallInfo(Func<object[], object> method)
          {
              _method = method;
          }
      
          public T GetResult<T>(params object[] parameters)
          {
              // use out Parameters class to ensure comparison
              // by value
              var key = new Parameters(parameters);
              object result = null;
      
              // result exists?
              if (!_cache.TryGetValue(key, out result))
              {
                  // do the actual service call
                  result = _method(parameters);
      
                  // add to cache
                  _cache.Add(key, result);
              }
              return (T)result;
          }
      }
      
    3. Create the final class which will reference services by name:

      public class ServiceCache
      {
          private readonly Dictionary<string, CachedCallInfo> _services =
              new Dictionary<string, CachedCallInfo>();
      
          public void RegisterService(string name, Func<object[], object> method)
          {
              _services[name] = new CachedCallInfo(method);
          }
      
          // "params" keyword is used to simplify method calls
          public T GetResult<T>(string serviceName, params object[] parameters)
          {
              return _services[serviceName].GetResult<T>(parameters);
          }
      }
      

    Your cache setup will then look like this:

    serviceCache.RegisterService("ServiceA", @params => DoSomething(@params));
    serviceCache.RegisterService("ServiceB", @params => SomethingElse(@params));
    

    And you would simply call it like this:

    var result = serviceCache.GetResult("ServiceA", paramA, paramB, paramC);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say that I have the following code: public class Shelter<A extends Animal, B
Lets say I have the following code. double *return_array(void) { double foo[2]; foo[0] =
Lets say I have the following code: public class Collection implements CollectionInterface{ ElementInterface[] elementArray
Let's say I have the following code: public class Foo { private int x;
Lets say I have the following code: abstract class Animal case class Dog(name:String) extends
Let's say I have the following ruby code : def use_object(object) puts object.some_method end
Let's say we have the following piece of code: public class Event { }
Lets say I have the following code: delegate int MyDel (int n); // my
Let's say I have the following class: class MyClass { private: int Data; public:
Lets say i have the following scenario: public class A { public String createString(final

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.