Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 2 additions & 31 deletions src/UiPath.Workflow/XamlIntegration/TextExpressionCompiler.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// This file is part of Core WF which is licensed under the MIT license.
// See LICENSE file in the project root for full license information.

using Microsoft.VisualBasic.Activities;
using System.Activities.Expressions;
using System.Activities.Internals;
using System.Activities.Runtime;
Expand All @@ -15,7 +16,6 @@
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Microsoft.VisualBasic.Activities;

namespace System.Activities.XamlIntegration;

Expand Down Expand Up @@ -2530,34 +2530,13 @@ private string GetActivityFullName(TextExpressionCompilerSettings settings)

private string FormatWithTypeCast(string coreExpressionText, Type resultType)
{
if (resultType == null)
if (resultType == null || string.IsNullOrEmpty(coreExpressionText))
{
return coreExpressionText;
}

if (CanUseSafeCastOperator(resultType))
{
return SafeCastText(coreExpressionText, resultType);
}

return ExplicitCastText(coreExpressionText, resultType);
}

private static bool CanUseSafeCastOperator(Type targetType)
=> !targetType.IsValueType || IsNullableValueType(targetType);

private string SafeCastText(string expressionText, Type targetType)
{
string typeName = GetFriendlyTypeName(targetType);
if (typeName == null)
{
return expressionText;
}

// Use 'as' operator for reference types and nullable value types
return $"{expressionText} as {typeName}";
}

private string ExplicitCastText(string expressionText, Type targetType)
{
string typeName = GetFriendlyTypeName(targetType);
Expand All @@ -2569,14 +2548,6 @@ private string ExplicitCastText(string expressionText, Type targetType)
return $"(({typeName}){expressionText})";
}

/// <summary>
/// Helper method to check if a type is a nullable value type (e.g., int?, DateTime?)
/// </summary>
private static bool IsNullableValueType(Type type)
{
return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>);
}

private string GetFriendlyTypeName(Type type)
{
return type is null ? null : _lazyProvider.Value.GetTypeOutput(new CodeTypeReference(type));
Expand Down