【发布时间】:2015-04-20 13:04:26
【问题描述】:
这并不像看起来那么简单。这是this question的后续。
假设我有一个带有属性的 Windows 窗体用户控件:
// using System.ComponentModel;
[DefaultValue(null)]
public object DataSource { … }
如果我把它翻译成 VB.NET,我会试试这个:
'Imports System.ComponentModel
<DefaultValue(Nothing)>
Public Property DataSource As Object
…
End Property
这不起作用,因为编译器在选择正确的overload of DefaultValueAttribute's constructor 时遇到问题:
重载解析失败,因为没有可访问的
New最适合这些参数:
Public Sub New(value As Boolean):不是很具体。Public Sub New(value As Byte):不是很具体。Public Sub New(value As Char):不是很具体。
我很确定这是因为 VB.NET 中的Nothing 不仅意味着“空引用”(C# 中的null),而且还意味着参数类型的“默认值”(C# 中的default(T) )。由于这种模糊性,每个构造函数重载都是潜在的匹配项。
<DefaultValue(CObj(Nothing))> 也不起作用,因为它不是一个常数值。
究竟是如何做我在 VB.NET 中编写的?
P.S.: <DefaultValue(GetType(Object), Nothing)> 是一个选项,但它可以规避问题。如果有任何方法可以为DefaultValueAttribute 使用与 C# 版本相同的构造函数,我真的很感兴趣。
【问题讨论】:
标签: c# vb.net overload-resolution c#-to-vb.net