如何将函数作为参数提供给在VB.NET中获取Object的构造函数?

时间:2021-09-02 00:30:50

I have a class in C# that i need to reimplement in VB. Only the constructor is relevant here, it looks like this:

我在C#中有一个类需要在VB中重新实现。这里只有构造函数相关,它看起来像这样:

public RelayCommand(Action<object> execute) : this(execute, DefaultCanExecute) { }
public ActionCommand(Action<object> exec, Predicate<object> canExec)
{
    //stuff...
}

This version works. Well, my VB.NET version does not. It looks like this:

这个版本有效。好吧,我的VB.NET版本没有。它看起来像这样:

Public Sub New(ByVal execute As Object)
    Me.New(execute, AddressOf DefaultCanExecute)
End Sub

Public Sub New(ByVal execute As Action(Of Object), ByVal canExec As Predicate(Of Object))
    'stuff...
End Sub

DefaultCanExecute is a private boolean function with no params always returning true.

DefaultCanExecute是一个私有布尔函数,没有params总是返回true。

When I try this in C#, it works:

当我在C#中尝试这个时,它可以工作:

var Foo = new ActionCommand(Bar);

In VB, the following fails:

在VB中,以下失败:

Dim Foo As VariantType = New RelayCommand(Bar)

The Sub in VB looks like that:

VB中的Sub看起来像这样:

Private Sub Bar(ByVal obj As Object)
    'stuff...
End Sub

And the C# version for the sake of completeness:

和C#版本为了完整性:

private void Bar(object obj)
{
    //stuff...
}

Does someone have an idea why the VB version does not work? Thank you :)

有人知道为什么VB版本不起作用?谢谢 :)

2 个解决方案

#1


This works fine for me in the VB.NET

这在VB.NET中对我来说很好

Public Sub New(ByVal execute As Action(Of Object))
    MyClass.New(execute, AddressOf DefaultCanExecute)
End Sub

Public Sub New(ByVal execute As Action(Of Object), 
               ByVal canExec As Predicate(Of Object))
    'stuff...
End Sub

Then using like this:

然后像这样使用:

Private Sub Bar(ByVal obj As Object)
    'stuff...
End Sub

Dim Foo As New RelayCommand(AddressOf Bar)

If you want pass function(pointer to function) as parameter use AddressOf keyword

如果要将pass函数(指向函数的指针)作为参数使用AddressOf关键字

#2


VariantType is an Enum and you cannot cast your RelayCommand class to an Enum.

VariantType是一个枚举,您不能将您的RelayCommand类强制转换为枚举。

To make the VB version equivalent to the C# change

使VB版本等同于C#更改

Dim Foo As VariantType = New RelayCommand(Bar)

Dim Foo As VariantType = New RelayCommand(Bar)

to

Dim Foo = New RelayCommand(Bar)

Dim Foo = New RelayCommand(Bar)

#1


This works fine for me in the VB.NET

这在VB.NET中对我来说很好

Public Sub New(ByVal execute As Action(Of Object))
    MyClass.New(execute, AddressOf DefaultCanExecute)
End Sub

Public Sub New(ByVal execute As Action(Of Object), 
               ByVal canExec As Predicate(Of Object))
    'stuff...
End Sub

Then using like this:

然后像这样使用:

Private Sub Bar(ByVal obj As Object)
    'stuff...
End Sub

Dim Foo As New RelayCommand(AddressOf Bar)

If you want pass function(pointer to function) as parameter use AddressOf keyword

如果要将pass函数(指向函数的指针)作为参数使用AddressOf关键字

#2


VariantType is an Enum and you cannot cast your RelayCommand class to an Enum.

VariantType是一个枚举,您不能将您的RelayCommand类强制转换为枚举。

To make the VB version equivalent to the C# change

使VB版本等同于C#更改

Dim Foo As VariantType = New RelayCommand(Bar)

Dim Foo As VariantType = New RelayCommand(Bar)

to

Dim Foo = New RelayCommand(Bar)

Dim Foo = New RelayCommand(Bar)