gpt4 book ai didi

expression - WF。 NativeActivity 内部的 C# 表达式

转载 作者:行者123 更新时间:2023-12-03 22:46:17 24 4
gpt4 key购买 nike

我正在尝试在 NativeActivity 中使用自定义 C# 表达式

它可以很好地处理像 Condition = new CSharpValue("1 == 1") 这样的简单表达式

它不适用于这样的表达式 Condition = new CSharpValue("Address == null")

由于表达式编译错误“名称‘xxxxx’在当前上下文中不存在”,我无法在表达式中引用事件的变量或 InArgument

工作代码

var act = new ExecuteIfTrue
{
Condition = new CSharpValue<Boolean>("1 == 1"),
Address = new InArgument<MailAddress>(ctx => new MailAddress { DisplayName = "TestDisplayName" }),
Body = new WriteLine { Text = "Rest!" }
};

CompileCSharpExpressions<MailAddress>(act);
WorkflowInvoker.Invoke(act);

Non-Working code(指NativeActivity的InArgument)

var act = new ExecuteIfTrue
{
Condition = new CSharpValue<Boolean>("Address.Email == null"),
//Condition = new CSharpValue<Boolean>("MailAddressVar == null"),
Address = new InArgument<MailAddress>(ctx => new MailAddress { DisplayName = "TestDisplayName" }),
Body = new WriteLine { Text = "Rest!" }
};

CompileCSharpExpressions<MailAddress>(act);
WorkflowInvoker.Invoke(act);

NativeActivity

public class ExecuteIfTrue : NativeActivity
{
[RequiredArgument]
public InArgument<bool> Condition { get; set; }

[RequiredArgument]
public InArgument<MailAddress> Address { get; set; }

public Variable<MailAddress> MailAddressVar;

public Activity Body { get; set; }

public ExecuteIfTrue()
{
MailAddressVar = new Variable<MailAddress> { Default = null };
}

protected override void Execute(NativeActivityContext context)
{
if (context.GetValue(this.Condition) && this.Body != null)
context.ScheduleActivity(this.Body);
}

protected override void CacheMetadata(NativeActivityMetadata metadata)
{
metadata.AddImplementationVariable(MailAddressVar);
base.CacheMetadata(metadata);
}
}


public class MailAddress
{
public String Email { get; set; }
public String DisplayName { get; set; }
}

辅助方法

public static void CompileCSharpExpressions<T>(Activity activity)
{
var impl = new AttachableMemberIdentifier(typeof(TextExpression), "NamespacesForImplementation");
var namespaces = new List<string> { typeof(T).Namespace };
TextExpression.SetReferencesForImplementation(activity, new AssemblyReference { Assembly = typeof(T).Assembly });
AttachablePropertyServices.SetProperty(activity, impl, namespaces);

var activityName = activity.GetType().ToString();
var activityType = activityName.Split('.').Last() + "_CompiledExpressionRoot";
var activityNamespace = string.Join(".", activityName.Split('.').Reverse().Skip(1).Reverse());

var settings = new TextExpressionCompilerSettings
{
Activity = activity,
Language = "C#",
ActivityName = activityType,
ActivityNamespace = activityNamespace,
RootNamespace = null,
GenerateAsPartialClass = false,
AlwaysGenerateSource = true,
ForImplementation = false
};

var results = new TextExpressionCompiler(settings).Compile();

if (results.HasErrors)
{
throw new Exception("Compilation failed.");
}

var compiledExpressionRoot = Activator.CreateInstance(results.ResultType, new object[] { activity }) as ICompiledExpressionRoot;
CompiledExpressionInvoker.SetCompiledExpressionRoot(activity, compiledExpressionRoot);
}

最佳答案

错误非常明显;您的表达式在范围内没有变量 MailAddressVar

原因是您需要将变量传递到表达式中。 ExecuteIfTrue 事件没有变量参数。

改为尝试这样的事情:

Variable<string> mailAddressVar = new Variable<string>(name: "MailAddressVar", defaultValue: null);

Activity seq = new Sequence
{
Variables = { mailAddressVar },
Activities =
{
new ExecuteIfTrue
{
//Condition = new CSharpValue<Boolean>("Address.Email == null"),
Condition = new CSharpValue<Boolean>("MailAddressVar == null"),
Address = new InArgument<MailAddress>(ctx => new MailAddress { DisplayName = "TestDisplayName" }),
Body = new WriteLine { Text = "Rest!" }
}
}
};

ExecuteIfTrue.CompileCSharpExpressions<MailAddress>(seq);
WorkflowInvoker.Invoke(seq);

关于expression - WF。 NativeActivity 内部的 C# 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16193628/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com