English 中文(简体)
您如何将一般性代表论点通过2.0网络的方法。
原标题:How do you pass a generic delegate argument to a method in .NET 2.0

我有一等代表声明。

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

然而,我不想在“方法”呼吁中明确划分Getter代表。 为什么我不能宣布参数如下......

... (ByVal g As Getter(Of TResult))

是否有办法这样做?

我的最终目标是能够在所谓的类别中为财产设定者和获得者设立一个代表。 但我的理解是,你可以这样做。 因此,我在该类别中提出了一套老练的方法,然后,我希望呼吁的阶层提出代表论点,然后援引。 是否有这样做的最佳做法。

我在上述例子中认识到,我可以确定一个叫人类别中的代表变量......但我试图建立一个有紧cap的单一州。

为记录起见,我不能使用在net35中宣布的新代表类别。

C#的答复值得欢迎。

任何想法?

牙齿

最佳回答

3. 您需要在方法上添加一个通用的参数:

Public Shared Sub MyMethod(Of TResult) (ByVal g As Getter(Of TResult))

在C#,这将

public static void MyMethod<TResult>(Getter<TResult> g) {
问题回答

2. 您可采用一种通用代表制方法:

Public Shared Sub MyMethod(Of TResult)(ByVal g As Getter(Of TResult))
     do stuff
End Sub




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签