简体   繁体   中英

Call a generic non-static method with dynamic type and generic callback as parameter

Developing an app for Android (and later iOS) using Xamarin/Mono. Normally I use this code to call a non-static generic method and it works great:

serverService.GetCustomListObject<T> (firstRequestInLine,
                                      null,
                                      onGetCustomListObjectFromThread<T>,
                                      onGetCustomListObjectFromThreadError); 

where the callbacks are defined as:

private void onGetCustomListObjectFromThread<T> (List<T> list, 
                                                 RequestStateGen<T>.SuccessfullDelegateType successDel
{ ... }

and

private void onGetCustomListObjectFromThreadError (String error,
                                                   WebRequest failedRequest)
{ ... }

However, now I need to call GetCustomListObject<t> where t is set dynamically. I am quite new to generics but have tried the following code from other examples without success:

typeof(ServerService).GetMethod ("GetCustomListObject").MakeGenericMethod (t).Invoke (serverService, new object[] {
        firstRequestInLine,
        null,
        typeof(LocalServerService).GetMethod ("onGetCustomListObjectFromThread").MakeGenericMethod (t),
        typeof(LocalServerService).GetMethod ("onGetCustomListObjectFromThreadError")
        });

where LocalServerService is the class all my examples here are in and serverService is of type ServerService

I get the following error:

Error: Ambiguous matching in method resolution

Edit: GetCustomListObject in ServerService:

public void GetCustomListObject<T> (WebRequest request,
                                    RequestStateGen<T>.SuccessfullDelegateType successDelegate,
                                    RequestStateGen<T>.InternalSuccessDelegateType internalSuccessDelegate,
                                    RequestStateGen<T>.ErrorDelegateType errorDelegate)

In your original code, you were calling a method passing in delegates.

In your reflection code, you appear to be passing in MethodInfo values instead - I don't believe they will automatically be converted to delegates.

Unfortunately it's hard to give a good code sample without knowing the declaration of your GetCustomListObject method, but you want something like:

Type thirdArgType = typeof(Foo<>).MakeGenericGenericType(t);
MethodInfo thirdArgMethod = typeof(LocalServerService)
                                .GetMethod("onGetCustomListObjectFromThread",
                                           BindingFlags.Instance | BindingFlags.NonPublic)
                                .MakeGenericMethod(t);
Delegate thirdArg = Delegate.CreateDelegate(thirdArgType, this, thirdArgMethod);

MethodInfo fourthArgMethod = typeof(LocalServerService)
                                 .GetMethod("onGetCustomListObjectFromThreadError",
                                            BindingFlags.Instance | BindingFlags.NonPublic);
Delegate fourthArg = Delegate.CreateDelegate(typeof(Bar), this, fourthArgMethod);

MethodInfo method = typeof(ServerService).GetMethod("GetCustomListObject")
                                         .MakeGenericMethod (t);
method.Invoke(serverService,
    new object[] {firstRequestInline, null, thirdArg, fourthArg });

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