问题
I hope I'm not being misguided in trying to do this, but I'd like to set the VB expression for an InArgument in Windows Workflow at runtime and have it access variables that are currently in scope.
In particular, I'd like to change the following bit of example code to say new VisualBasicValue<string>("String.Format(\"Hello {0}\", FirstName)
- that is, I'd like to stop specifying the explicit string "World" and start passing in a name to the workflow from Main
. I add a new InArgument called FirstName
to the workflow itself, and then try and access it as above, but it doesn't work - I'm guessing because it doesn't know what FirstName
is referring to at the point at which it compiles the expression. Please do you know of an alternative way to accomplish the same goal?
(The context for this problem is that I'm writing code to run different types of calculation. A calculation takes a string parameter that contains a VB expression that must be run to get some data from one of a variety of different locations. I want to specify the VB expression for each different calculation type in a .config file, then evaluate those expressions "on-the-fly" at runtime when it becomes necessary to run the various different calculations.)
using System.Activities;
using Microsoft.VisualBasic.Activities;
namespace SMG_RuntimeExpressionTest
{
class Program
{
static void Main(string[] args)
{
var wf = new Workflow();
wf.Text = new VisualBasicValue<string>("String.Format(\"Hello {0}\", \"World\")");
WorkflowInvoker.Invoke(wf);
}
}
}
回答1:
I've got a couple of blog posts on arguments WF4: Passing Arguments to Activities and Passing arguments to Workflow Activities (again) that might help you.
来源:https://stackoverflow.com/questions/8350154/how-to-access-variables-in-windows-workflow-in-vb-expressions-constructed-at-run