简体   繁体   中英

Pass different methods as parameter to invoke by method name

I have several methods in a Util class with different return types and different parameters, fe:

int RealMethodToExecute(int i, string s) { ... }

Those methods must be invoked in the ui thread and the call would look something like that:

int x = (int)InvokeControl.Invoke("Util.RealMethodToExecute");

Because in my opinion using the method names as string is not really pretty, i thought at the following solution:

public int WrappedRealMethodToExecute(int i, string s)
{
    return InvokeMethod(Util.RealMethodToExecute, i, s);
}

private static T InvokeMethod<T>(Func<int, string, T> func, params object[] p)
{            
    return (T)InvokeControl.Invoke(func.Method.DeclaringType.FullName + "." + func.Method.Name, p);
}

The problem now is that InvokeMethod only acceppts methods with an int and a string parameter and a return value but i have different methods which i want to execute.

I dont want to overload InvokeMethod . I just need the full qualified name of the function to execute it.

Any ideas??

edit: extended example with parameters

Fankly, this might as well be just:

private static object InvokeMethod(Delegate method, params object[] args)
{            
    return InvokeControl.Invoke(method, args);
}

where InvokeControl.Invoke uses (to get to the UI thread):

someControl.Invoke(method, args);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM