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 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 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 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); }
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 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 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 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 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 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 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 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 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 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 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 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 TestCacheableConstructorTimed() { var tCachedInvoke = new CacheableInvocation(InvocationKind.Constructor, argCount: 1); var tWatch = TimeIt.Go(() => { var tOut = tCachedInvoke.Invoke(typeof(Tuple <string>), "Test"); }); var tWatch2 = TimeIt.Go(() => { var tOut = Activator.CreateInstance(typeof(Tuple <string>), "Test"); }); TestContext.WriteLine("Impromptu: " + tWatch.Elapsed); TestContext.WriteLine("Refelection: " + 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 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 TestCacheableSetNullTimed() { var tPoco = new PropPoco(); String tSetValue = null; var tCachedInvoke = new CacheableInvocation(InvocationKind.Set, "Prop1"); var tWatch = TimeIt.Go(() => tCachedInvoke.Invoke(tPoco, tSetValue), 500000); var tPropertyInfo = tPoco.GetType().GetProperty("Prop1"); var tWatch2 = TimeIt.Go(() => tPropertyInfo.SetValue(tPoco, tSetValue, new object[] { }), 500000); TestContext.WriteLine("Impromptu: " + tWatch.Elapsed); TestContext.WriteLine("Refelection: " + 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 TestCachedConstructorValueTypeTimed() { var tIter = 1000000; var tCachedInvoke = new CacheableInvocation(InvocationKind.Constructor, argCount: 3); var tWatch = TimeIt.Go(() => { var tOut = tCachedInvoke.Invoke(typeof(DateTime), 2010, 1, 20); }, tIter); var tWatch2 = TimeIt.Go(() => { var tOut = Activator.CreateInstance(typeof(DateTime), 2010, 1, 20); }, tIter); TestContext.WriteLine("Impromptu: " + tWatch.Elapsed); TestContext.WriteLine("Refelection: " + 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 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 TestCacheableMethodPocoVoidTimed() { var tValue = new Dictionary <object, object>(); var tCachedInvoke = new CacheableInvocation(InvocationKind.InvokeMemberAction, "Clear"); var tWatch = TimeIt.Go(() => tCachedInvoke.Invoke(tValue)); var tMethodInfo = tValue.GetType().GetMethod("Clear", new Type[] { }); var tWatch2 = TimeIt.Go(() => tMethodInfo.Invoke(tValue, new object[] { })); 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 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 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 TestOptionalArgumentActivationNoneAndCacheable() { AssertException <MissingMethodException>(() => Activator.CreateInstance <ImpromptuList>()); var tList = Impromptu.InvokeConstructor(typeof(ImpromptuList)); Assert.AreEqual(typeof(ImpromptuList), tList.GetType()); var tCachedInvoke = new CacheableInvocation(InvocationKind.Constructor); var tList1 = tCachedInvoke.Invoke(typeof(ImpromptuList)); Assert.AreEqual(typeof(ImpromptuList), tList1.GetType()); }
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 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 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 CachedConstructorValueTypeTimed() { var tCachedInvoke = new CacheableInvocation(InvocationKind.Constructor, argCount: 3); Timer.Action1 = (() => { var tOut = tCachedInvoke.Invoke(typeof(DateTime), 2010, 1, 20); }); Timer.Action2 = (() => { var tOut = Activator.CreateInstance(typeof(DateTime), 2010, 1, 20); }); 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 TestCacheableMethodPocoGetValueTimed() { var tValue = 1; var tCachedInvoke = new CacheableInvocation(InvocationKind.InvokeMember, "ToString"); var tWatch = TimeIt.Go(() => { var tOut = tCachedInvoke.Invoke(tValue); }, 500000); var tMethodInfo = tValue.GetType().GetMethod("ToString", new Type[] { }); var tWatch2 = TimeIt.Go(() => { var tOut = tMethodInfo.Invoke(tValue, new object[] { }); }, 500000); TestContext.WriteLine("Impromptu: " + tWatch.Elapsed); TestContext.WriteLine("Refelection: " + 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 CacheableMethodPocoVoidTimed() { var tValue = new Dictionary <object, object>(); var tCachedInvoke = new CacheableInvocation(InvocationKind.InvokeMemberAction, "Clear"); Timer.Action1 = (() => tCachedInvoke.Invoke(tValue)); var tMethodInfo = tValue.GetType().GetMethod("Clear", new Type[] { }); Timer.Action2 = (() => tMethodInfo.Invoke(tValue, 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 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 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 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 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 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 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 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 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); }
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 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 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 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); }
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 CacheableMethodStaticMethodValueTimed() { var tStaticType = typeof(DateTime); var tContext = InvokeContext.CreateStatic(tStaticType); string tDate = "01/20/2009"; var tCachedInvoke = new CacheableInvocation(InvocationKind.InvokeMember, "Parse", argCount: 1, context: tContext); Timer.Action1=(() => { var tOut = tCachedInvoke.Invoke(tStaticType, tDate); }); var tMethodInfo = typeof(DateTime).GetMethod("Parse", new[] { typeof(string) }); Timer.Action2=(() => { var tOut = tMethodInfo.Invoke(tStaticType, new object[] { tDate }); }); 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 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 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 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); }