Allow C# Script to use dot notation with ExpandoObject (JSON variables) (#6689)

* Initial plan for issue

* Implement special handling for ExpandoObject variables in C# Script

Co-authored-by: sfmskywalker <938393+sfmskywalker@users.noreply.github.com>

* Refactor ExpandoObject handling to make code more concise

Co-authored-by: sfmskywalker <938393+sfmskywalker@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: sfmskywalker <938393+sfmskywalker@users.noreply.github.com>
This commit is contained in:
Copilot 2025-05-25 12:26:59 +02:00 committed by GitHub
parent f041f55624
commit d6c90b611e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,3 +1,4 @@
using System.Dynamic;
using System.Text;
using Elsa.Expressions.CSharp.Extensions;
using Elsa.Expressions.CSharp.Notifications;
@ -38,10 +39,17 @@ public class GenerateWorkflowVariableAccessors(IOptions<CSharpOptions> options)
{
var variableName = variable.Name.Pascalize();
var variableType = variable.GetVariableType();
var friendlyTypeName = variableType.GetFriendlyTypeName(Brackets.Angle);
sb.AppendLine($"\tpublic {friendlyTypeName} {variableName}");
// Check if the variable type is ExpandoObject
bool isExpandoObject = variableType == typeof(ExpandoObject);
// Use dynamic type for ExpandoObject to enable dot notation but keep the original type for retrieval
var displayTypeName = isExpandoObject ? "dynamic" : variableType.GetFriendlyTypeName(Brackets.Angle);
var retrieveTypeName = isExpandoObject ? "ExpandoObject" : displayTypeName;
sb.AppendLine($"\tpublic {displayTypeName} {variableName}");
sb.AppendLine("\t{");
sb.AppendLine($"\t\tget => Get<{friendlyTypeName}>(\"{variableName}\");");
sb.AppendLine($"\t\tget => Get<{retrieveTypeName}>(\"{variableName}\");");
sb.AppendLine($"\t\tset => Set(\"{variableName}\", value);");
sb.AppendLine("\t}");
}