wf4 一个activity只能访问自己的实现变量
wf4 An activity can only access its own implementation variables
我有一个带有变量(C# 表达式)的 activity,但无法读取它们的值。
public Collection<Variable> Variables { get; } = new Collection<Variable>();
protected override void DoExecute(NativeActivityContext context)
{
var x = Variables.FirstOrDefault(...).Get(context);
}
导致
Activity '1.1: MyActivity' cannot access this variable
because it is declared at the scope of activity '1.1: MyActivity'.
An activity can only access its own implementation variables.
我试图通过缓存元数据公开它们
protected override void CacheMetadata(NativeActivityMetadata metadata)
{
metadata.SetImplementationVariablesCollection(Variables);
}
结果是
Exception <System.NotSupportedException:
Expression Activity type 'CSharpValue`1' requires compilation in order to run.
Please ensure that the workflow has been compiled.
我的变量是 c# 表达式并使用
编译
var wwfActivity = ActivityXamlServices.Load(xamlReader, new ActivityXamlServicesSettings {CompileExpressions = true});
我可以用
破解它
var var = context.DataContext.GetProperties()["variableName"];
var value = var.GetValue(context.DataContext) as Whatever;
没有覆盖CacheMetadata
方法,但感觉有点奇怪;
我认为如果你在中间有一个计数器的 while 循环中并试图访问像 "Number" + i.ToString 这样的变量,你将不会得到正确的答案。它不会评估 i.ToString.
using System;
using System.ComponentModel;
using System.IO;
using System.Runtime;
using System.Activities.Validation;
using System.Collections.Generic;
using System.Windows.Markup;
using System.Collections.ObjectModel;
using System.Activities;
namespace WorkflowConsoleApplication2
{
public sealed class CodeActivity1 : CodeActivity
{
// Define an activity input argument of type string
[DefaultValue(null)]
public InArgument<string> Test
{
get;
set;
}
protected override void CacheMetadata(CodeActivityMetadata metadata)
{
RuntimeArgument textArgument = new RuntimeArgument("Test", typeof(string), ArgumentDirection.In);
metadata.Bind(this.Test, textArgument);
metadata.SetArgumentsCollection(
new Collection<RuntimeArgument>
{
textArgument,
});
}
// If your activity returns a value, derive from CodeActivity<TResult>
// and return the value from the Execute method.
protected override void Execute(CodeActivityContext context)
{
Console.WriteLine(this.Test.Get(context));
}
}
您需要将该 RuntimeArgument 添加到 ArgumentCollection 中,如上所示。
我从我回答的问题中得到这个:
我有一个带有变量(C# 表达式)的 activity,但无法读取它们的值。
public Collection<Variable> Variables { get; } = new Collection<Variable>();
protected override void DoExecute(NativeActivityContext context)
{
var x = Variables.FirstOrDefault(...).Get(context);
}
导致
Activity '1.1: MyActivity' cannot access this variable
because it is declared at the scope of activity '1.1: MyActivity'.
An activity can only access its own implementation variables.
我试图通过缓存元数据公开它们
protected override void CacheMetadata(NativeActivityMetadata metadata)
{
metadata.SetImplementationVariablesCollection(Variables);
}
结果是
Exception <System.NotSupportedException:
Expression Activity type 'CSharpValue`1' requires compilation in order to run.
Please ensure that the workflow has been compiled.
我的变量是 c# 表达式并使用
编译var wwfActivity = ActivityXamlServices.Load(xamlReader, new ActivityXamlServicesSettings {CompileExpressions = true});
我可以用
破解它var var = context.DataContext.GetProperties()["variableName"];
var value = var.GetValue(context.DataContext) as Whatever;
没有覆盖CacheMetadata
方法,但感觉有点奇怪;
我认为如果你在中间有一个计数器的 while 循环中并试图访问像 "Number" + i.ToString 这样的变量,你将不会得到正确的答案。它不会评估 i.ToString.
using System;
using System.ComponentModel;
using System.IO;
using System.Runtime;
using System.Activities.Validation;
using System.Collections.Generic;
using System.Windows.Markup;
using System.Collections.ObjectModel;
using System.Activities;
namespace WorkflowConsoleApplication2
{
public sealed class CodeActivity1 : CodeActivity
{
// Define an activity input argument of type string
[DefaultValue(null)]
public InArgument<string> Test
{
get;
set;
}
protected override void CacheMetadata(CodeActivityMetadata metadata)
{
RuntimeArgument textArgument = new RuntimeArgument("Test", typeof(string), ArgumentDirection.In);
metadata.Bind(this.Test, textArgument);
metadata.SetArgumentsCollection(
new Collection<RuntimeArgument>
{
textArgument,
});
}
// If your activity returns a value, derive from CodeActivity<TResult>
// and return the value from the Execute method.
protected override void Execute(CodeActivityContext context)
{
Console.WriteLine(this.Test.Get(context));
}
}
您需要将该 RuntimeArgument 添加到 ArgumentCollection 中,如上所示。
我从我回答的问题中得到这个: