Skip to content

Commit cbe4a92

Browse files
committed
refactor code
1 parent 02aa9d5 commit cbe4a92

File tree

3 files changed

+29
-33
lines changed

3 files changed

+29
-33
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.TryGetImplicitConversionOperatorMethod(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 & 21 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

@@ -136,26 +135,7 @@ public ExpressionPromoter(ParsingConfig config)
136135
return sourceExpression;
137136
}
138137

139-
// 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)
138+
if (TypeHelper.TryGetImplicitConversionOperatorMethod(returnType, type, out _))
159139
{
160140
return Expression.Convert(sourceExpression, type);
161141
}

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,4 +533,31 @@ public static bool IsDictionary(Type? type)
533533
TryFindGenericType(typeof(IReadOnlyDictionary<,>), type, out _);
534534
#endif
535535
}
536+
537+
/// <summary>
538+
/// Check for implicit conversion operators (op_Implicit) from returnType to type.
539+
/// Look for op_Implicit on the source type or the target type.
540+
/// </summary>
541+
public static bool TryGetImplicitConversionOperatorMethod(Type returnType, Type type, [NotNullWhen(true)] out MethodBase? implicitOperator)
542+
{
543+
const string methodName = "op_Implicit";
544+
545+
implicitOperator = Find(returnType) ?? Find(type);
546+
return implicitOperator != null;
547+
548+
MethodBase? Find(Type searchType)
549+
{
550+
return searchType.GetMethods(BindingFlags.Public | BindingFlags.Static)
551+
.FirstOrDefault(m =>
552+
{
553+
if (m.Name != methodName || m.ReturnType != type)
554+
{
555+
return false;
556+
}
557+
558+
var parameters = m.GetParameters();
559+
return parameters.Length == 1 && parameters[0].ParameterType == returnType;
560+
});
561+
}
562+
}
536563
}

0 commit comments

Comments
 (0)