VisualBasicValue<T>:访问自定义类及其方法\属性 [英] VisualBasicValue<T>: Access custom classes and its methods\properties

查看:22
本文介绍了VisualBasicValue<T>:访问自定义类及其方法\属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个自定义类(任何类)及其方法和属性:

Suppose I've a custom class (any class), with its methods and properties:

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

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

现在我想使用 VisualBasicValue 在 WF4 活动之间移动和处理它的属性.例如:

Now I want to move and handle its properties between WF4 activities using VisualBasicValue<T>. For example:

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();
    }
}

这段代码编译没有问题.变量可以处理任何类型的类,但在运行时它似乎抱怨自定义类的使用.一些例外,如:

This code compiles without a problem. Variable can handle any kind of class, but while running it seems to complain of the custom class usage. Some exception like:

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".

运算符&"没有为String"和WorkflowConsoleApplication3.Test"类型定义.

Operator '&' is not defined for types 'String' and 'WorkflowConsoleApplication3.Test'.

我读到您可以按照以下方式做一些事情:

I've read that you can do something along this lines:

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);

但这似乎不起作用.有什么帮助吗?

But that doesn't seems to do the trick. Any help?

PS:在同一主题上,有人能给我一个小例子如何\在哪里使用 VisualBasicReference'与OutArgument`?这似乎是我可以在稍后阶段使用的东西,但我会找到任何类型的例子.

PS: At the same topic, can someone give me a little example how\where to use VisualBasicReference<T>' withOutArgument`? It seems something I can use at a later stage but I'm to find any kind of example.

推荐答案

我进行了一些更改以使您的代码正常工作.

I made a couple of changes to make your code work.

  1. 变量构造函数是改为使用 ActivityFunc超载
  2. 第二个 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 中声明的类型不同的地方之一.

One more thing. Some have questioned why it was necessary to call Test.ToString() explicitly with the VB Concat operator. This is a curious issue and it is one of the places where a type declared in C# differs from a type declared in VB.

C# 使用 + 运算符进行加法和连接,其中 VB 具有 &concat 的运算符和特定的 IL 指令 op_Concat.

C# uses the + operator for both addition and concatenation where VB has the & operator for concat and a specific IL instruction op_Concat.

如果你在 VB 中声明你的类型,你可以重载 &运算符以消除在表达式中调用 ToString() 的需要.

If you declare your type in VB, you can overload the & operator to eliminate the need to call ToString() in your expression.

例如

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 控制台应用程序来测试工作流之外的事情

When working on problems like in VB I often just create VB console apps to test things out apart from Workflow

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 显示了操作员

The IL emitted for this app shows the operator

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)

这篇关于VisualBasicValue&lt;T&gt;:访问自定义类及其方法\属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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