代理和ParamArray - 解决方法建议? [英] Delegates and ParamArray - Workaround Suggestions?

查看:120
本文介绍了代理和ParamArray - 解决方法建议?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

某些预定义方法包含 ParamArray 在他们的签名。
然而,代理人不能在其签名中包含ParamArray。

Some predefined methods contain a ParamArray in their signature. Delegates, however, cannot contain a ParamArray in their signature.

问题:假设您希望为需要ParamArray的特定方法创建委托机制。你如何解决这个约束?

Question: Assume you wish to create a delegation mechanism for a specific method which requires a ParamArray. How would you work around this constraint?

编辑:只是为了表明,假设你不能改变方法签名本身(预定义的方法,由一些第三方定义,无论是微软还是非微软)。

just to make clear, assume you cannot change the method signatures themselves (pre-defined methods, defined by some 3rd party, be it Microsoft or not).

EDIT2:这里的真正交易是保持语法糖,因为以下代码可以正常工作,但消除糖:

The real deal here is keeping the syntax sugar, because the following code does work, but eliminates the sugar:

Public Delegate Sub MyDelegate(ByVal myArgs() As Object)

Public Sub PredefinedSub(ByVal ParamArray myArgs() As Object)
    '...'
End Sub

Sub Test()
    Dim aDelegate As New MyDelegate(AddressOf PredefinedSub)
    aDelegate.Invoke(New Object() {1, 2, 3, 4})
End Sub

EDIT3:事实证明,Skeet的解决方案也适用于创建包含ParamArray的事件和操作符。已添加到我的博客

It turns out that Skeet's solutions is applicable also for creating Events and Operators containing a ParamArray. Added to my blog.

推荐答案

嗯...它在C#中有效:

Hmm... it works in C#:

using System;

class Test
{
    delegate void Foo(params string[] args);

    static void Main()
    {
        Foo f = x => Console.WriteLine(x.Length);

        f("a", "b", "c");
    }
}

但是,你是对的 - 等效的委托声明在VB中失败:

However, you're right - the equivalent delegate declaration in VB fails:

Delegate Sub Foo(ParamArray ByVal args() As String)

提供:


错误BC33009:委托参数不能被声明为ParamArray。

error BC33009: 'Delegate' parameters cannot be declared 'ParamArray'.

好奇。幸运的是,有一种方法:

Curious. Fortunately, there's a way round it:

Imports System

Public Class Test

    Delegate Sub Foo(<[ParamArray]()> ByVal args() As String)

    Public Shared Sub Main()
        Dim f As Foo = AddressOf PrintLength
        f("a", "b", "c")
    End Sub

    Private Shared Sub PrintLength(ByVal x() As String)
        Console.WriteLine(x.Length)
    End Sub

End Class

基本上我我刚刚应用了 ParamArrayAttribute 手动似乎工作正常。

Basically I've just applied ParamArrayAttribute manually. Seems to work fine.

但是,这些都不会阻止您使用现有的 ParamArray 方法。这些方法非常有能力使用正常的数组 - 所以你可以将你的委托类型声明为正常,并且仍然创建了委托实例,它们根本没有提到这些方法。代理类型仅影响您可以调用委托的方式。

However, none of this would have stopped you from using existing ParamArray methods anyway. Those methods are quite capable of taking normal arrays - so you could have declared your delegate types as normal and still created delegate instances which referred to those methods with no problems at all. The delegate type only affects how you would be able to call the delegate.

除了使用参数数组声明委托类型之外,我看不到有什么问题是。

Other than declaring a delegate type with a parameter array, I don't really see what the issue was.

这篇关于代理和ParamArray - 解决方法建议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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