在.NET 2.0你如何通过一个通用的委托参数的方法 [英] How do you pass a generic delegate argument to a method in .NET 2.0
问题描述
我有一类与委托声明如下...
I have a class with a delegate declaration as follows...
Public Class MyClass
Public Delegate Function Getter(Of TResult)() As TResult
''#the following code works.
Public Shared Sub MyMethod(ByVal g As Getter(Of Boolean))
''#do stuff
End Sub
End Class
不过,我不想明确键入消气代表在方法调用。为什么如下我不能声明参数...
However, I do not want to explicitly type the Getter delegate in the Method call. Why can I not declare the parameter as follows...
... (ByVal g As Getter(Of TResult))
有没有办法做到这一点?
Is there a way to do it?
我的最终目标是能够设置一个委托财产getter和setter方法被调用的类。但我的读数表明,你不能这样做。所以我把setter和getter方法的类,然后我想调用类设置代理的参数,然后调用。是否有这样做的最佳实践。
My end goal was to be able to set a delegate for property setters and getters in the called class. But my reading indicates you can't do that. So I put setter and getter methods in that class and then I want the calling class to set the delegate argument and then invoke. Is there a best practice for doing this.
我意识到在上面的例子中,我可以设置设置为从调用类的委托变量...但我想创建紧密封装单身。
I realize in the above example that I can set set the delegate variable from the calling class...but I am trying to create a singleton with tight encapsulation.
有关的记录,我不能使用任何.net35宣布新的委托类型。
For the record, I can't use any of the new delegate types declared in .net35.
答案在C#中是受欢迎的。
Answers in C# are welcome.
有什么想法?
赛斯
推荐答案
您需要一个泛型类型参数添加到方法:
You need to add a generic type parameter to the method:
Public Shared Sub MyMethod(Of TResult) (ByVal g As Getter(Of TResult))
在C#中,这将是
public static void MyMethod<TResult>(Getter<TResult> g) {
这篇关于在.NET 2.0你如何通过一个通用的委托参数的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!