Skip to content

Commit d0cc881

Browse files
CopilotStefH
andcommitted
Refactor: move implicit operator lookup to TypeHelper.TryFindImplicitConversionOperator
Agent-Logs-Url: https://github.com/zzzprojects/System.Linq.Dynamic.Core/sessions/3d221836-ae46-4e82-b79f-a67b544ee3af Co-authored-by: StefH <249938+StefH@users.noreply.github.com>
1 parent 02aa9d5 commit d0cc881

File tree

3 files changed

+30
-32
lines changed

3 files changed

+30
-32
lines changed

src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -680,18 +680,7 @@ private Expression ParseComparisonOperator()
680680

681681
private static bool HasImplicitConversion(Type baseType, Type targetType)
682682
{
683-
var baseTypeHasConversion = baseType.GetMethods(BindingFlags.Public | BindingFlags.Static)
684-
.Where(mi => mi.Name == "op_Implicit" && mi.ReturnType == targetType)
685-
.Any(mi => mi.GetParameters().FirstOrDefault()?.ParameterType == baseType);
686-
687-
if (baseTypeHasConversion)
688-
{
689-
return true;
690-
}
691-
692-
return targetType.GetMethods(BindingFlags.Public | BindingFlags.Static)
693-
.Where(mi => mi.Name == "op_Implicit" && mi.ReturnType == targetType)
694-
.Any(mi => mi.GetParameters().FirstOrDefault()?.ParameterType == baseType);
683+
return TypeHelper.TryFindImplicitConversionOperator(baseType, targetType, out _);
695684
}
696685

697686
private static ConstantExpression ParseEnumToConstantExpression(int pos, Type leftType, ConstantExpression constantExpr)

src/System.Linq.Dynamic.Core/Parser/ExpressionPromoter.cs

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Linq.Expressions;
22
using System.Reflection;
3-
using System.Linq;
43

54
namespace System.Linq.Dynamic.Core.Parser;
65

@@ -137,25 +136,7 @@ public ExpressionPromoter(ParsingConfig config)
137136
}
138137

139138
// Check for implicit conversion operators (op_Implicit) from returnType to type.
140-
// Look for op_Implicit on the source type or the target type.
141-
var implicitOperator =
142-
returnType.GetMethods(BindingFlags.Public | BindingFlags.Static)
143-
.FirstOrDefault(m =>
144-
{
145-
if (m.Name != "op_Implicit" || m.ReturnType != type) return false;
146-
var parameters = m.GetParameters();
147-
return parameters.Length == 1 && parameters[0].ParameterType == returnType;
148-
})
149-
??
150-
type.GetMethods(BindingFlags.Public | BindingFlags.Static)
151-
.FirstOrDefault(m =>
152-
{
153-
if (m.Name != "op_Implicit" || m.ReturnType != type) return false;
154-
var parameters = m.GetParameters();
155-
return parameters.Length == 1 && parameters[0].ParameterType == returnType;
156-
});
157-
158-
if (implicitOperator != null)
139+
if (TypeHelper.TryFindImplicitConversionOperator(returnType, type, out _))
159140
{
160141
return Expression.Convert(sourceExpression, type);
161142
}

src/System.Linq.Dynamic.Core/Parser/TypeHelper.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,4 +533,32 @@ public static bool IsDictionary(Type? type)
533533
TryFindGenericType(typeof(IReadOnlyDictionary<,>), type, out _);
534534
#endif
535535
}
536+
537+
public static bool TryFindImplicitConversionOperator(Type sourceType, Type targetType, [NotNullWhen(true)] out MethodInfo? implicitOperator)
538+
{
539+
// Look for op_Implicit on the source type that converts sourceType -> targetType
540+
implicitOperator = sourceType.GetMethods(BindingFlags.Public | BindingFlags.Static)
541+
.FirstOrDefault(m =>
542+
{
543+
if (m.Name != "op_Implicit" || m.ReturnType != targetType) return false;
544+
var parameters = m.GetParameters();
545+
return parameters.Length == 1 && parameters[0].ParameterType == sourceType;
546+
});
547+
548+
if (implicitOperator != null)
549+
{
550+
return true;
551+
}
552+
553+
// Look for op_Implicit on the target type that converts sourceType -> targetType
554+
implicitOperator = targetType.GetMethods(BindingFlags.Public | BindingFlags.Static)
555+
.FirstOrDefault(m =>
556+
{
557+
if (m.Name != "op_Implicit" || m.ReturnType != targetType) return false;
558+
var parameters = m.GetParameters();
559+
return parameters.Length == 1 && parameters[0].ParameterType == sourceType;
560+
});
561+
562+
return implicitOperator != null;
563+
}
536564
}

0 commit comments

Comments
 (0)