Skip to content

Commit

Permalink
Fix for Codeplex Issue #43
Browse files Browse the repository at this point in the history
  • Loading branch information
RupertAvery committed Jan 23, 2016
1 parent a03c144 commit 94370b1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
6 changes: 5 additions & 1 deletion ExpressionEvaluator/Parser/ExpressionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@ public static void LowerBoundInference(Type U, Type V, Dictionary<string, TypeVa
//Otherwise, no inferences are made.
}

private static bool IsDynamic(Expression expr)
{
return (expr.NodeType == ExpressionType.Dynamic) || expr.Type.IsDynamic() || (expr.NodeType == ExpressionType.Call && ((MethodCallExpression)expr).Method.ReturnTypeCustomAttributes.GetCustomAttributes(typeof(DynamicAttribute), true).Length > 0);
}

public static Expression GetProperty(Expression le, string membername)
{
Expand All @@ -291,7 +295,7 @@ public static Expression GetProperty(Expression le, string membername)

type = le.Type;
instance = le;
isDynamic = (le.NodeType == ExpressionType.Dynamic) || type.IsDynamic();
isDynamic = IsDynamic(le);

if (!isDynamic)
{
Expand Down
38 changes: 38 additions & 0 deletions UnitTests/DynamicTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections;
using System.Dynamic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand Down Expand Up @@ -98,5 +99,42 @@ public void DynamicValue()
ret = cc.Eval();
Assert.AreEqual(true, ret);
}

[TestMethod]
public void DynamicMethodReturnType()
{
var typeRegistry = new TypeRegistry();
dynamic obj = new Entity { Name = "MyName" };
typeRegistry.RegisterSymbol("obj", obj);
typeRegistry.RegisterSymbol("entity", new EntityLoader());

var entity = new EntityLoader();
Console.WriteLine("Expected: " + entity.GetParent(obj).Name);
ValidateExpression<string>(typeRegistry, "entity.GetParent(obj).Name");
}

private static void ValidateExpression<T>(TypeRegistry typeRegistry, string expression)
{
var compiler = new CompiledExpression<string> { TypeRegistry = typeRegistry, StringToParse = expression };
compiler.Compile();
var result = compiler.Eval();
Console.WriteLine(expression + " :: " + result);
}

}

public class Entity
{
public string Name { get; set; }
}

public class EntityLoader
{
public dynamic GetParent(dynamic entity)
{
return new Entity { Name = "Parent of " + entity.Name };
}
}


}

0 comments on commit 94370b1

Please sign in to comment.