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

前端 未结 3 1172
慢半拍i
慢半拍i 2020-12-11 15:06

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

3条回答
  •  既然无缘
    2020-12-11 15:42

    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.

提交回复
热议问题