Skip to content

Commit

Permalink
Merge pull request #55 from kendarorg/master
Browse files Browse the repository at this point in the history
Fix Issue "Nested dynamic objects #54"
  • Loading branch information
davideicardi authored Dec 14, 2016
2 parents 1862196 + f1f9e47 commit c662463
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/DynamicExpresso.Core/Parsing/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,7 @@ Expression GeneratePropertyOrFieldExpression(Type type, Expression instance, int
Expression.Field(instance, (FieldInfo)member);
}

if (IsDynamicType(type))
if (IsDynamicType(type) || IsDynamicExpression(instance))
return ParseDynamicProperty(type, instance, propertyOrFieldName);

throw CreateParseException(errorPos, ErrorMessages.UnknownPropertyOrField, propertyOrFieldName, GetTypeName(type));
Expand All @@ -882,7 +882,7 @@ Expression ParseMethodInvocation(Type type, Expression instance, int errorPos, s
if (methodInvocationExpression != null)
return methodInvocationExpression;

if (IsDynamicType(type))
if (IsDynamicType(type) || IsDynamicExpression(instance))
return ParseDynamicMethodInvocation(type, instance, methodName, args);

throw new NoApplicableMethodException(methodName, GetTypeName(type), errorPos);
Expand Down Expand Up @@ -1071,6 +1071,11 @@ static bool IsDynamicType(Type type)
return typeof(IDynamicMetaObjectProvider).IsAssignableFrom(type);
}

static bool IsDynamicExpression(Expression instance)
{
return instance != null && instance.NodeType == ExpressionType.Dynamic;
}

static Type GetNonNullableType(Type type)
{
return IsNullableType(type) ? type.GetGenericArguments()[0] : type;
Expand Down
27 changes: 27 additions & 0 deletions test/DynamicExpresso.UnitTest/DynamicTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ public void Get_Property_of_an_ExpandoObject()
Assert.AreEqual(dyn.Foo, interpreter.Eval("dyn.Foo"));
}

[Test]
public void Get_Property_of_a_nested_ExpandoObject()
{
dynamic dyn = new ExpandoObject();
dyn.Sub = new ExpandoObject();
dyn.Sub.Foo = "bar";

var interpreter = new Interpreter()
.SetVariable("dyn", dyn);

Assert.AreEqual(dyn.Sub.Foo, interpreter.Eval("dyn.Sub.Foo"));
}

//[Test]
//public void Set_Property_of_an_ExpandoObject()
//{
Expand Down Expand Up @@ -58,6 +71,20 @@ public void Invoke_Method_of_an_ExpandoObject()
Assert.AreEqual(dyn.Foo(), interpreter.Eval("dyn.Foo()"));
}


[Test]
public void Invoke_Method_of_a_nested_ExpandoObject()
{
dynamic dyn = new ExpandoObject();
dyn.Sub = new ExpandoObject();
dyn.Sub.Foo = new Func<string>(() => "bar");

var interpreter = new Interpreter()
.SetVariable("dyn", dyn);

Assert.AreEqual(dyn.Sub.Foo(), interpreter.Eval("dyn.Sub.Foo()"));
}

[Test]
public void Standard_methods_have_precedence_over_dynamic_methods()
{
Expand Down

0 comments on commit c662463

Please sign in to comment.