Skip to content

Commit 8b74ee2

Browse files
CopilotStefH
andcommitted
Add implicit operator support in ExpressionPromoter and tests
Agent-Logs-Url: https://github.com/zzzprojects/System.Linq.Dynamic.Core/sessions/82ed2f4e-0c76-407c-a330-d4043e96162f Co-authored-by: StefH <249938+StefH@users.noreply.github.com>
1 parent e60d21e commit 8b74ee2

File tree

3 files changed

+18659
-0
lines changed

3 files changed

+18659
-0
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Linq.Expressions;
22
using System.Reflection;
3+
using System.Linq;
34

45
namespace System.Linq.Dynamic.Core.Parser;
56

@@ -135,6 +136,22 @@ public ExpressionPromoter(ParsingConfig config)
135136
return sourceExpression;
136137
}
137138

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+
.Where(m => m.Name == "op_Implicit" && m.ReturnType == type)
144+
.FirstOrDefault(m => m.GetParameters().FirstOrDefault()?.ParameterType == returnType)
145+
??
146+
type.GetMethods(BindingFlags.Public | BindingFlags.Static)
147+
.Where(m => m.Name == "op_Implicit" && m.ReturnType == type)
148+
.FirstOrDefault(m => m.GetParameters().FirstOrDefault()?.ParameterType == returnType);
149+
150+
if (implicitOperator != null)
151+
{
152+
return Expression.Convert(sourceExpression, type);
153+
}
154+
138155
return null;
139156
}
140157
}

0 commit comments

Comments
 (0)