如何在执行方法后保留泛型方法的类型参数? [英] How can I preserve the type parameters of a generic methods after the execution of the method?

查看:127
本文介绍了如何在执行方法后保留泛型方法的类型参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我会解释我的scnerio。我有一个像这样的静态泛型类。



Hi All,

I will explain my scnerio. I have a static generic class like this.

public static class Cls<T>





此类中有一个通用函数,如下所示。





There is a generic function in this class like below.

public static void Foo<K>()





此函数的目的是创建 Action< T,K> 类型的lambda表达式并存储它在类成员变量中用于其他函数。我的问题是我无法访问函数范围内的 K 类型。这样我就无法创建一个 Action< T,K> 类型的类成员变量。我无法更改类签名,例如 Cls< T,K> ,因为不应该针对单个<$处理不同的 K c $ c> T 类型。



一个解决方案我发现行动< T,K> type函数可以存储在非泛型 Delegate 变量中,并使用 DynamicInvoke 来执行该函数。它会工作,但 DynamicInvoke 的执行时间超出了我的预期。这有什么解决方法吗?非常感谢您给予的任何帮助。



谢谢,

Thomas



The purpose of this function is to create a lambda expression of the type Action<T, K> and store it in a class member variable for use in other functions. My problem is that I cannot access the K type out of the function scope. So that I cannot create a Action<T, K> type class member variable. I cannot change the class signature like Cls<T,K> because different K should be processed against a single T type.

One solution I found that the Action<T, K> type function can be stored in a non generic Delegate variable and use the DynamicInvoke to execute the function. It will work but the execution time of DynamicInvoke is more than I expect. Is there any workaround for this? Any help you can give will be greatly appreciated.

Thanks,
Thomas

推荐答案

最有可能的是,你正朝着错误的方向前进,但我无法确定该做什么,因为你没有告诉我们所有这些活动的最终目的。



相反,我会尝试向您解释一些您可能会错过的泛型概念。



您无法获得 T 或 K 因为它们不存储在任何地方。这根本没有意义。每次使用泛型类型时,例如,实际调用它的方法,你用一些实际类型实例化它,你替换 T 。在您的情况下,情况更复杂,因为您存储静态值。每次调用方法 Foo 存储它时,都会创建此静态值。如果您使用此方法的结果(为什么,为什么不将其作为函数结果返回?)变为无法访问,则情况等同于您从未调用该方法的情况。如果您需要更多详细信息,最好使用因此调用而计算的存储静态值来显示代码。



您可以通过以下方式解决问题:从此方法返回lambda表达式;这样您就可以将它存储在泛型类的实例化之外。但是,整个情况对我来说都是病态的。有可能,您可以提出更合理的代码设计。如果你能解释所有这一切的最终目的,我可能会建议别的。



-SA
Most likely, you are moving in wrong direction, but I cannot tell what to do for sure because you did not tell us the ultimate purpose of all this activity.

Instead, I''ll try to explain you some concept of generics which you might miss.

You cannot get the actual type of T or K because they are not stored anywhere. It simply does not makes sense. Each time you use the generic type, for example, actually call its method, you instantiate it with some actual type which you substitute for T. In your case, the situation is more complex, because you store the static value. This static value is created each time you call the method Foo storing it. If you the result of this method (why, why you don''t return it as a function result?) becomes unreachable, the situation is equivalent to the case when you never called the method. If you need more detail on it, you should better show the code using the stored static value calculated as a result of this call.

You could probably solve the problem by returning your lambda expression from this method; this way you can store it outside the instantiation of a generic class. However, the whole situation looks pathological to me. Chances are, you could come up with more reasonable design of your code. Again, I would probably advise something else if you could explain the ultimate purpose of all that.

—SA


这篇关于如何在执行方法后保留泛型方法的类型参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆