2011-04-27 4 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); 

하지만 그 트릭을 할 것 같다하지 않습니다. 어떤 도움이 필요합니까?

추신 : 동일한 주제에서 누군가 내가 \ 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_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

    0

    다음 코드가 작동합니다. 고정 값 대신 람다를 사용하여 변수를 초기화하는 방법에 유의하고 두 번째 VB 식은 & 대신 +를 사용합니다. 마지막 버그는 나에게 버그처럼 보입니다. 그리고 나는 그것에 대해 후속 조치를 취할 것입니다.

    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 { 공용 문자열 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

    나는 질문을 게시 한 직후 오류를 수정했지만 probl 남아있다. 이상한데, 올바른 사용법 인 것 같습니다. 'VisualBasicSettings'과'VisualBasicImportReference'에 대해서 알고 계십니까? 나는 그게 문제라고 내기하고있다. – Joao

    +0

    그들은 그것에 대해 이야기 [여기] (http://social.msdn.microsoft.com/Forums/en-US/wfprerelease/thread/2b77771e-84a6-4ec3-a944-3de2a60201fc) – Joao

    +0

    어떤 오류 메시지를 받고 무엇을 현재 사용하고있는 VB 표현식입니까? – Maurice