VB中的.NET 4.0框架动态特征与选项严格打开? [英] .NET 4.0 framework dynamic features in VB with Option Strict On?

查看:174
本文介绍了VB中的.NET 4.0框架动态特征与选项严格打开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在VB框架中使用新的动态功能,如 ExpandoObject 而不设置 Option Strict Off ?使用C#,您将使用您特别声明为 dynamic 的变量,输入仅安全 。但是使用VB,我发现使用这些功能的唯一方法是从一开始就在VB.NET中使用的旧的 Option Strict Off 技巧。没有选项严格,文件中的所有内容都会受到模糊类型的污染,如下所示:

 选项显示
选项严格关闭
选项Infer On

部分公共类ClassX

公共子TestDynamic()
Dim dyn As Object = New System.Dynamic.ExpandoObject()
Dim a As String = 1'Ew!
Dim obj As Object =999

dyn.Str = a''#a是一个字符串,记得吗?即使它有一个数字
''#dyn.Str = 1:Type = System.String
Console.WriteLine(dyn.Str = {0}:Type = {1},dyn。 Str,dyn.Str.GetType()。ToString())

dyn.Num = 123
''#dyn.Num = 123:Type = System.Int32
控制台.WriteLine(dyn.Num = {0}:Type = {1},dyn.Num,dyn.Num.GetType()。ToString())

dyn.Dbl = obj / 9
''#dyn.Dbl = 111:Type = System.Double
Console.WriteLine(dyn.Dbl = {0}:Type = {1},dyn.Dbl,dyn.Dbl。 GetType()。ToString())

dyn.Obj = obj
''#dyn.Obj = 999:Type = System.String
Console.WriteLine(dyn。 Obj = {0}:Type = {1},dyn.Obj,dyn.Obj.GetType()。ToString())

dyn.Dte =#5/5/1955#
''#dyn.Dte = 7/7/1977 12:00:00 AM:Type = System.DateTime
Console.WriteLine(dyn.Dte = {0}:Type = {1}, dyn.Dte,dyn.Dte.GetType()。ToString())

AmICalled(dyn.Num)
AmIC alled(dyn.Obj)
AmICalled(dyn.Str)
AmICalled(dyn.Dbl)

尝试
AmICalled(dyn.Dte)
Catch
Console.WriteLine(日期不转换为int我猜...但我们不知道'直到运行时')
结束尝试

Console.WriteLine (dyn.Num + dyn.Str)'124 !?
Console.WriteLine(dyn.Num& dyn.Str)'1231 !?

End Sub

私有子消息(ByVal i As Integer)
Console.WriteLine(Amicalled被调用:& i)
End Sub

结束类

这是否正确?而如果是这样,最好的办法还是使用 ExpandoObject 这样的东西,并减轻丢失所有类型安全的风险?部分班?或者在这种情况下我不应该担心类型安全吗?

解决方案

看来你不能不必转选项严格关闭
我会研究一些。






修改






经过 ExpandoObject 上的一些文档,它似乎被用于COM和Office Interop的C#。传统上,在VB.NET中, Object 被用于此目的,这将需要您关闭Option Strict。



要回答您的问题,这意味着您可以使用 Object 类型而不是 ExpandoObject [如果VB.NET中存在这种类型],请设置选项继承选项严格打开或关闭

您还可以考虑使用部分类将非选项严格代码本地化为特定文件。







建议阅读




Is there any way to use the new dynamic features in the 4.0 framework like ExpandoObject in VB.NET without setting Option Strict Off? With C#, you lose type safety only with the variables you specifically declare as dynamic. But with VB, the only way I've found to use these features is with the old Option Strict Off trick that's been in VB.NET since the beginning. Without Option Strict, everything in the file is polluted with fuzzy typing like so:

Option Explicit On
Option Strict Off
Option Infer On

Partial Public Class ClassX

   Public Sub TestDynamic()
      Dim dyn As Object = New System.Dynamic.ExpandoObject()
      Dim a As String = 1 ''# Ew!
      Dim obj As Object = "999"

      dyn.Str = a   ''# a is a string, remember?  Even though it has a number
      ''# dyn.Str = 1 : Type = System.String
      Console.WriteLine("dyn.Str = {0} : Type = {1}", dyn.Str, dyn.Str.GetType().ToString())

      dyn.Num = 123
      ''# dyn.Num = 123 : Type = System.Int32
      Console.WriteLine("dyn.Num = {0} : Type = {1}", dyn.Num, dyn.Num.GetType().ToString())

      dyn.Dbl = obj / 9
      ''# dyn.Dbl = 111 : Type = System.Double
      Console.WriteLine("dyn.Dbl = {0} : Type = {1}", dyn.Dbl, dyn.Dbl.GetType().ToString())

      dyn.Obj = obj
      ''# dyn.Obj = 999 : Type = System.String
      Console.WriteLine("dyn.Obj = {0} : Type = {1}", dyn.Obj, dyn.Obj.GetType().ToString())

      dyn.Dte = #5/5/1955#
      ''# dyn.Dte = 7/7/1977 12:00:00 AM : Type = System.DateTime
      Console.WriteLine("dyn.Dte = {0} : Type = {1}", dyn.Dte, dyn.Dte.GetType().ToString())

      AmICalled(dyn.Num)
      AmICalled(dyn.Obj)
      AmICalled(dyn.Str)
      AmICalled(dyn.Dbl)

      Try
         AmICalled(dyn.Dte)
      Catch
         Console.WriteLine("Dates don't convert to ints I guess... but we don't know that 'till runtime")
      End Try

      Console.WriteLine(dyn.Num + dyn.Str) ' 124!?
      Console.WriteLine(dyn.Num & dyn.Str) ' 1231!?

   End Sub

   Private Sub AmICalled(ByVal i As Integer)
      Console.WriteLine("AmICalled was called with: " & i)
   End Sub

End Class

Is this really correct? And, if so, what's the best way to still use things like ExpandoObject and mitigate the risk of losing all type safety? Partial classes? Or should I just not be so worried about type safety in this case?

解决方案

It appears you can't without having to turn Option Strict off. I'll research some more though.


Edit


After going through some documentation on the ExpandoObject, it appears it is used in C# for COM and Office Interop. Traditionally, in VB.NET, the Object was used for such purposes and that would require you turn off Option Strict.

To answer your question this means that you can use dynamic types in VB.NET by using the Object type instead of the ExpandoObject [if such a type exists in VB.NET], set Option Infer On and Option Strict On or Off.
You could also consider using partial classes to localise your non Option Strict code to particular files.



Suggested Reading

这篇关于VB中的.NET 4.0框架动态特征与选项严格打开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆