Skip to content

Commit e27564a

Browse files
committed
WIP
1 parent 5fefd46 commit e27564a

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

src/UiPath.Workflow/XamlIntegration/TextExpressionCompiler.cs

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,7 +1525,8 @@ private void GenerateExpressionGetTreeMethod(Activity activity, CompiledExpressi
15251525
}
15261526
else if (IsCs)
15271527
{
1528-
expressionText = string.Concat(CSharpLambdaString, coreExpressionText);
1528+
var safeCastText = SafeCastText(coreExpressionText, expressionDescriptor.ResultType);
1529+
expressionText = string.Concat(CSharpLambdaString, safeCastText);
15291530
}
15301531

15311532
if (expressionText != null)
@@ -1570,7 +1571,16 @@ private CodeMemberMethod GenerateGetMethod(Activity activity, Type resultType, s
15701571
new CodeAttributeDeclaration(new CodeTypeReference(typeof(DebuggerHiddenAttribute))));
15711572

15721573
AlignText(activity, ref expressionText, out var pragma);
1573-
CodeStatement statement = new CodeMethodReturnStatement(new CodeSnippetExpression(expressionText));
1574+
CodeStatement statement;
1575+
if (IsCs)
1576+
{
1577+
statement = new CodeMethodReturnStatement(SafeCast(expressionText, resultType));
1578+
}
1579+
else
1580+
{
1581+
statement = new CodeMethodReturnStatement(new CodeSnippetExpression(expressionText));
1582+
}
1583+
15741584
statement.LinePragma = pragma;
15751585
expressionMethod.Statements.Add(statement);
15761586

@@ -2512,6 +2522,36 @@ private string GetActivityFullName(TextExpressionCompilerSettings settings)
25122522
return activityFullName;
25132523
}
25142524

2525+
private string GetFriendlyTypeName(Type type)
2526+
{
2527+
using var codeDomProvider = CodeDomProvider.CreateProvider(_settings.Language);
2528+
return type is null ? null : codeDomProvider.GetTypeOutput(new CodeTypeReference(type));
2529+
}
2530+
2531+
/// <summary>
2532+
/// Safely formats a string representation of a variable name and its target type.
2533+
/// </summary>
2534+
/// <param name="variableName">The name of the variable to be cast. If null or whitespace, "null" is used instead.</param>
2535+
/// <param name="targetType">The target <see cref="Type"/> to which the variable is being cast.</param>
2536+
/// <returns>A string in the format "<paramref name="variableName"/> as <paramref name="targetType"/>".</returns>
2537+
private string SafeCastText(string variableName, Type targetType)
2538+
{
2539+
string varName = string.IsNullOrWhiteSpace(variableName) ? "null" : variableName;
2540+
string typeName = GetFriendlyTypeName(targetType);
2541+
if (typeName == null)
2542+
{
2543+
return varName;
2544+
}
2545+
return $"{varName} as {typeName}";
2546+
}
2547+
2548+
/// <summary>
2549+
/// Creates a CodeSnippetExpression like: "variableName as TargetType".
2550+
/// If variableName or targetType is null/empty, falls back to "null".
2551+
/// </summary>
2552+
private CodeSnippetExpression SafeCast(string variableName, Type targetType)
2553+
=> new CodeSnippetExpression(SafeCastText(variableName, targetType));
2554+
25152555
private class ExpressionCompilerActivityVisitor : CompiledExpressionActivityVisitor
25162556
{
25172557
private readonly TextExpressionCompiler _compiler;

0 commit comments

Comments
 (0)