有什么方法可以存储泛型类型参数以备将来使用。场景就是这样。
Queue<ApiHandlerHelper> requestQueue = new Queue<ApiHandlerHelper> ();
public void HitApi<T> (ApiHandlerHelper helper)
{
if (_IfHandlerProcessing) {
requestQueue.Enqueue (helper);
} else {
StartCoroutine (checkInternetConnection<T> ());
_IfHandlerProcessing = true;
}
}
HitApi接到来自多个管理者的呼叫,我想检查Hitpi是否正在忙于处理一个管理者的请求,即将到来的请求应放入队列中。现在,我需要存储通用类型参数“ T”,以便在以后的阶段中使用。像这样的东西
AoiHandlerObject.StoreGenerticType<T> ();
我需要存储T类型,以便在我们收到前任经理的回复时可以自动调用HitApi。
您不能存储泛型类型参数,但是可以对泛型参数进行存储。虽然您将无法在编译期间提供泛型参数类型,但可以使用反射来调用泛型方法(或实例化泛型类型):
Type genericArg1 = typeof(T);
现在应该使用反射调用您的方法:
// obj.HitApi<T>(ApiHandlerHelper helper)
typeof(ClassContainingHitApiMethod)
.GetMethod("HitApi", BindingFlags.Public | BindingFlags.Instance)
.MakeGenericMethod(genericArg1)
.Invoke(instanceOfClassContainingHitApiMethod, new object[] { instanceOfApiHelper });
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句