Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullReferenceException thrown when using CSharpValue #226

Open
zh6335901 opened this issue Dec 25, 2022 · 0 comments
Open

NullReferenceException thrown when using CSharpValue #226

zh6335901 opened this issue Dec 25, 2022 · 0 comments

Comments

@zh6335901
Copy link

Here is sample code:

internal static class Program
{
    static void Main(string[] args)
    {
        var v1 = new Variable<int>();

        var helloWorldActivity = new Sequence()
        {
            Variables = { v1 },
            Activities =
            {
                new WriteLine
                {
                    Text = new CSharpValue<string>("\"aaaa\" + \"sadas\"")
                }
            }
        };

        CompileWorkflow(helloWorldActivity);
        var wfApp = new WorkflowApplication(helloWorldActivity);

        wfApp.Run();

        Console.ReadLine();
    }

    private static void CompileWorkflow(Activity activity)
    {
        var activityName = activity.GetType().ToString();
        var activityType = activityName.Split('.').Last() + "_CompiledExpressionRoot";
        var activityNamespace = string.Join(".", activityName.Split('.').Reverse().Skip(1).Reverse());

        var settings = new TextExpressionCompilerSettings
        {
            Activity = activity,
            Language = "C#",
            ActivityName = activityType,
            ActivityNamespace = activityNamespace,
            RootNamespace = null,
            GenerateAsPartialClass = false,
            AlwaysGenerateSource = true,
            ForImplementation = false,
        };

        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.
        var compiledExpressionRoot =
            Activator.CreateInstance(results.ResultType,
                new object[] { activity }) as ICompiledExpressionRoot;

        CompiledExpressionInvoker.SetCompiledExpressionRoot(
            activity, compiledExpressionRoot);
    }
}

The result of running:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant