WF4.5。使用带有外部类引用的C#表达式
我正在尝试使用新的WF4.5特性-C#表达式编译动态活动。 在我在表达式中添加外部类之前,它一直有效 如果表达式包含基本对象,则它是符合的。 如果我添加对外部类的引用,它将生成一个错误“找不到类型或命名空间名称“xxxxx”(是否缺少using指令或程序集引用?) 所以,问题是如何为C#表达式引用外部类 另外,它适用于VisualBasic表达式类型 谢谢WF4.5。使用带有外部类引用的C#表达式,c#,workflow-foundation,C#,Workflow Foundation,我正在尝试使用新的WF4.5特性-C#表达式编译动态活动。 在我在表达式中添加外部类之前,它一直有效 如果表达式包含基本对象,则它是符合的。 如果我添加对外部类的引用,它将生成一个错误“找不到类型或命名空间名称“xxxxx”(是否缺少using指令或程序集引用?) 所以,问题是如何为C#表达式引用外部类 另外,它适用于VisualBasic表达式类型 谢谢 //Compiled without errors var errorCodeWorkflow = new DynamicActivity
//Compiled without errors
var errorCodeWorkflow = new DynamicActivity
{
Name = "DynamicActivity",
Implementation = () => new WriteLine
{
Text = new CSharpValue<String>
{
ExpressionText = "new Random().Next(1, 101).ToString()"
}
}
};
CompileExpressions(errorCodeWorkflow);
WorkflowInvoker.Invoke(errorCodeWorkflow);
//Error
using System;
using System.Activities;
using System.Activities.Expressions;
using System.Activities.Statements;
using System.Activities.XamlIntegration;
using System.Collections.Generic;
using System.Linq;
using Microsoft.CSharp.Activities;
namespace CSharpExpression
{
class Program
{
static void Main()
{
var errorCodeWorkflow = new DynamicActivity
{
Name = "MyScenario.MyDynamicActivity3",
Properties =
{
new DynamicActivityProperty
{
Name = "Address",
Type = typeof(InArgument<MailAddress>),
},
},
Implementation = () => new WriteLine
{
Text = new CSharpValue<String>
{
ExpressionText = "\"MyDynamicActivity \" + Address.DisplayName"
}
}
};
CompileExpressions(errorCodeWorkflow);
WorkflowInvoker.Invoke(errorCodeWorkflow, new Dictionary<String, Object> { { "Address", new MailAddress { DisplayName = "TestDisplayName" } } });
}
static void CompileExpressions(DynamicActivity dynamicActivity)
{
var activityName = dynamicActivity.Name;
var activityType = activityName.Split('.').Last() + "_CompiledExpressionRoot";
var activityNamespace = string.Join(".", activityName.Split('.').Reverse().Skip(1).Reverse());
var settings = new TextExpressionCompilerSettings
{
Activity = dynamicActivity,
Language = "C#",
ActivityName = activityType,
ActivityNamespace = activityNamespace,
RootNamespace = "CSharpExpression",
GenerateAsPartialClass = false,
AlwaysGenerateSource = true,
ForImplementation = true
};
var results = new TextExpressionCompiler(settings).Compile();
if (results.HasErrors)
{
throw new Exception("Compilation failed.");
}
var compiledExpressionRoot = Activator.CreateInstance(results.ResultType, new object[] { dynamicActivity }) as ICompiledExpressionRoot;
CompiledExpressionInvoker.SetCompiledExpressionRootForImplementation(dynamicActivity, compiledExpressionRoot);
}
}
public class MailAddress
{
public String Address { get; set; }
public String DisplayName { get; set; }
}
}
//编译时没有错误
var errorCodeWorkflow=新的动态活动
{
Name=“DynamicActivity”,
实现=()=>newwriteline
{
Text=新值
{
ExpressionText=“new Random().Next(1101).ToString()”
}
}
};
编译表达式(errorCodeWorkflow);
WorkflowInvoker.Invoke(errorCodeWorkflow);
//错误
使用制度;
使用系统活动;
使用系统、活动、表达式;
使用系统、活动、报表;
使用System.Activities.XamlIntegration;
使用System.Collections.Generic;
使用System.Linq;
使用Microsoft.CSharp.Activities;
命名空间CSharpExpression
{
班级计划
{
静态void Main()
{
var errorCodeWorkflow=新的动态活动
{
Name=“MyScenario.MyDynamicActivity3”,
性质=
{
新的动态特性
{
Name=“地址”,
类型=类型(无粘性),
},
},
实现=()=>newwriteline
{
Text=新值
{
ExpressionText=“\”MyDynamicActivity\”+地址.显示名称”
}
}
};
编译表达式(errorCodeWorkflow);
调用(errorCodeWorkflow,新字典{{“地址”,新邮件地址{DisplayName=“TestDisplayName”}}});
}
静态void编译器表达式(DynamicActivity DynamicActivity)
{
var activityName=dynamicActivity.Name;
var activityType=activityName.Split('.').Last()+“_CompiledExpressionRoot”;
var activityNamespace=string.Join(“.”),activityName.Split('.').Reverse().Skip(1.Reverse());
var设置=新的TextExpressionCompilerSettings
{
活动=动态性,
Language=“C#”,
ActivityName=activityType,
ActivityNamespace=ActivityNamespace,
RootNamespace=“CSharpExpression”,
GeneratesPartialClass=false,
AlwaysGenerateSource=true,
ForImplementation=true
};
var results=new TextExpressionCompiler(设置).Compile();
if(results.HasErrors)
{
抛出新异常(“编译失败”);
}
var compiledExpressionRoot=Activator.CreateInstance(results.ResultType,新对象[]{dynamicActivity})作为ICompiledExpressionRoot;
SetCompiledExpressionRootForImplementation(dynamicActivity,compiledExpressionRoot);
}
}
公共类邮件地址
{
公共字符串地址{get;set;}
公共字符串DisplayName{get;set;}
}
}
听起来您需要一个using语句来指定外部类所在的名称空间
例如:
使用myNamespace 我在处理非动态活动时遇到了相同的问题,现在已经检查了使其工作所需的内容: 总结
System.Xaml的引用
邮件地址
移动到其他命名空间动态活动中添加一些有关课程来源的信息:
工作代码
使用系统;
使用系统活动;
使用系统、活动、表达式;
使用系统、活动、报表;
使用System.Activities.XamlIntegration;
使用System.Collections.Generic;
使用System.Linq;
使用System.Xaml;
使用外部名称空间;
使用Microsoft.CSharp.Activities;
命名空间CSharpExpression
{
班级计划
{
静态void Main()
{
var errorCodeWorkflow=新的动态活动
{
Name=“MyScenario.MyDynamicActivity3”,
性质=
{
新的动态特性
{
Name=“地址”,
类型=类型(无粘性),
},
},
实现=()=>newwriteline
{
Text=新值
{
ExpressionText=“\”MyDynamicActivity\”+地址.显示名称”
}
}
};
var impl=新的AttachableMemberIdentifier(typeof(TextExpression),“NamespacesForImplementation”);
var namespaces=新列表{typeof(maildAddress).Namespace};
TextExpression.SetReferencesForImplementation(errorCodeWorkflow,new AssemblyReference{Assembly=typeof(MailAddress.Assembly});
AttachablePropertyServices.SetProperty(errorCodeWorkflow、impl、命名空间);
编译表达式(errorCodeWorkflow);
调用(errorCodeWorkflow,新字典{{“地址”,新邮件地址{DisplayName=“TestDisplayName”}}});
}
静态void编译器表达式(DynamicActivity DynamicActivity)
{
var activityName=dynamicActivity.Name;
var activityType=activityName.S
var impl = new AttachableMemberIdentifier(typeof(TextExpression), "NamespacesForImplementation");
var namespaces = new List<string> { typeof(MailAddress).Namespace };
TextExpression.SetReferencesForImplementation(dynamicActivity, new AssemblyReference { Assembly = typeof(MailAddress).Assembly });
AttachablePropertyServices.SetProperty(dynamicActivity, impl, namespaces);
var impl = new AttachableMemberIdentifier(typeof(TextExpression), "Namespaces");
var namespaces = new List<string> { typeof(MailAddress).Namespace };
TextExpression.SetReferences(nonDynamicActivity, new AssemblyReference { Assembly = typeof(MailAddress).Assembly });
AttachablePropertyServices.SetProperty(nonDynamicActivity, impl, namespaces);
using System;
using System.Activities;
using System.Activities.Expressions;
using System.Activities.Statements;
using System.Activities.XamlIntegration;
using System.Collections.Generic;
using System.Linq;
using System.Xaml;
using ExternalNamespace;
using Microsoft.CSharp.Activities;
namespace CSharpExpression
{
class Program
{
static void Main()
{
var errorCodeWorkflow = new DynamicActivity
{
Name = "MyScenario.MyDynamicActivity3",
Properties =
{
new DynamicActivityProperty
{
Name = "Address",
Type = typeof(InArgument<MailAddress>),
},
},
Implementation = () => new WriteLine
{
Text = new CSharpValue<String>
{
ExpressionText = "\"MyDynamicActivity \" + Address.DisplayName"
}
}
};
var impl = new AttachableMemberIdentifier(typeof(TextExpression), "NamespacesForImplementation");
var namespaces = new List<string> { typeof(MailAddress).Namespace };
TextExpression.SetReferencesForImplementation(errorCodeWorkflow, new AssemblyReference { Assembly = typeof(MailAddress).Assembly });
AttachablePropertyServices.SetProperty(errorCodeWorkflow, impl, namespaces);
CompileExpressions(errorCodeWorkflow);
WorkflowInvoker.Invoke(errorCodeWorkflow, new Dictionary<String, Object> { { "Address", new MailAddress { DisplayName = "TestDisplayName" } } });
}
static void CompileExpressions(DynamicActivity dynamicActivity)
{
var activityName = dynamicActivity.Name;
var activityType = activityName.Split('.').Last() + "_CompiledExpressionRoot";
var activityNamespace = string.Join(".", activityName.Split('.').Reverse().Skip(1).Reverse());
var settings = new TextExpressionCompilerSettings
{
Activity = dynamicActivity,
Language = "C#",
ActivityName = activityType,
ActivityNamespace = activityNamespace,
RootNamespace = "CSharpExpression",
GenerateAsPartialClass = false,
AlwaysGenerateSource = true,
ForImplementation = true
};
var results = new TextExpressionCompiler(settings).Compile();
if (results.HasErrors)
{
throw new Exception("Compilation failed.");
}
var compiledExpressionRoot = Activator.CreateInstance(results.ResultType, new object[] { dynamicActivity }) as ICompiledExpressionRoot;
CompiledExpressionInvoker.SetCompiledExpressionRootForImplementation(dynamicActivity, compiledExpressionRoot);
}
}
}
namespace ExternalNamespace
{
public class MailAddress
{
public String Address { get; set; }
public String DisplayName { get; set; }
}
}