public void CacheableMethodPocoGetValue4argsTimed() { var tValue = "test 123 45 string"; var tCachedInvoke = new CacheableInvocation(InvocationKind.InvokeMember, "IndexOf", 4); Timer.Action1 = (() => { var tOut = tCachedInvoke.Invoke(tValue, "45", 0, 14, StringComparison.InvariantCulture); }); var tMethodInfo = tValue.GetType().GetMethod("IndexOf", new Type[] { typeof(string), typeof(int), typeof(int), typeof(StringComparison) }); Timer.Action2 = (() => { var tOut = tMethodInfo.Invoke(tValue, new object[] { "45", 0, 14, StringComparison.InvariantCulture }); }); var elapsed = Timer.Go(); Console.WriteLine("Impromptu: " + elapsed.Item1); Console.WriteLine("Refelection: " + elapsed.Item2); Console.WriteLine("Impromptu VS Reflection: {0}", TimeIt.RelativeSpeed(elapsed)); Assert.Less(elapsed.Item1, elapsed.Item2); }
public void TestCacheableConstructValueType() { var tCachedInvoke = new CacheableInvocation(InvocationKind.Constructor, argCount: 3); dynamic tCast = tCachedInvoke.Invoke(typeof(DateTime), 2009, 1, 20); Assert.AreEqual(20, tCast.Day); }
public void CacheableMethodPocoGetValuePassNullDoubleCallTimed() { var tValue = new OverloadingMethPoco(); var tCachedInvoke = new CacheableInvocation(InvocationKind.InvokeMember, "Func", 1); Timer.Action1 = (() => { var tOut = tCachedInvoke.Invoke(tValue, null); var tOut2 = tCachedInvoke.Invoke(tValue, 2); }); var tMethodInfo = tValue.GetType().GetMethod("Func", new Type[] { typeof(object) }); var tMethodInfo2 = tValue.GetType().GetMethod("Func", new Type[] { typeof(int) }); Timer.Action2 = (() => { var tOut = tMethodInfo.Invoke(tValue, new object[] { null }); var tOut2 = tMethodInfo2.Invoke(tValue, new object[] { 2 }); }); var elapsed = Timer.Go(); Console.WriteLine("Impromptu: " + elapsed.Item1); Console.WriteLine("Refelection: " + elapsed.Item2); Console.WriteLine("Impromptu VS Reflection: {0}", TimeIt.RelativeSpeed(elapsed)); Assert.Less(elapsed.Item1, elapsed.Item2); }
public void TestCacheableMethodPocoGetValuePassNullDoubleCallTimed() { var tValue = new OverloadingMethPoco(); var tCachedInvoke = new CacheableInvocation(InvocationKind.InvokeMember, "Func", 1); var tInteration = 500000; var tWatch = TimeIt.Go(() => { var tOut = tCachedInvoke.Invoke(tValue, null); var tOut2 = tCachedInvoke.Invoke(tValue, 2); }, tInteration); var tMethodInfo = tValue.GetType().GetMethod("Func", new Type[] { typeof(object) }); var tMethodInfo2 = tValue.GetType().GetMethod("Func", new Type[] { typeof(int) }); var tWatch2 = TimeIt.Go(() => { var tOut = tMethodInfo.Invoke(tValue, new object[] { null }); var tOut2 = tMethodInfo2.Invoke(tValue, new object[] { 2 }); }, tInteration); TestContext.WriteLine("Impromptu: " + tWatch.Elapsed); TestContext.WriteLine("Reflection: " + tWatch2.Elapsed); TestContext.WriteLine("Impromptu VS Reflection: {0:0.0} x faster", (double)tWatch2.Elapsed.Ticks / tWatch.Elapsed.Ticks); Assert.Less(tWatch.Elapsed, tWatch2.Elapsed); }
public void TestCacheablePrimativeDateTimeObjectNullableAndGuidNoParams() { var tCachedInvoke = new CacheableInvocation(InvocationKind.Constructor); dynamic tCast = tCachedInvoke.Invoke(typeof(Int32)); Assert.AreEqual(default(Int32), tCast); tCast = tCachedInvoke.Invoke(typeof(DateTime)); Assert.AreEqual(default(DateTime), tCast); tCast = tCachedInvoke.Invoke(typeof(List <string>)); Assert.AreEqual(typeof(List <string>), tCast.GetType()); tCast = tCachedInvoke.Invoke(typeof(object)); Assert.AreEqual(typeof(object), tCast.GetType()); tCast = tCachedInvoke.Invoke(typeof(Nullable <Int32>)); Assert.AreEqual(null, tCast); tCast = tCachedInvoke.Invoke(typeof(Guid)); Assert.AreEqual(default(Guid), tCast); }
public void TestCacheableDyanmicSetAndPocoSetAndSetNull() { dynamic tExpando = new ExpandoObject(); var tSetValueD = "4"; var tCachedInvoke = new CacheableInvocation(InvocationKind.Set, "Prop1"); tCachedInvoke.Invoke((object)tExpando, tSetValueD); Assert.AreEqual(tSetValueD, tExpando.Prop1); var tPoco = new PropPoco(); var tSetValue = "1"; tCachedInvoke.Invoke(tPoco, tSetValue); Assert.AreEqual(tSetValue, tPoco.Prop1); String tSetValue2 = null; tCachedInvoke.Invoke(tPoco, tSetValue2); Assert.AreEqual(tSetValue2, tPoco.Prop1); }
public void TestCacheableGet() { var tCached = new CacheableInvocation(InvocationKind.Get, "Prop1"); var tSetValue = "1"; var tAnon = new PropPoco { Prop1 = tSetValue }; var tOut = tCached.Invoke(tAnon); Assert.AreEqual(tSetValue, tOut); var tSetValue2 = "2"; tAnon = new PropPoco { Prop1 = tSetValue2 }; var tOut2 = tCached.Invoke(tAnon); Assert.AreEqual(tSetValue2, tOut2); }
/// <summary> /// Initializes a new instance of the <see cref="ImpromptuRelayCommand"/> class. /// </summary> /// <param name="executeTarget">The execute target.</param> /// <param name="executeName">Name of the execute.</param> /// <param name="setup">The setup which has the on error event</param> public ImpromptuRelayCommand(object executeTarget, String_OR_InvokeMemberName executeName, ISetupViewModel setup = null) { _executeTarget = executeTarget; _setup = setup; _executeInvoke = new CacheableInvocation(InvocationKind.InvokeMemberAction, executeName, 1); _executeInvokeNoArg = new CacheableInvocation(InvocationKind.InvokeMemberAction, executeName, 0); }
public void CacheableGetStaticTimed() { var tStaticType = typeof(DateTime); var tContext = InvokeContext.CreateStatic(tStaticType); var tCachedInvoke = new CacheableInvocation(InvocationKind.Get, "Today", context: tContext); Timer.Action1 = (() => { var tOut = tCachedInvoke.Invoke(tStaticType); }); var tMethodInfo = typeof(DateTime).GetProperty("Today").GetGetMethod(); Timer.Action2 = (() => { var tOut = tMethodInfo.Invoke(tStaticType, new object[] { }); }); var elapsed = Timer.Go(3 * TimeIt.Million); Console.WriteLine("Impromptu: " + elapsed.Item1); Console.WriteLine("Refelection: " + elapsed.Item2); Console.WriteLine("Impromptu VS Reflection: {0}", TimeIt.RelativeSpeed(elapsed)); Assert.Less(elapsed.Item1, elapsed.Item2); }
public void TestCacheableExposePrivateMethodViaType() { var tTest = new TestWithPrivateMethod(); var tCachedInvoke = new CacheableInvocation(InvocationKind.InvokeMember, "Test", context: typeof(TestWithPrivateMethod)); Assert.AreEqual(3, tCachedInvoke.Invoke(tTest)); }
public void TestCacheablePocoSubtractAssign() { var tPoco = new PocoEvent(); bool tTest = false; var tEvent = new EventHandler <EventArgs>((@object, args) => { tTest = true; }); var tCachedInvoke = new CacheableInvocation(InvocationKind.SubtractAssign, "Event"); tPoco.Event += tEvent; tCachedInvoke.Invoke(tPoco, tEvent); tPoco.OnEvent(null, null); Assert.AreEqual(false, tTest); tCachedInvoke.Invoke(tPoco, tEvent);//Test Second Time var tPoco2 = new PropPoco() { Event = 3 }; tCachedInvoke.Invoke(tPoco2, 4); Assert.AreEqual(-1, tPoco2.Event); }
public void TestCacheableMethodPocoGetValuePassNullTimed() { #if DEBUG Assert.Ignore("Visual Studio slows down dynamic too much in debug mode"); #endif var tValue = new OverloadingMethPoco(); var tInteration = 1000000; var tCachedInvoke = new CacheableInvocation(InvocationKind.InvokeMember, "Func", argCount: 1); var tWatch = TimeIt.Go(() => { var tOut = tCachedInvoke.Invoke(tValue, null); }, tInteration); var tMethodInfo = tValue.GetType().GetMethod("Func", new Type[] { typeof(object) }); var tWatch2 = TimeIt.Go(() => { var tOut = tMethodInfo.Invoke(tValue, new object[] { null }); }, tInteration); TestContext.WriteLine("Impromptu: " + tWatch.Elapsed); TestContext.WriteLine("Reflection: " + tWatch2.Elapsed); TestContext.WriteLine("Impromptu VS Reflection: {0:0.0} x faster", (double)tWatch2.Elapsed.Ticks / tWatch.Elapsed.Ticks); Assert.Less(tWatch.Elapsed, tWatch2.Elapsed); }
public void CacheableGetValueTimed() { var tSetValue = "1"; var tAnon = new PropPoco() { Prop1 = tSetValue }; var tInvoke = new CacheableInvocation(InvocationKind.Get, "Prop1"); Timer.Action1 = () => { var tOut = tInvoke.Invoke(tAnon); }; var tPropertyInfo = tAnon.GetType().GetProperty("Prop1"); Timer.Action2 = () => { var tOut = tPropertyInfo.GetValue(tAnon, null); }; var elapsed = Timer.Go(2 * TimeIt.Million); Console.WriteLine("Impromptu: " + elapsed.Item1); Console.WriteLine("Refelection: " + elapsed.Item2); Console.WriteLine("Impromptu VS Reflection: {0}", TimeIt.RelativeSpeed(elapsed)); Assert.Less(elapsed.Item1, elapsed.Item2); }
public void TestCacheableExposePrivateMethodViaInstance() { var tTest = new TestWithPrivateMethod(); var tCachedInvoke = new CacheableInvocation(InvocationKind.InvokeMember, "Test", context: tTest); Assert.That(tCachedInvoke.Invoke(tTest), Is.EqualTo(3)); }
public void TestCacheableDoNotExposePrivateMethod() { var tTest = new TestWithPrivateMethod(); var tCachedInvoke = new CacheableInvocation(InvocationKind.InvokeMember, "Test"); AssertException <RuntimeBinderException>(() => tCachedInvoke.Invoke(tTest)); }
public void CacheableMethodPocoGetValuePassNullTimed() { #if DEBUG Assert.Ignore("Visual Studio slows down dynamic too much in debug mode"); #endif var tValue = new OverloadingMethPoco(); var tCachedInvoke = new CacheableInvocation(InvocationKind.InvokeMember, "Func", argCount: 1); Timer.Action1 = (() => { var tOut = tCachedInvoke.Invoke(tValue, null); }); var tMethodInfo = tValue.GetType().GetMethod("Func", new Type[] { typeof(object) }); Timer.Action2 = (() => { var tOut = tMethodInfo.Invoke(tValue, new object[] { null }); }); var elapsed = Timer.Go(); Console.WriteLine("Impromptu: " + elapsed.Item1); Console.WriteLine("Refelection: " + elapsed.Item2); Console.WriteLine("Impromptu VS Reflection: {0}", TimeIt.RelativeSpeed(elapsed)); Assert.Less(elapsed.Item1, elapsed.Item2); }
/// <summary> /// Initializes a new instance of the <see cref="ImpromptuRelayCommand"/> class. /// </summary> /// <param name="executeTarget">The execute target.</param> /// <param name="executeName">Name of the execute method.</param> /// <param name="canExecuteTarget">The can execute target.</param> /// <param name="canExecuteName">Name of the can execute method.</param> public ImpromptuRelayCommand(object executeTarget, String_OR_InvokeMemberName executeName, object canExecuteTarget, String_OR_InvokeMemberName canExecuteName, ISetupViewModel setup = null) : this(executeTarget, executeName, setup) { _canExecuteTarget = canExecuteTarget; _canExecuteInvoke = new CacheableInvocation(InvocationKind.InvokeMember, canExecuteName, 1); _canExecuteInvokeGet = new CacheableInvocation(InvocationKind.Get, canExecuteName); _canExecuteInvokeNoArg = new CacheableInvocation(InvocationKind.InvokeMember, canExecuteName); }
public void TestCacheableStaticGet() { var tCached = new CacheableInvocation(InvocationKind.Get, "Today", context: new StaticContext(typeof(DateTime))); var tDate = tCached.Invoke(typeof(DateTime)); Assert.AreEqual(DateTime.Today, tDate); }
public void TestCacheableStaticDateTimeMethod() { object tDateDyn = "01/20/2009"; var tCachedInvoke = new CacheableInvocation(InvocationKind.InvokeMember, "Parse", 1, context: new StaticContext(typeof(DateTime))); var tDate = tCachedInvoke.Invoke(typeof(DateTime), tDateDyn); Assert.AreEqual(new DateTime(2009, 1, 20), tDate); }
public void TestCacheableStaticCall() { var tCached = new CacheableInvocation(InvocationKind.InvokeMember, "Create".WithGenericArgs(typeof(bool)), argCount: 1, context: typeof(StaticType).WithStaticContext()); var tOut = tCached.Invoke(typeof(StaticType), 1); Assert.AreEqual(false, tOut); }
public void TestCacheableStaticCall() { var tCached = new CacheableInvocation(InvocationKind.InvokeMember, new InvokeMemberName("Create", typeof(bool)), argCount: 1, context: new StaticContext(typeof(StaticType))); var tOut = tCached.Invoke(typeof(StaticType), 1); Assert.AreEqual(false, tOut); }
internal DependObject(ImpromptuViewModel parent, string property, FireOnPropertyChangedDependencyAware onChange) { _parent = parent; _property = property; _onChange = onChange; _dependency = new Dependency(_parent, _property); _unDependency = new UnDependency(_parent, _property); _getProprty = new CacheableInvocation(InvocationKind.Get, _property); _setProprty = new CacheableInvocation(InvocationKind.Set, _property); }
public void TestCacheableStaticSet() { int tValue = 12; var tCachedInvoke = new CacheableInvocation(InvocationKind.Set, "TestSet", context: typeof(StaticType).WithStaticContext()); tCachedInvoke.Invoke(typeof(StaticType), tValue); Assert.AreEqual(tValue, StaticType.TestSet); }
public void TestCacheableConstructOptional() { var tCachedInvoke = new CacheableInvocation(InvocationKind.Constructor, argCount: 1, argNames: new[] { "three" }); var tCast = (PocoOptConstructor)tCachedInvoke.Invoke(typeof(PocoOptConstructor), "3"); Assert.AreEqual("-1", tCast.One); Assert.AreEqual("-2", tCast.Two); Assert.AreEqual("3", tCast.Three); }
public void TestCacheableImplicitConvert() { var tEl = 45; var tCachedInvoke = CacheableInvocation.CreateConvert(typeof(long)); var tCast = tCachedInvoke.Invoke(tEl); Assert.AreEqual(typeof(long), tCast.GetType()); }
public void TestCacheableStaticDateTimeMethod() { var @static = InvokeContext.CreateStatic; object tDateDyn = "01/20/2009"; var tCachedInvoke = new CacheableInvocation(InvocationKind.InvokeMember, "Parse", argCount: 2, context: @static(typeof(DateTime))); var tDate = tCachedInvoke.Invoke(typeof(DateTime), new object[] { tDateDyn, CultureInfo.GetCultureInfo("en-US") }); Assert.AreEqual(new DateTime(2009, 1, 20), tDate); }
public void TestCacheableStaticSet() { int tValue = 12; var @static = InvokeContext.CreateStatic; var tCachedInvoke = new CacheableInvocation(InvocationKind.Set, "TestSet", context: @static(typeof(StaticType))); tCachedInvoke.Invoke(typeof(StaticType), tValue); Assert.AreEqual(tValue, StaticType.TestSet); }
public void TestCacheableConstruct() { var tCachedInvoke = new CacheableInvocation(InvocationKind.Constructor, argCount: 1); dynamic tCast = tCachedInvoke.Invoke(typeof(List <object>), new object[] { new string[] { "one", "two", "three" } }); Assert.AreEqual("two", tCast[1]); }
public void TestConvertCacheable() { var tEl = new XElement("Test", "45"); var tCacheInvoke = new CacheableInvocation(InvocationKind.Convert, convertType: typeof(int), convertExplicit: true); var tCast = tCacheInvoke.Invoke(tEl); Assert.AreEqual(typeof(int), tCast.GetType()); Assert.AreEqual(45, tCast); }
public void TestCacheableConstruct() { var tCachedInvoke = new CacheableInvocation(InvocationKind.Constructor, argCount: 1); dynamic tCast = tCachedInvoke.Invoke(typeof(List<object>), new object[] { new string[] {"one", "two", "three"} }); Assert.AreEqual("two", tCast[1]); }
public void TestCacheableStaticCall() { var @static = InvokeContext.CreateStatic; var generic = InvokeMemberName.Create; var tCached = new CacheableInvocation(InvocationKind.InvokeMember, generic("Create", new[] { typeof(bool) }), argCount: 1, context: @static(typeof(StaticType))); var tOut = tCached.Invoke(typeof(StaticType), 1); Assert.AreEqual(false, tOut); }
public void CacheableConstructorTimed() { var tCachedInvoke = new CacheableInvocation(InvocationKind.Constructor, argCount: 1); Timer.Action1 = (() => { var tOut = tCachedInvoke.Invoke(typeof(Tuple<string>), "Test"); }); Timer.Action2 = (() => { var tOut = Activator.CreateInstance(typeof(Tuple<string>), "Test"); }); var elapsed = Timer.Go(); Console.WriteLine("Impromptu: " + elapsed.Item1); Console.WriteLine("Refelection: " + elapsed.Item2); Console.WriteLine("Impromptu VS Reflection: {0}", TimeIt.RelativeSpeed(elapsed)); Assert.Less(elapsed.Item1, elapsed.Item2); }
public void CachableConstructorNoARgTimed() { var tCachedInvoke = new CacheableInvocation(InvocationKind.Constructor); Timer.Action1=(() => { var tOut = tCachedInvoke.Invoke(typeof(List<string>)); }); Timer.Action2=(() => { var tOut = Activator.CreateInstance(typeof(List<string>)); }); Timer.Action3=(() => { var tOut = Activator.CreateInstance<List<string>>(); }); var elapsed = Timer.GoThree(); Console.WriteLine("Impromptu: " + elapsed.Item1); Console.WriteLine("Refelection: " + elapsed.Item2); Console.WriteLine("Refelection Generic: " + elapsed.Item3); Console.WriteLine("Impromptu VS Reflection: {0:0.0} x slower", (double)elapsed.Item1.Ticks / elapsed.Item2.Ticks); Assert.Ignore("I don't think this is beatable at the moment"); Assert.Less(elapsed.Item1, elapsed.Item2); }
public void CacheableGetStaticTimed() { var tStaticType = typeof(DateTime); var tContext = InvokeContext.CreateStatic(tStaticType); var tCachedInvoke = new CacheableInvocation(InvocationKind.Get, "Today", context: tContext); Timer.Action1=(() => { var tOut = tCachedInvoke.Invoke(tStaticType); }); var tMethodInfo = typeof(DateTime).GetProperty("Today").GetGetMethod(); Timer.Action2=(() => { var tOut = tMethodInfo.Invoke(tStaticType, new object[] { }); }); var elapsed = Timer.Go(3 * TimeIt.Million); Console.WriteLine("Impromptu: " + elapsed.Item1); Console.WriteLine("Refelection: " + elapsed.Item2); Console.WriteLine("Impromptu VS Reflection: {0}", TimeIt.RelativeSpeed(elapsed)); Assert.Less(elapsed.Item1, elapsed.Item2); }
public void TestCacheableGetDynamic() { var tSetValue = "1"; dynamic tExpando = new ExpandoObject(); tExpando.Test = tSetValue; var tCached = new CacheableInvocation(InvocationKind.Get, "Test"); var tOut = tCached.Invoke((object)tExpando); Assert.AreEqual(tSetValue, tOut); }
public void TestCachedMethodStaticOverloadingPassAndGetValue() { var tPoco = new OverloadingMethPoco(); var tValue = 1; var tCachedInvoke = new CacheableInvocation(InvocationKind.InvokeMember, "Func", argCount: 1); var tOut = tCachedInvoke.Invoke(tPoco, tValue); Assert.AreEqual("int", tOut); Assert.AreEqual("int", (object)tOut); //should still be int because this uses runtime type var tOut2 = tCachedInvoke.Invoke(tPoco, 1m); Assert.AreEqual("object", tOut2); var tOut3 = tCachedInvoke.Invoke(tPoco, new { Anon = 1 }); Assert.AreEqual("object", tOut3); }
public void TestCacheableSetIndexer() { dynamic tSetValue = "3"; var tList = new List<string> { "1", "2" }; var tCachedInvoke = new CacheableInvocation(InvocationKind.SetIndex, argCount: 2); tCachedInvoke.Invoke(tList, 0, tSetValue); Assert.AreEqual(tSetValue, tList[0]); }
public void TestCacheableIndexer() { var tStrings = new[] { "1", "2" }; var tCachedInvoke = new CacheableInvocation(InvocationKind.GetIndex, argCount: 1); var tOut = (string)tCachedInvoke.Invoke(tStrings, 0); Assert.AreEqual(tStrings[0], tOut); var tOut2 = (string)tCachedInvoke.Invoke(tStrings, 1); Assert.AreEqual(tStrings[1], tOut2); var tInts = new int[] { 3, 4 }; var tOut3 = (int)tCachedInvoke.Invoke(tInts, 0); Assert.AreEqual(tInts[0], tOut3); var tOut4 = (int)tCachedInvoke.Invoke(tInts, 1); Assert.AreEqual(tInts[1], tOut4); var tList = new List<string> { "5", "6" }; var tOut5 = (string)tCachedInvoke.Invoke(tList, 0); Assert.AreEqual(tList[0], tOut5); var tOut6 = (string)tCachedInvoke.Invoke(tList, 0); Assert.AreEqual(tList[0], tOut6); }
public void TestCacheableStaticDateTimeMethod() { var @static = InvokeContext.CreateStatic; object tDateDyn = "01/20/2009"; var tCachedInvoke = new CacheableInvocation(InvocationKind.InvokeMember, "Parse", 1, context: @static(typeof(DateTime))); var tDate = tCachedInvoke.Invoke(typeof(DateTime), tDateDyn); Assert.AreEqual(new DateTime(2009, 1, 20), tDate); }
public void TestCacheableMethodPocoOverloadingPassAndGetValueArgOptional() { var tPoco = new OverloadingMethPoco(); var tValue = 1; var tCachedIvnocation = new CacheableInvocation(InvocationKind.InvokeMember, "Func", argCount: 1, argNames: new[] { "two" }); var tOut = tCachedIvnocation.Invoke(tPoco, tValue); Assert.AreEqual("object named", tOut); Assert.AreEqual("object named", (object)tOut); }
public void CacheableGetValueTimed() { var tSetValue = "1"; var tAnon = new PropPoco() { Prop1 = tSetValue }; var tInvoke = new CacheableInvocation(InvocationKind.Get, "Prop1"); Timer.Action1 = () => { var tOut = tInvoke.Invoke(tAnon); }; var tPropertyInfo = tAnon.GetType().GetProperty("Prop1"); Timer.Action2 = () => { var tOut = tPropertyInfo.GetValue(tAnon, null); }; var elapsed = Timer.Go(2*TimeIt.Million); Console.WriteLine("Impromptu: " + elapsed.Item1); Console.WriteLine("Refelection: " + elapsed.Item2); Console.WriteLine("Impromptu VS Reflection: {0}", TimeIt.RelativeSpeed(elapsed)); Assert.Less(elapsed.Item1, elapsed.Item2); }
public void TestCacheablePrimativeDateTimeObjectNullableAndGuidNoParams() { var tCachedInvoke = new CacheableInvocation(InvocationKind.Constructor); dynamic tCast = tCachedInvoke.Invoke(typeof(Int32)); Assert.AreEqual(default(Int32), tCast); tCast = tCachedInvoke.Invoke(typeof(DateTime)); Assert.AreEqual(default(DateTime), tCast); tCast = tCachedInvoke.Invoke(typeof(List<string>)); Assert.AreEqual(typeof(List<string>), tCast.GetType()); tCast = tCachedInvoke.Invoke(typeof(object)); Assert.AreEqual(typeof(object), tCast.GetType()); tCast = tCachedInvoke.Invoke(typeof(Nullable<Int32>)); Assert.AreEqual(null, tCast); tCast = tCachedInvoke.Invoke(typeof(Guid)); Assert.AreEqual(default(Guid), tCast); }
public void TestCacheableDynamicAddAssign() { var tDyanmic = Build.NewObject(Prop2: 3, Event: null, OnEvent: new ThisAction<object, EventArgs>((@this, obj, args) => @this.Event(obj, args))); var tDynamic2 = Build.NewObject(Event: 3); bool tTest = false; var tCachedInvoke = new CacheableInvocation(InvocationKind.AddAssign, "Event"); tCachedInvoke.Invoke((object)tDyanmic, new EventHandler<EventArgs>((@object, args) => { tTest = true; })); tDyanmic.OnEvent(null, null); Assert.AreEqual(true, tTest); tCachedInvoke.Invoke((object)tDynamic2, 4); Assert.AreEqual(7, tDynamic2.Event); }
public void TestCacheableMethodDynamicPassVoid() { var tTest = "Wrong"; var tValue = "Correct"; dynamic tExpando = new ExpandoObject(); tExpando.Action = new Action<string>(it => tTest = it); var tCachedInvoke = new CacheableInvocation(InvocationKind.InvokeMemberAction, "Action", argCount: 1); tCachedInvoke.Invoke((object)tExpando, tValue); Assert.AreEqual(tValue, tTest); }
public void TestCacheableMethodDynamicUnknowns() { var tTest = "Wrong"; var tValue = "Correct"; dynamic tExpando = new ExpandoObject(); tExpando.Action = new Action<string>(it => tTest = it); tExpando.Func = new Func<string, string>(it => it); var tCachedInvoke = new CacheableInvocation(InvocationKind.InvokeMemberUnknown, "Action", argCount: 1); tCachedInvoke.Invoke((object)tExpando, tValue); Assert.AreEqual(tValue, tTest); var tCachedInvoke2 = new CacheableInvocation(InvocationKind.InvokeMemberUnknown, "Func", argCount: 1); var Test2 = tCachedInvoke2.Invoke((object)tExpando, tValue); Assert.AreEqual(tValue, Test2); }
/// <summary> /// Provides the implementation for operations that invoke an object. Classes derived from the <see /// cref="T:System.Dynamic.DynamicObject" /> class can override this method to specify dynamic behavior for operations such as invoking an object or a delegate. /// </summary> /// <param name="binder"> Provides information about the invoke operation. </param> /// <param name="args"> The arguments that are passed to the object during the invoke operation. For example, for the sampleObject(100) operation, where sampleObject is derived from the <see /// cref="T:System.Dynamic.DynamicObject" /> class, <paramref name="args[0]" /> is equal to 100. </param> /// <param name="result"> The result of the object invocation. </param> /// <returns> true if the operation is successful; otherwise, false. If this method returns false, the run-time binder of the language determines the behavior. (In most cases, a language-specific run-time exception is thrown. </returns> public override bool TryInvoke(InvokeBinder binder, object[] args, out object result) { var tNamedArgs = Util.NameArgsIfNecessary(binder.CallInfo, args); var tNewArgs = _args.Concat(tNamedArgs).ToArray(); if (_totalArgCount.HasValue && (_totalArgCount - Args.Length - args.Length > 0)) //Not Done currying { result = new Currying(Target, MemberName, tNewArgs, TotalArgCount, InvocationKind); return true; } var tInvokeDirect = String.IsNullOrWhiteSpace(_memberName); var tDel = _target as Delegate; if (tInvokeDirect && binder.CallInfo.ArgumentNames.Count == 0 && _target is Delegate) //Optimization for direct delegate calls { result = tDel.FastDynamicInvoke(tNewArgs); return true; } Invocation tInvocation; if (binder.CallInfo.ArgumentNames.Count == 0) //If no argument names we can cache the callsite { CacheableInvocation tCacheableInvocation; if (!_cacheableInvocation.TryGetValue(tNewArgs.Length, out tCacheableInvocation)) { tCacheableInvocation = new CacheableInvocation(InvocationKind, _memberName, argCount: tNewArgs.Length, context: _target); _cacheableInvocation[tNewArgs.Length] = tCacheableInvocation; } tInvocation = tCacheableInvocation; } else { tInvocation = new Invocation(InvocationKind, _memberName); } result = tInvocation.Invoke(_target, tNewArgs); return true; }
public void TestCacheableMethodDynamicPassAndGetValue() { dynamic tExpando = new ExpandoObject(); tExpando.Func = new Func<int, string>(it => it.ToString()); var tValue = 1; var tCachedInvoke = new CacheableInvocation(InvocationKind.InvokeMember, "Func", 1); var tOut = tCachedInvoke.Invoke((object)tExpando, tValue); Assert.AreEqual(tValue.ToString(), tOut); }
public void TestCacheableIsEventAndIsNotEvent() { object tPoco = new PocoEvent(); var tCachedInvoke = new CacheableInvocation(InvocationKind.IsEvent, "Event"); var tResult = tCachedInvoke.Invoke(tPoco); Assert.AreEqual(true, tResult); dynamic tDynamic = new DynamicObjects.Dictionary(); tDynamic.Event = null; var tResult2 = tCachedInvoke.Invoke((object)tDynamic); Assert.AreEqual(false, tResult2); }
public void TestCacheableStaticCall() { var @static = InvokeContext.CreateStatic; var generic = InvokeMemberName.Create; var tCached = new CacheableInvocation(InvocationKind.InvokeMember, generic("Create",new[]{typeof(bool)}) , argCount: 1, context: @static(typeof(StaticType))); var tOut = tCached.Invoke(typeof(StaticType), 1); Assert.AreEqual(false, tOut); }
public void TestOptionalArgumentActivationNoneAndCacheable() { Assert.Throws<MissingMethodException>(() => Activator.CreateInstance<DynamicObjects.List>()); var tList = Dynamic.InvokeConstructor(typeof(DynamicObjects.List)); Assert.AreEqual(typeof(DynamicObjects.List), tList.GetType()); var tCachedInvoke = new CacheableInvocation(InvocationKind.Constructor); var tList1 = tCachedInvoke.Invoke(typeof(DynamicObjects.List)); Assert.AreEqual(typeof(DynamicObjects.List), tList1.GetType()); }
public void CacheableSetTimed() { var tPoco1 = new PropPoco(); var tPoco2 = new PropPoco(); var tSetValue = "1"; var tCacheable = new CacheableInvocation(InvocationKind.Set, "Prop1"); Timer.Action1 = () => tCacheable.Invoke(tPoco1, tSetValue); var tPropertyInfo = tPoco2.GetType().GetProperty("Prop1"); Timer.Action2 = () => tPropertyInfo.SetValue(tPoco2, tSetValue, new object[] { }); var elapsed = Timer.Go(); Console.WriteLine("Impromptu: " + elapsed.Item1); Console.WriteLine("Refelection: " + elapsed.Item2); Console.WriteLine("Impromptu VS Reflection: {0}", TimeIt.RelativeSpeed(elapsed)); Assert.Less(elapsed.Item1, elapsed.Item2); }
public void TestCacheablePocoAddAssign() { var tPoco = new PocoEvent(); bool tTest = false; var tCachedInvoke = new CacheableInvocation(InvocationKind.AddAssign, "Event"); tCachedInvoke.Invoke(tPoco, new EventHandler<EventArgs>((@object, args) => { tTest = true; })); tPoco.OnEvent(null, null); Assert.AreEqual(true, tTest); var tPoco2 = new PropPoco() { Event = 3 }; tCachedInvoke.Invoke(tPoco2, 4); Assert.AreEqual(7L, tPoco2.Event); }
public void TestCacheableStaticGet() { var @static = InvokeContext.CreateStatic; var tCached = new CacheableInvocation(InvocationKind.Get, "Today", context: @static(typeof(DateTime))); var tDate = tCached.Invoke(typeof(DateTime)); Assert.AreEqual(DateTime.Today, tDate); }
public void TestCacheablePocoSubtractAssign() { var tPoco = new PocoEvent(); bool tTest = false; var tEvent = new EventHandler<EventArgs>((@object, args) => { tTest = true; }); var tCachedInvoke = new CacheableInvocation(InvocationKind.SubtractAssign, "Event"); tPoco.Event += tEvent; tCachedInvoke.Invoke(tPoco, tEvent); tPoco.OnEvent(null, null); Assert.AreEqual(false, tTest); tCachedInvoke.Invoke(tPoco, tEvent);//Test Second Time var tPoco2 = new PropPoco() { Event = 3 }; tCachedInvoke.Invoke(tPoco2, 4); Assert.AreEqual(-1, tPoco2.Event); }