在VB .NET 4.0框架的动态特性与期权严格的? [英] .NET 4.0 framework dynamic features in VB with Option Strict On?

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

问题描述

有什么办法没有设置选项严格关 ExpandoObject 在VB.NET的新的动态特性C $ C>?在C#中,你失去的类型安全的的变量,你特别声明为动态。但用VB,我发现使用这些功能的唯一方法就是与旧选项严格关技巧,因为开头一直在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

这真的是正确的吗?而且,如果是的话,什么是仍然使用的东西,像 ExpandoObject 和减轻失去所有类型安全的风险的最佳方法是什么?部分类?或者我应该只是不会那么担心在这种情况下,类型安全?

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.

修改

在经历了一些文件的 ExpandoObject ,看来它是用在C#中的COM和Office互操作。传统上,在VB.NET中,对象用于这样的目的,这将需要您关闭选项严格。

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.

要回答你的问题,这意味着你可以使用动态类型VB.NET中使用对象键入而不是 ExpandoObject [如果这种类型的VB.NET中存在],设置选项推断在选项严格开或关
你也可以考虑使用部分类本地化你非选项严格code到特定的文件。



推荐阅读

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

  • <一个href="http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/b9ee3471-0b34-4783-b243-673bebc9bb9d"相对=nofollow>动态类型在C#中,相当于在VB
  • 使用动态类(C#编程指南)
  • Dynamic Type in C#, Equivalent in VB
  • Using Type Dynamic (C# Programming Guide)

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

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