.NET 4.0 framework dynamic features in VB with Option Strict On?

孤街浪徒 提交于 2019-11-28 13:18:12

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

No. This is sort of the modern day late-binding.

I haven't tried this, and it wouldn't be pretty, but you ought to be able to use CallByName.

Adapting your example

Partial Public Class ClassX  

   Public Sub TestDynamic()  
      Dim dyn As Object = New System.Dynamic.ExpandoObject()  
      Dim a As String = "1" ''# Option Strict is on  
      Dim obj As Object = "999"  

      ''# dyn.Str = a  
      CallByName(dyn, "Str", CallType.Set, a) 
      Console.WriteLine("dyn.Str = {0} : Type = {1}", 
        CallByName(dyn, "Str", CallType.Get, a), 
        CallByName(dyn, "Str", CallType.Get, a).GetType().ToString()
      )     

      ''# etc etc... I can't face any more of that  

As I said, it's not pretty.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!