开发者

How do you do an assignment of a delegate to a delegate in .NET 2.0

I just go the answer on how to pass a generic delegate as a parameter. Thanks for the help. Now I need to know how to ASSIGN the delegate to another delegate declarartion. Can this be done?

Public Class MyClass  
    Public Delegate Function Getter(Of TResult)() As TResult    

    ''#the following code works.
    Public Shared Sub MyMethod(Of TResult)(ByVal g As Getter(Of TResult))
        ''# I want to assig开发者_如何转开发n Getter = g  ''#how do you do it.
    End Sub
End Class

Notice that Getter is now private. How can I ASSIGN Getter = G

When I try Getter = g 'I get too few type arguments compile error. When I try Getter(Of TResult) = g 'I get Getter is a type and cannot be used as an expression.

How do you do it?

Seth


I'm not quite sure what you're trying to do - in your code, Getter is a name of a type (a delegate type), not an instance field of the class that you could assign something to.

Did you intend to declare an event (that other users can registre handlers to)? If that's the case, see for example this article at MSDN.


You can't yet. Your Getter(Of TResult)() delegate is not a variable that you can assign things to. It's more like a class definition. So first you need to also declare a delegate variable you can use:

Public Class MyClass  
    Public Delegate Function Getter(Of TResult)() As TResult    

    Private MyGetter As Getter(Of TResult)

    Public Shared Sub MyMethod(Of TResult)(ByVal g As Getter(Of TResult))
        MyGetter = g  
    End Sub
End Class

Note that you still have an issue with the generic type, because the generic type you declared for MyMethod(Of TResult) is not connected to the type of the delegate variable I just added here. You have to make the entire class generic instead of the just the method for that to work.

I'm also wondering why you don't just use the existing Func(Of T) delegate?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜