C# 以动态类型和泛型回调作为参数调用泛型非静态方法

C# 以动态类型和泛型回调作为参数调用泛型非静态方法,c#,generics,mono,C#,Generics,Mono,使用Xamarin/Mono为Android(以及更高版本的iOS)开发应用程序。通常,我使用此代码调用非静态泛型方法,效果非常好: serverService.GetCustomListObject<T> (firstRequestInLine, null, onGetCustomListObjectFromThread<T&

使用Xamarin/Mono为Android(以及更高版本的iOS)开发应用程序。通常,我使用此代码调用非静态泛型方法,效果非常好:

serverService.GetCustomListObject<T> (firstRequestInLine,
                                      null,
                                      onGetCustomListObjectFromThread<T>,
                                      onGetCustomListObjectFromThreadError); 
但是,现在我需要调用
GetCustomListObject
,其中
t
是动态设置的。我对泛型非常陌生,但从其他示例中尝试了以下代码,但没有成功:

typeof(ServerService).GetMethod ("GetCustomListObject").MakeGenericMethod (t).Invoke (serverService, new object[] {
        firstRequestInLine,
        null,
        typeof(LocalServerService).GetMethod ("onGetCustomListObjectFromThread").MakeGenericMethod (t),
        typeof(LocalServerService).GetMethod ("onGetCustomListObjectFromThreadError")
        });
其中,
LocalServerService
是类,我这里的所有示例都在类中,
serverService
是类型
serverService

我得到以下错误:

Error: Ambiguous matching in method resolution
编辑:服务器服务中的GetCustomListObject:

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

在原始代码中,您正在调用传递委托的方法

在您的反射代码中,您似乎正在传入
MethodInfo
值-我不相信它们会自动转换为委托

不幸的是,如果不知道
GetCustomListObject
方法的声明,就很难给出一个好的代码示例,但是您需要这样的东西:

Type thirdArgType=typeof(Foo);
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);
调用(serverService,
新对象[]{firstRequestInline,null,thirdArg,fourthArg});

对于
GetCustomListObject
onGetCustomListObjectFromThread
onGetCustomListObjectFromThreadError
,是否存在任何其他方法重载?i、 e.相同的名称但不同的签名?我想知道
GetMethod
是否是抛出的东西……错误到底发生在哪里?如果您将所有这些都分解,以便每个反射调用都有单独的语句,这可能会很有用。不,没有重复的方法名称。我很不确定第三个和第四个参数的结构是否正确,因为我没有引用我的
LocalServerService
的实例。我把代码分了一点,它在第三个参数上崩溃了。谢谢!现在差不多了。我假设它应该是
method.Invoke(serverService,新对象[]{firstRequestInLine,null,thirdArg,fourthArg})在最后一行?*我添加了
fouthArg
因为回调也需要是泛型的
Error: Ambiguous matching in method resolution
public void GetCustomListObject<T> (WebRequest request,
                                    RequestStateGen<T>.SuccessfullDelegateType successDelegate,
                                    RequestStateGen<T>.InternalSuccessDelegateType internalSuccessDelegate,
                                    RequestStateGen<T>.ErrorDelegateType errorDelegate)
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 });