-
-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathDynamicGetMemberBinder.cs
More file actions
90 lines (75 loc) · 3.52 KB
/
DynamicGetMemberBinder.cs
File metadata and controls
90 lines (75 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#if !NET35 && !UAP10_0 && !NETSTANDARD1_3
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq.Expressions;
using System.Reflection;
namespace System.Linq.Dynamic.Core;
/// <summary>
/// Code is based on SqlLinq by dkackman (https://github.com/dkackman/SqlLinq/blob/210b594e37f14061424397368ed750ce547c21e7/License.md) however it's modified to solve several issues.
/// </summary>
/// <seealso cref="GetMemberBinder" />
internal class DynamicGetMemberBinder : GetMemberBinder
{
private static readonly MethodInfo DynamicGetMemberMethod = typeof(DynamicGetMemberBinder).GetMethod(nameof(GetDynamicMember))!;
// The _metaObjectCache uses a Tuple<Type, string, bool> as the key to cache DynamicMetaObject instances.
// The key components are:
// - Type: The LimitType of the target object, ensuring type-specific caching.
// - string: The member name being accessed.
// - bool: The IgnoreCase flag, indicating whether the member name comparison is case-insensitive.
// This strategy ensures that the cache correctly handles different types, member names, and case-sensitivity settings.
private readonly ConcurrentDictionary<Tuple<Type, string, bool>, DynamicMetaObject> _metaObjectCache = new();
internal DynamicGetMemberBinder(string name, ParsingConfig? config) : base(name, config?.IsCaseSensitive != true)
{
}
public override DynamicMetaObject FallbackGetMember(DynamicMetaObject target, DynamicMetaObject? errorSuggestion)
{
var methodCallExpression = Expression.Call(
DynamicGetMemberMethod,
target.Expression,
Expression.Constant(Name),
Expression.Constant(IgnoreCase));
// Fix #907 and #912: "The result of the dynamic binding produced by the object with type '<>f__AnonymousType1`4' for the binder 'System.Linq.Dynamic.Core.DynamicGetMemberBinder' needs at least one restriction.".
// Fix #921: "Slow Performance"
// Only add TypeRestriction if it's a Dynamic type and make sure to cache the DynamicMetaObject.
if (target.Value is IDynamicMetaObjectProvider)
{
var key = new Tuple<Type, string, bool>(target.LimitType, Name, IgnoreCase);
return _metaObjectCache.GetOrAdd(key, _ =>
{
var restrictions = BindingRestrictions.GetTypeRestriction(target.Expression, target.LimitType);
return new DynamicMetaObject(methodCallExpression, restrictions, target.Value);
});
}
return DynamicMetaObject.Create(target.Value!, methodCallExpression);
}
public static object? GetDynamicMember(object value, string name, bool ignoreCase)
{
if (value == null)
{
throw new InvalidOperationException();
}
if (value is IDictionary<string, object> stringObjectDictionary)
{
return stringObjectDictionary[name];
}
if (value is IDictionary nonGenericDictionary)
{
return nonGenericDictionary[name];
}
var flags = BindingFlags.Instance | BindingFlags.Public;
if (ignoreCase)
{
flags |= BindingFlags.IgnoreCase;
}
var type = value.GetType();
var property = type.GetProperty(name, flags);
if (property == null)
{
throw new InvalidOperationException($"Unable to find property '{name}' on type '{type}'.");
}
return property.GetValue(value, null);
}
}
#endif