VB.NET无法区分重载函数

问题描述 投票:2回答:1

当前版本的MVVM Light在Set类中有一个名为ObservableObject的辅助函数,继承的ViewModel类可以调用它来更改属性值并在一次调用中引发更改通知。与新的NameOf运算符一起,这使得属性的样板代码更小。

然而问题是Set函数超载并且超出3次重载,以下2次重载使VB.NET生气:

Protected Function [Set](Of T)(propertyName As String, ByRef field As T, newValue As T) As Boolean
Protected Function [Set](Of T)(ByRef field As T, newValue As T, <CallerMemberName> Optional propertyName As String = Nothing) As Boolean

现在,如果你有一个String类型属性,VB.NET无法区分我们调用的重载。

重载决策失败,因为没有可访问的'[Set]'对这些参数最具体:

'Protected Overloads Function [Set](Of String)(propertyName As String,ByRef field as String,newValue As String)As Boolean':不是最具体的。

'Protected Overloads Function [Set](Of String)(ByRef field As String,newValue As String,[propertyName As String = Nothing])As Boolean':不是最具体的。

请注意,通过使用ref关键字,C#可以轻松处理这种情况。此外,即使当前的情况与MVVM Light有关,问题本身也是通用的。我也试过使用命名参数,但这也无济于事。关于如何解决这个问题的任何提示?

vb.net mvvm overloading mvvm-light
1个回答
1
投票

差不多一年后再来一次。我刚刚发现了一些在大多数情况下都能正常工作的解决方法。而不是调用问题中提到的重载之一,使用第三个重载:

Protected Function [Set](Of T)(ByRef field As T, newValue As T, <CallerMemberName> Optional propertyName As String = Nothing) As Boolean

此重载的第三个参数是可选的,如果在调用中跳过它,它将使用CallerMemberName为其赋值。由于Set几乎总是从属性内部调用,这种方法应该很好地工作。没有其他重载需要两个参数,因此编译器可以正确解析它。

© www.soinside.com 2019 - 2024. All rights reserved.