workflow foundation - WF 4.5. Using C# expressions with external class references -
i trying compile dynamic activity using new wf 4.5 features - c# expression. works until add external class in expression.
if expression contains basic objects it's complied. if add reference external class it's generates error "the type or namespace name 'xxxxx' not found (are missing using directive or assembly reference?)"
so, question how can reference external classes c# expressions?
p.s. works fine visualbasic expression type
thanks
//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 }) icompiledexpressionroot; compiledexpressioninvoker.setcompiledexpressionrootforimplementation(dynamicactivity, compiledexpressionroot); } } public class mailaddress { public string address { get; set; } public string displayname { get; set; } } }
i ran identical issue working non-dynamic activities , have checked required work:
summary
add reference
system.xaml
move
mailaddress
different namespace.add information
dynamic activity
classes coming from:
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);
to @ answer needed dig around in textexpressioncompiler
source there might lot more elegant.
non-dynamic activities
if not using dynamic activities call compileexpressions
change described here: http://msdn.microsoft.com/en-us/library/jj591618.aspx.
adding information activity modified removing "forimplementation" parts:
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);
working code
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 }) icompiledexpressionroot; compiledexpressioninvoker.setcompiledexpressionrootforimplementation(dynamicactivity, compiledexpressionroot); } } } namespace externalnamespace { public class mailaddress { public string address { get; set; } public string displayname { get; set; } } }
Comments
Post a Comment