当前版本的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有关,问题本身也是通用的。我也试过使用命名参数,但这也无济于事。关于如何解决这个问题的任何提示?
答案 0 :(得分:1)
Protected Function [Set](Of T)(ByRef field As T, newValue As T, <CallerMemberName> Optional propertyName As String = Nothing) As Boolean
此重载的第三个参数是可选的,如果您在调用中跳过它,它将使用CallerMemberName
为其赋值。由于Set
几乎总是从属性内部调用,因此这种方法应该可以很好地工作。没有其他重载需要两个参数,因此编译器可以正确解析它。