How do I write [DefaultValue(null)] in VB.NET?

2019-06-22 10:02发布

问题:

This isn't as trivial as it appears to be. It's a follow-up to this question.

Let's say I have a Windows Forms user control with a property:

// using System.ComponentModel;

[DefaultValue(null)]
public object DataSource { … }

If I translate this to VB.NET, I would try this:

'Imports System.ComponentModel

<DefaultValue(Nothing)>
Public Property DataSource As Object
    …
End Property

This won't work because the compiler has problems choosing the correct overload of DefaultValueAttribute's constructor:

Overload resolution failed because no accessible New is most specific for these arguments:

  • Public Sub New(value As Boolean): Not most specific.
  • Public Sub New(value As Byte): Not most specific.
  • Public Sub New(value As Char): Not most specific.

I am quite certain this is because Nothing in VB.NET not only means "the null reference" (null in C#), but also "the default value for" the parameter type (default(T) in C#). Because of this ambiguity, every constructor overload is a potential match.

<DefaultValue(CObj(Nothing))> also won't work because it is not a constant value.

How exactly do I write this in VB.NET?

P.S.: <DefaultValue(GetType(Object), Nothing)> is an option, but it circumvents the problem. I'm really interested if there's any way to use the same constructor for DefaultValueAttribute as the C# version does.

回答1:

"[…] won't work because the compiler has problems choosing the correct overload of DefaultValueAttribute's constructor[.]"

The compiler can be assisted with a type-cast of Nothing to the desired type:

<DefaultValue(DirectCast(Nothing, Object))>

"<DefaultValue(CObj(Nothing))> also won't work because it is not a constant value."

Fortunately, unlike CObj(Nothing), the compiler considers DirectCast(Nothing, Object) — and suprisingly, CType(Nothing, Object), too — a constant value, so it is accepted.