Click here to Skip to main content
15,886,724 members
Articles / Web Development / HTML

Dynamic Expresso

Rate me:
Please Sign up or sign in to vote.
4.91/5 (44 votes)
18 Jan 2015MIT11 min read 80.4K   935   72  
Generate LambdaExpression or function delegate on the fly without compilation.
using System;
using System.Linq;
using NUnit.Framework;
using System.Collections.Generic;

namespace DynamicExpresso.UnitTest
{
	[TestFixture]
	public class ExtensionsMethodsTest
	{
		[Test]
		public void Invoke_extension_method()
		{
			var x = new MyClass();

			var target = new Interpreter()
									.Reference(typeof(TestExtensionsMethods))
									.SetVariable("x", x);

			Assert.AreEqual(x.HelloWorld(), target.Eval("x.HelloWorld()"));
			Assert.AreEqual(x.HelloWorldWithParam(DateTime.Now), target.Eval("x.HelloWorldWithParam(DateTime.Now)"));
		}

		[Test]
		public void Invoke_generic_extension_method()
		{
			var x = new MyClass();

			var target = new Interpreter()
									.Reference(typeof(TestExtensionsMethods))
									.SetVariable("x", x);

			Assert.AreEqual(x.GenericHello(), target.Eval("x.GenericHello()"));
		}

		[Test]
		public void Invoke_generic_parameter_extension_method()
		{
			var x = new MyClass[0];

			var target = new Interpreter()
									.Reference(typeof(TestExtensionsMethods))
									.SetVariable("x", x);

			Assert.AreEqual(x.GenericParamHello(), target.Eval("x.GenericParamHello()"));
		}

		[Test]
		public void Invoke_generic_with_2_parameters_and_output_extension_method()
		{
			var x = new Dictionary<string, MyClass>();
			x.Add("i1", new MyClass());

			var target = new Interpreter()
									.Reference(typeof(TestExtensionsMethods))
									.SetVariable("x", x);

			Assert.AreEqual(x.GenericWith2Params(), target.Eval("x.GenericWith2Params()"));
		}

		[Test]
		public void Invoke_generic_mixed_parameter_extension_method()
		{
			var x = new Dictionary<string, MyClass>();

			var target = new Interpreter()
									.Reference(typeof(TestExtensionsMethods))
									.SetVariable("x", x);

			Assert.AreEqual(x.GenericMixedParamHello(), target.Eval("x.GenericMixedParamHello()"));
		}

		public class MyClass
		{
		}
	}

	public static class TestExtensionsMethods
	{
		public static string HelloWorld(this ExtensionsMethodsTest.MyClass test)
		{
			return "Hello Test Class";
		}

		public static string HelloWorldWithParam(this ExtensionsMethodsTest.MyClass test, DateTime date)
		{
			return "Hello Test Class " + date.Year;
		}

		public static string GenericHello<T>(this T test)
			where T : ExtensionsMethodsTest.MyClass
		{
			return "Hello with generic!";
		}

		public static string GenericParamHello<T>(this IEnumerable<T> test)
			where T : ExtensionsMethodsTest.MyClass
		{
			return "Hello with generic param!";
		}

		public static string GenericMixedParamHello<T>(this IDictionary<string, T> test)
			where T : ExtensionsMethodsTest.MyClass
		{
			return "Hello with generic param!";
		}

		public static T2 GenericWith2Params<T1, T2>(this IDictionary<T1, T2> test)
				where T2 : ExtensionsMethodsTest.MyClass
		{
			return test.First().Value;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Software Developer
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions