C# WF 4.5编译CSharpValue<;T>;错误。正确的方法是什么?
我一直在联合测试编译C# WF 4.5编译CSharpValue<;T>;错误。正确的方法是什么?,c#,workflow-foundation-4,workflow-foundation,C#,Workflow Foundation 4,Workflow Foundation,我一直在联合测试编译CSharpValue活动的两个简单示例。一个有效,另一个无效我不明白为什么。如果有人能够指出问题,并在可能的情况下进行修改以纠正问题 详情: 第一个单元测试工作SequenceActivityCompile()第二个CodeActivityCompile失败,出现NotSupportedException(表达式活动类型CSharpValue需要编译才能运行。请确保已编译工作流。) 我听说这可能与ForImplementation有关,但是CodeActivityCompi
CSharpValue
活动的两个简单示例。一个有效,另一个无效我不明白为什么。如果有人能够指出问题,并在可能的情况下进行修改以纠正问题
详情:
第一个单元测试工作SequenceActivityCompile()
第二个CodeActivityCompile
失败,出现NotSupportedException
(表达式活动类型CSharpValue
需要编译才能运行。请确保已编译工作流。)
我听说这可能与ForImplementation
有关,但是CodeActivityCompile
的值是真还是假都有相同的错误
此示例是对Microsoft示例的基本改编,位于:
这篇示例博客文章详细讨论了在WF4+中编译C#表达式。如果有人提出这个问题,需要对该主题进行基本介绍:
相关代码:
[TestMethod]
public void SequenceActivityCompile()
{
Activity sequence = new Sequence
{
Activities = { new CSharpValue<string>("\"Hello World \"") }
};
CompileExpressions(sequence);
var result = WorkflowInvoker.Invoke(sequence);
}
[TestMethod]
public void CodeActivityCompile()
{
var code = new CSharpValue<String>("\"Hello World\"");
CompileExpressions(code);
var result = WorkflowInvoker.Invoke(code);
}
void CompileExpressions(Activity activity)
{
// activityName is the Namespace.Type of the activity that contains the
// C# expressions.
string activityName = activity.GetType().ToString();
// Split activityName into Namespace and Type.Append _CompiledExpressionRoot to the type name
// to represent the new type that represents the compiled expressions.
// Take everything after the last . for the type name.
//string activityType = activityName.Split('.').Last() + "_CompiledExpressionRoot";
string activityType = "TestType";
// Take everything before the last . for the namespace.
//string activityNamespace = string.Join(".", activityName.Split('.').Reverse().Skip(1).Reverse());
string activityNamespace = "TestSpace";
// Create a TextExpressionCompilerSettings.
TextExpressionCompilerSettings settings = new TextExpressionCompilerSettings
{
Activity = activity,
Language = "C#",
ActivityName = activityType,
ActivityNamespace = activityNamespace,
RootNamespace = null,
GenerateAsPartialClass = false,
AlwaysGenerateSource = true,
ForImplementation = false
};
// Compile the C# expression.
TextExpressionCompilerResults results =
new TextExpressionCompiler(settings).Compile();
// Any compilation errors are contained in the CompilerMessages.
if (results.HasErrors)
{
throw new Exception("Compilation failed.");
}
// Create an instance of the new compiled expression type.
ICompiledExpressionRoot compiledExpressionRoot =
Activator.CreateInstance(results.ResultType,
new object[] { activity }) as ICompiledExpressionRoot;
// Attach it to the activity.
System.Activities.Expressions.CompiledExpressionInvoker.SetCompiledExpressionRoot(
activity, compiledExpressionRoot);
}
[TestMethod]
public void SequenceActivityCompile()
{
活动序列=新序列
{
活动={new CSharpValue(“\'Hello World\”)}
};
编译表达式(序列);
var result=WorkflowInvoker.Invoke(序列);
}
[测试方法]
public void CodeActivityCompile()
{
var代码=新CSharpValue(“\“Hello World\”);
编译表达式(代码);
var result=WorkflowInvoker.Invoke(代码);
}
void CompileExpressions(活动)
{
//activityName是命名空间。包含
//C#表达式。
字符串activityName=activity.GetType().ToString();
//将activityName拆分为命名空间和类型。将_CompiledExpressionRoot附加到类型名称
//表示表示已编译表达式的新类型。
//取最后一个之后的所有内容。对于类型名称。
//字符串activityType=activityName.Split('.').Last()+“_CompiledExpressionRoot”;
字符串activityType=“TestType”;
//取最后一个之前的所有内容。用于命名空间。
//string activityNamespace=string.Join(“.”),activityName.Split('.').Reverse().Skip(1.Reverse());
字符串activityNamespace=“TestSpace”;
//创建TextExpressionCompilerSettings。
TextExpressionCompilerSettings设置=新建TextExpressionCompilerSettings
{
活动=活动,
Language=“C#”,
ActivityName=activityType,
ActivityNamespace=ActivityNamespace,
RootNamespace=null,
GeneratesPartialClass=false,
AlwaysGenerateSource=true,
ForImplementation=false
};
//编译C#表达式。
TextExpressionCompilerResults结果=
新建TextExpressionCompiler(设置).Compile();
//编译器消息中包含任何编译错误。
if(results.HasErrors)
{
抛出新异常(“编译失败”);
}
//创建新编译表达式类型的实例。
ICompiledExpressionRoot编译表达式根=
Activator.CreateInstance(results.ResultType,
新对象[]{activity})作为ICompiledExpressionRoot;
//将其附加到活动中。
System.Activities.Expressions.CompiledExpressionInvoker.SetCompiledExpressionRoot(
活动,compiledExpressionRoot);
}