Skip to content

Commit 44c4dfe

Browse files
Fix for issue: #397
1 parent 8d88836 commit 44c4dfe

File tree

5 files changed

+68
-40
lines changed

5 files changed

+68
-40
lines changed

Z.Dynamic.Core.Lab/Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class Program
66
{
77
static void Main(string[] args)
88
{
9-
Request_DynamicLinqType.Execute();
9+
Request_Dictionary.Execute();
1010
}
1111
}
1212
}
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq.Dynamic.Core;
4+
using System.Linq.Dynamic.Core.CustomTypeProviders;
5+
using System.Linq.Expressions;
6+
using System.Text;
7+
8+
namespace Z.Dynamic.Core.Lab
9+
{
10+
public class Request_Dictionary
11+
{
12+
public static void Execute()
13+
{
14+
object CreateDicParameter(string name) => new Dictionary<string, object>
15+
{{"Name", new Dictionary<string, object> {{"FirstName", name }}}};
16+
17+
var config = new ParsingConfig()
18+
{
19+
CustomTypeProvider = new DefaultDynamicLinqCustomTypeProvider()
20+
};
21+
var parType = new Dictionary<string, object>().GetType();
22+
var lambda = DynamicExpressionParser.ParseLambda(config, new[] { Expression.Parameter(parType, "item") }, typeof(object), "item.Name.FirstName").Compile();
23+
24+
var x1 = lambda.DynamicInvoke(CreateDicParameter("Julio"));
25+
var x2 = lambda.DynamicInvoke(CreateDicParameter("John"));
26+
}
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Description: C# Eval Function | Evaluate, Compile and Execute C# code and expression at runtime.
2+
// Website & Documentation: https://github.com/zzzprojects/Eval-Expression.NET
3+
// Forum & Issues: https://github.com/zzzprojects/Eval-Expression.NET/issues
4+
// License: https://github.com/zzzprojects/Eval-Expression.NET/blob/master/LICENSE
5+
// More projects: http://www.zzzprojects.com/
6+
// Copyright © ZZZ Projects Inc. 2014 - 2017. All rights reserved.
7+
8+
using System.Collections.Generic;
9+
10+
namespace System.Linq.Dynamic.Core
11+
{
12+
internal static class Dynamic
13+
{
14+
internal static object DynamicIndex(object obj, string name)
15+
{
16+
// CAUTION: This method is called via reflection, so even with 0 reference, the method is used
17+
// var method = typeof(Dynamic).GetMethod("DynamicIndex", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
18+
// return Expression.Call(null, method, instance, Expression.Constant(id));
19+
20+
// TBD: If we want to really support Expando Object for properties & method
21+
// - We will need to add a reference to: Microsoft.CSharp.dll
22+
// - Copy source from Z.Expressions.Eval\Z.Expressions.Compiler.Shared\CodeCompiler\CSharp\Compiler\Dynamic\DynamicIndexer.cs and other files
23+
24+
// At this moment, this is only a very quick fix for issue: https://github.com/zzzprojects/System.Linq.Dynamic.Core/issues/397
25+
// To replace DynamicGetMemberBinder.cs old logic that was caching the result
26+
27+
var dictionary = obj as IDictionary<string, object>;
28+
if (dictionary == null)
29+
{
30+
throw new InvalidOperationException("Target object is not an ExpandoObject");
31+
}
32+
33+
return dictionary[name];
34+
}
35+
}
36+
}

src/System.Linq.Dynamic.Core/DynamicGetMemberBinder.cs

-34
This file was deleted.

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -1682,13 +1682,11 @@ Expression ParseMemberAccess(Type type, Expression instance)
16821682
{
16831683
return Expression.Field(instance, field);
16841684
}
1685-
1686-
#if !NET35 && !UAP10_0 && !NETSTANDARD1_3
16871685
if (type == typeof(object))
16881686
{
1689-
return Expression.Dynamic(new DynamicGetMemberBinder(id), type, instance);
1687+
var method = typeof(Dynamic).GetMethod("DynamicIndex", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
1688+
return Expression.Call(null, method, instance, Expression.Constant(id));
16901689
}
1691-
#endif
16921690
if (!_parsingConfig.DisableMemberAccessToIndexAccessorFallback && instance != null)
16931691
{
16941692
MethodInfo indexerMethod = instance.Type.GetMethod("get_Item", new[] { typeof(string) });
@@ -1938,7 +1936,7 @@ static bool TryGetMemberName(Expression expression, out string memberName)
19381936
{
19391937
memberExpression = (expression as BinaryExpression).Left as MemberExpression;
19401938
}
1941-
1939+
19421940
if (memberExpression != null)
19431941
{
19441942
memberName = memberExpression.Member.Name;

0 commit comments

Comments
 (0)