Skip to content

Commit 2f9c286

Browse files
authored
1 parent 8622e13 commit 2f9c286

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

LiteDB/Client/Mapper/Linq/LinqExpressionVisitor.cs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ internal class LinqExpressionVisitor : ExpressionVisitor
2626
[typeof(ICollection)] = new ICollectionResolver(),
2727
[typeof(IGrouping<,>)] = new GroupingResolver(),
2828
[typeof(Enumerable)] = new EnumerableResolver(),
29+
[typeof(MemoryExtensions)] = new MemoryExtensionsResolver(),
2930
[typeof(Guid)] = new GuidResolver(),
3031
[typeof(Math)] = new MathResolver(),
3132
[typeof(Regex)] = new RegexResolver(),
@@ -182,6 +183,13 @@ protected override Expression VisitMember(MemberExpression node)
182183
/// </summary>
183184
protected override Expression VisitMethodCall(MethodCallExpression node)
184185
{
186+
if (this.IsSpanImplicitConversion(node.Method))
187+
{
188+
this.Visit(node.Arguments[0]);
189+
190+
return node;
191+
}
192+
185193
// if special method for index access, eval index value (do not use parameters)
186194
if (this.IsMethodIndexEval(node, out var obj, out var idx))
187195
{
@@ -757,5 +765,30 @@ private bool TryGetResolver(Type declaringType, out ITypeResolver typeResolver)
757765

758766
return _resolver.TryGetValue(type, out typeResolver);
759767
}
768+
769+
private bool IsSpanImplicitConversion(MethodInfo method)
770+
{
771+
if (method == null || method.Name != "op_Implicit" || method.GetParameters().Length != 1)
772+
{
773+
return false;
774+
}
775+
776+
var returnType = method.ReturnType;
777+
778+
return this.IsSpanLike(returnType);
779+
}
780+
781+
private bool IsSpanLike(Type type)
782+
{
783+
if (type == null || !type.IsGenericType)
784+
{
785+
return false;
786+
}
787+
788+
var definition = type.GetGenericTypeDefinition();
789+
var name = definition.FullName;
790+
791+
return name == "System.Span`1" || name == "System.ReadOnlySpan`1";
792+
}
760793
}
761-
}
794+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.Reflection;
2+
3+
namespace LiteDB
4+
{
5+
internal class MemoryExtensionsResolver : ITypeResolver
6+
{
7+
public string ResolveMethod(MethodInfo method)
8+
{
9+
if (method.Name == nameof(System.MemoryExtensions.Contains))
10+
{
11+
var parameters = method.GetParameters();
12+
13+
if (parameters.Length == 2)
14+
{
15+
return "@0 ANY = @1";
16+
}
17+
}
18+
19+
return null;
20+
}
21+
22+
public string ResolveMember(MemberInfo member) => null;
23+
24+
public string ResolveCtor(ConstructorInfo ctor) => null;
25+
}
26+
}

0 commit comments

Comments
 (0)