[英]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. 使用Xamarin / Mono开发适用于Android(及更高版本的iOS)的应用。 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. 但是,现在我需要调用
GetCustomListObject<t>
,其中t
是动态设置的。 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
其中
LocalServerService
是我在此处的所有示例均位于的类,而serverService
的类型为ServerService
I get the following error: 我收到以下错误:
Error: Ambiguous matching in method resolution
Edit: GetCustomListObject in ServerService: 编辑: ServerService中的GetCustomListObject:
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. 在您的反射代码中,您似乎是在传递
MethodInfo
值-我不相信它们会自动转换为委托。
Unfortunately it's hard to give a good code sample without knowing the declaration of your GetCustomListObject
method, but you want something like: 不幸的是很难提供一个良好的代码示例,不知道你的声明
GetCustomListObject
方法,但你想要的东西 ,如:
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 });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.