使用“Dim”的VB.NET和类型推断

时间:2021-01-31 17:03:39

I'm coming from a C# background and I really like the type inference that C# 3.0 has. I'm trying to do similar things in VB.NET (some of which appear possible), but in some cases the compiler seems to be not nearly as good at inferring the type.

我来自C#背景,我非常喜欢C#3.0的类型推断。我试图在VB.NET中做类似的事情(其中一些似乎是可能的),但在某些情况下,编译器似乎在推断类型方面似乎不太好。

For example, I have a method that returns an object of type System.Guid. In C# I'd do this and the variable 'prop' would be of type Guid through inference.

例如,我有一个返回System.Guid类型的对象的方法。在C#中我会这样做,变量'prop'将通过推理变为Guid类型。

var prop = RegisterProperty<Guid>(...);

However, if I do a similar thing in VB.NET:

但是,如果我在VB.NET中做类似的事情:

Dim prop = RegisterProperty(Of Guid(...)

I get prop as type System.Object. I've played with some of the VB.NET project settings but the only thing it changes is whether I get a warning that the object is of type Object when I use it later as a Guid.

我得到支持类型System.Object。我已经玩了一些VB.NET项目设置,但它唯一改变的是我在以后作为Guid使用它时是否收到对象类型为Object的警告。

Any ideas? I'm thinking the use of generics should allow the compiler to tell beyond a doubt what type prop should be.

有任何想法吗?我认为使用泛型应该允许编译器毫无疑问地告诉我应该使用什么类型的prop。


@J Cooper: ok, I did have that setting turned on, but I just re-read the documentation for that compiler option and it reads "Specifies whether to allow local type inference in variable declarations". I believe the reason it's not working for me is that I'm declaring static fields in my class. I guess even though they are initialized when declared, the compiler doesn't support type inference at that point. Bummer.

@J Cooper:好的,我确实打开了那个设置,但我只是重新阅读了该编译器选项的文档,并且它显示“指定是否允许在变量声明中进行本地类型推断”。我相信它不适合我的原因是我在课堂上宣布静态字段。我猜即使它们在声明时被初始化,编译器也不支持那时的类型推断。游民。

3 个解决方案

#1


1  

You mention below that you're trying to do this in a static variable declaration. That won't work in C#, and will give you Object in VB.NET (for instance and statics).

您在下面提到您正在尝试在静态变量声明中执行此操作。这在C#中不起作用,并且会在VB.NET中为您提供Object(例如静态)。

#2


2  

I'm assuming by "played with the VB.NET project settings" you mean you already did this: Type Inference in VB.NET

我假设“使用VB.NET项目设置”,你的意思是你已经这样做了:在VB.NET中输入类型推断

If not, may help

如果没有,可能会有帮助

#3


1  

If VB is infering the type as Object, you need to turn Option Strict On at the top of your class file at least. Once you do that, you should see that class fields do not support type inferencing. The following raises the compiler error "Option Strict On requires all variable declarations to have an As clause".

如果VB将类型推断为Object,则至少需要在类文件的顶部打开Option Strict On。执行此操作后,您应该看到类字段不支持类型推断。以下引发了编译器错误“Option Strict On要求所有变量声明都有一个As子句”。

Public Shared foo = "Test"

In general, you should use Dim inside of methods and Public/Private/Friend on the class/module level fields.

通常,您应该在方法和类/模块级别字段中使用方法中的Dim和Public / Private / Friend。

#1


1  

You mention below that you're trying to do this in a static variable declaration. That won't work in C#, and will give you Object in VB.NET (for instance and statics).

您在下面提到您正在尝试在静态变量声明中执行此操作。这在C#中不起作用,并且会在VB.NET中为您提供Object(例如静态)。

#2


2  

I'm assuming by "played with the VB.NET project settings" you mean you already did this: Type Inference in VB.NET

我假设“使用VB.NET项目设置”,你的意思是你已经这样做了:在VB.NET中输入类型推断

If not, may help

如果没有,可能会有帮助

#3


1  

If VB is infering the type as Object, you need to turn Option Strict On at the top of your class file at least. Once you do that, you should see that class fields do not support type inferencing. The following raises the compiler error "Option Strict On requires all variable declarations to have an As clause".

如果VB将类型推断为Object,则至少需要在类文件的顶部打开Option Strict On。执行此操作后,您应该看到类字段不支持类型推断。以下引发了编译器错误“Option Strict On要求所有变量声明都有一个As子句”。

Public Shared foo = "Test"

In general, you should use Dim inside of methods and Public/Private/Friend on the class/module level fields.

通常,您应该在方法和类/模块级别字段中使用方法中的Dim和Public / Private / Friend。