forked from zzzprojects/System.Linq.Dynamic.Core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicGetMemberBinder.cs
More file actions
34 lines (30 loc) · 1.21 KB
/
DynamicGetMemberBinder.cs
File metadata and controls
34 lines (30 loc) · 1.21 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
#if !NET35
using System.Collections.Generic;
using System.Dynamic;
using System.Linq.Expressions;
using System.Reflection;
namespace System.Linq.Dynamic.Core
{
/// <summary>
/// Based on From SqlLinq by dkackman. https://github.com/dkackman/SqlLinq/blob/210b594e37f14061424397368ed750ce547c21e7/License.md
/// </summary>
/// <seealso cref="GetMemberBinder" />
internal class DynamicGetMemberBinder : GetMemberBinder
{
private static readonly PropertyInfo Indexer = typeof(IDictionary<string, object>).GetProperty("Item");
public DynamicGetMemberBinder(string name)
: base(name, true)
{
}
public override DynamicMetaObject FallbackGetMember(DynamicMetaObject target, DynamicMetaObject errorSuggestion)
{
IDictionary<string, object> dictionary = target.Value as IDictionary<string, object>;
if (dictionary == null)
{
throw new InvalidOperationException("Target object is not an ExpandoObject");
}
return DynamicMetaObject.Create(dictionary, Expression.MakeIndex(Expression.Constant(dictionary), Indexer, new Expression[] { Expression.Constant(Name) }));
}
}
}
#endif