VB.NET无法区分重载函数

时间:2016-04-19 16:22:40

标签: vb.net mvvm overloading mvvm-light

当前版本的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]'对这些参数最具体:

     

'受保护的重载函数[Set](字符串)(propertyName As String,ByRef field as String,newValue As String)As Boolean':不是最具体的。

     

'受保护的重载函数[Set](字符串)(ByRef字段为String,newValue为String,[propertyName As String = Nothing])As Boolean':不是最具体的。

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

1 个答案:

答案 0 :(得分:1)

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

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

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