From d6c90b611ea91cfbfc33498bb461a5651d941cdf Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Sun, 25 May 2025 12:26:59 +0200 Subject: [PATCH] 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> --- .../Handlers/GenerateWorkflowVariableAccessors.cs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/modules/Elsa.Expressions.CSharp/Handlers/GenerateWorkflowVariableAccessors.cs b/src/modules/Elsa.Expressions.CSharp/Handlers/GenerateWorkflowVariableAccessors.cs index 248873d9b..903fde04a 100644 --- a/src/modules/Elsa.Expressions.CSharp/Handlers/GenerateWorkflowVariableAccessors.cs +++ b/src/modules/Elsa.Expressions.CSharp/Handlers/GenerateWorkflowVariableAccessors.cs @@ -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 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}"); }