2011-04-27 92 views
0

想我也一個自定義類(任何類),其方法和屬性:VisualBasicValue <T>:訪問自定義類及其方法性能

public class Test 
{ 
    public string MyString { get; set; } 
    public bool MyBool { get; set; } 

    public override string ToString() 
    { 
     return "Test Class : " + this.MyString + " - " + MyBool; 
    } 
} 

現在我想移動和處理其WF4之間的性能活動使用VisualBasicValue<T>。例如:

public class Program 
{ 
    static void Main(string[] args) 
    { 

     Test testClass = new Test() 
     { 
      MyString = "some string", 
      MyBool = true 
     }; 

     Sequence wf = new Sequence() 
     { 
      Variables = 
      { 
       new Variable<Test>("varName", testClass), 
      }, 

      Activities = 
      { 
       new WriteLine() { Text = new VisualBasicValue<string>("\"Test Class Properties: \" & varName.MyString & \"-\" & varName.MyBool") }, 
       new WriteLine() { Text = new VisualBasicValue<string>("\"Test Class ToString(): \" & varName") } 
      } 
     }; 

     WorkflowInvoker.Invoke(wf); 

     Console.ReadKey(); 
    } 
} 

此代碼編譯沒有問題。變量可以處理任何種類的類,但在運行時似乎會抱怨自定義類的用法。有些例外,如:

The following errors were encountered while processing the workflow tree: 
'Literal<Test>': Literal only supports value types and the immutable type System.String. The type WorkflowConsoleApplication3.Test cannot be used as a literal. 
'VisualBasicValue<String>': Compiler error(s) encountered processing expression ""Test Class ToString(): " & varName". 

操作「&」不是類型「字符串」和「WorkflowConsoleApplication3.Test」定義。

我讀過,你可以沿着這條線做一些事情:

VisualBasicSettings vbSettings = new VisualBasicSettings(); 
vbSettings.ImportReferences.Add(new VisualBasicImportReference() 
{ 
    Assembly = typeof(Test).Assembly.GetName().Name, 
    Import = typeof(Test).Namespace 
}); 

// construct workflow 

VisualBasic.SetSettings(wf, vbSettings); 

WorkflowInvoker.Invoke(wf); 

但是,這並不似乎這樣的伎倆。任何幫助?

PS:在同一主題,有人可以給我一個小示例如何\在哪裏使用VisualBasicReference<T>' with OutArgument`?我似乎可以在稍後階段使用它,但我可以找到任何示例。

回答

1

我做了一些更改以使您的代碼正常工作。

  1. 的變量構造函數 改變爲在 表達

校正代碼如下

明確使用ActivityFunc 過載
  • 第二的WriteLine需要 調用的ToString()
    private static void Main(string[] args) 
    { 
        var testClass = new Test { MyString = "some string", MyBool = true }; 
        var wf = new Sequence 
        { 
         Variables = { 
             // Changed to use ActivityFunc so testClass is not interpreted as a literal 
             new Variable<Test>("varName", ctx => testClass), 
            }, 
         Activities = 
          { 
           new WriteLine 
            { 
             Text = 
              new VisualBasicValue<string>(
              "\"Test Class Properties: \" & varName.MyString & \"-\" & varName.MyBool") 
            }, 
            // Changed to call ToString explicitly 
            new WriteLine { Text = new VisualBasicValue<string>("\"Test Class ToString(): \" & varName.ToString()") } 
          } 
        }; 
        var settings = new VisualBasicSettings(); 
        settings.ImportReferences.Add(
         new VisualBasicImportReference 
          { 
           Assembly = typeof(Test).Assembly.GetName().Name, Import = typeof(Test).Namespace 
          }); 
    
        // construct workflow 
        VisualBasic.SetSettings(wf, settings); 
        WorkflowInvoker.Invoke(wf); 
        Console.ReadKey(); 
    } 
    

    還有一件事。有人質疑爲什麼有必要使用VB Concat操作符顯式調用Test.ToString()。這是一個奇怪的問題,它是C#中聲明的類型與VB中聲明的類型不同的地方之一。

    C#使用+運算符進行加法和連接,其中VB具有用於concat的&運算符和特定的IL指令op_Concat。

    如果您在VB中聲明您的類型,則可以重載&運算符以消除在表達式中調用ToString()的需要。

    例如

    Public Class Test 
        Public Property MyString As String 
        Public Property MyBool As Boolean 
    
        Public Overrides Function ToString() As String 
         Return "Test Class : " & MyString + " - " & MyBool 
        End Function 
    
        Public Shared Operator &(ByVal left As String, ByVal right As Test) As String 
         Return left & "-" & right.ToString 
        End Operator 
    End Class 
    

    上的問題,像在VB中我往往只是創建VB控制檯應用程序從工作流程

    除了測試的東西出來工作時
    Module Module1 
    
        Dim varName As New Test With {.MyBool = True, .MyString = "some string"} 
    
        Sub Main() 
         Console.WriteLine("Test Class Properties: " & varName.MyString & "-" & varName.MyBool) 
         Console.WriteLine("Test Class ToString(): " & varName) 
         Console.ReadKey() 
        End Sub 
    
    End Module 
    

    的IL爲這個應用程序發出的顯示操作

    IL_002f: ldstr  "Test Class ToString(): " 
    IL_0034: ldsfld  class VBTest.Test VBTest.Module1::varName 
    IL_0039: call  string VBTest.Test::op_Concatenate(string, class VBTest.Test) 
    IL_003e: call  void [mscorlib]System.Console::WriteLine(string) 
    
  • +0

    是的!這正是問題所在。謝謝。 – Joao 2011-04-28 18:51:34

    0

    以下代碼有效。請注意我用lambda代替固定值初始化變量的方式,第二個VB表達式使用+而不是&。最後一個看起來像是一個bug,我將繼續跟進。

    static void Main() 
    { 
        Test testClass = new Test() 
        { 
         MyString = "some string", 
         MyBool = true 
        }; 
    
        Sequence wf = new Sequence() 
        { 
         Variables = 
         { 
          new Variable<Test>("varName", c => testClass), 
         }, 
    
         Activities = 
         { 
          new WriteLine() { Text = new VisualBasicValue<string>("\"Test Class Properties: \" & varName.MyString & \"-\" & varName.MyBool") }, 
          new WriteLine() { Text = new VisualBasicValue<string>("\"Test Class ToString(): \" + varName") } 
         } 
        }; 
    
        var vbSettings = new VisualBasicSettings(); 
        vbSettings.ImportReferences.Add(new VisualBasicImportReference() 
        { 
         Assembly = typeof(Test).Assembly.GetName().Name, 
         Import = typeof(Test).Namespace 
        }); 
    
    
        VisualBasic.SetSettings(wf, vbSettings); 
        WorkflowInvoker.Invoke(wf); 
    
        Console.ReadKey(); 
    } 
    

    我必須對Test類做一些小改動,爲字符串連接添加一個+運算符。 public class Test { public string MyString {get;組; } public bool MyBool {get;組; }

    public override string ToString() 
        { 
         return "Test Class : " + this.MyString + " - " + MyBool; 
        } 
    
        public static string operator +(string s, Test t) 
        { 
         return s + t.ToString(); 
        } 
    } 
    
    +0

    我已經在發佈問題後不久糾正了錯誤,但問題他們保持。這很奇怪,這似乎是正確的用法。你知道一些關於'VisualBasicSettings'和'VisualBasicImportReference'的東西。我敢打賭,這是問題所在。 – Joao 2011-04-27 19:09:55

    +0

    他們談論它[這裏](http://social.msdn.microsoft.com/Forums/en-US/wfprerelease/thread/2b77771e-84a6-4ec3-a944-3de2a60201fc) – Joao 2011-04-27 19:11:19

    +0

    你得到什麼錯誤信息和什麼是你正在使用的當前VB表達式? – Maurice 2011-04-27 19:31:08