public override void SetValue(object obj, object value, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture) { if (index != null && index.Length > 0) { Impromptu.InvokeGetIndex(obj, index.Concat(new[] { value }).ToArray()); return; } _cachedSet.Invoke(obj, value); }
public override object GetValue(object obj, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture) { if (index != null && index.Length > 0) { return(Impromptu.InvokeGetIndex(obj, index)); } return(_cachedGet.Invoke(obj)); }
/// <summary> /// When overridden in a derived class, sets the value of the component to a different value. /// </summary> /// <param name="component">The component with the property value that is to be set.</param> /// <param name="value">The new value.</param> public override void SetValue(object component, object value) { try { _invokeSet.Invoke(component, value); } catch (RuntimeBinderException) { } }
/// <summary> /// When overridden in a derived class, gets the current value of the property on a component. /// </summary> /// <param name="component">The component with the property for which to retrieve the value.</param> /// <returns> /// The value of a property for a given component. /// </returns> public override object GetValue(object component) { try { return(_invokeGet.Invoke(component)); } catch (RuntimeBinderException) { return(null); } }
public void TestCacheableSetTimed() { var tPoco = new PropPoco(); var tSetValue = "1"; var tCacheable = new CacheableInvocation(InvocationKind.Set, "Prop1"); var tWatch = TimeIt.Go(() => tCacheable.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 TestCacheableDynamicSetAndPocoSetAndSetNull() { 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 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 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 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 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 TestCacheableStaticDateTimeMethod() { object tDateDyn = "01/20/2009"; var tCachedInvoke = new CacheableInvocation(InvocationKind.InvokeMember, "Parse", 1, context: typeof (DateTime).WithStaticContext()); var tDate = tCachedInvoke.Invoke(typeof(DateTime), tDateDyn); Assert.AreEqual(new DateTime(2009, 1, 20), 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 TestCacheableStaticGet() { var tCached = new CacheableInvocation(InvocationKind.Get, "Today", context: typeof(DateTime).WithStaticContext()); var tDate = tCached.Invoke(typeof(DateTime)); Assert.AreEqual(DateTime.Today, tDate); }
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 TestCacheableDoNotExposePrivateMethod() { var tTest = new TestWithPrivateMethod(); var tCachedInvoke = new CacheableInvocation(InvocationKind.InvokeMember, "Test"); AssertException<RuntimeBinderException>(() => tCachedInvoke.Invoke(tTest)); }
public override object InvokeMember(string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters) { object tContext = typeof (object); InvocationKind tKind; if ((BindingFlags.GetProperty & invokeAttr) != 0) { tKind = name.Equals(Invocation.IndexBinderName) ? InvocationKind.GetIndex : InvocationKind.Get; } else if ((BindingFlags.SetProperty & invokeAttr) != 0) { tKind = name.Equals(Invocation.IndexBinderName) ? InvocationKind.SetIndex : InvocationKind.Set; } else { tKind = InvocationKind.InvokeMemberUnknown; } if((BindingFlags.NonPublic & invokeAttr) !=0) { tContext = target; } //Use cachedable invocation not because it's getting cached, but because the constructor matches the parameters better. var tCachedInvocation = new CacheableInvocation(tKind, name, args.Length, namedParameters, tContext); return tCachedInvocation.Invoke(target, args); }
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 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 TestCacheableIsEventAndIsNotEvent() { object tPoco = new PocoEvent(); var tCachedInvoke = new CacheableInvocation(InvocationKind.IsEvent, "Event"); var tResult = tCachedInvoke.Invoke(tPoco); Assert.AreEqual(true, tResult); dynamic tDynamic = new ImpromptuDictionary(); tDynamic.Event = null; var tResult2 = tCachedInvoke.Invoke((object) tDynamic); Assert.AreEqual(false, tResult2); }
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 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 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); }
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 TestCacheableDynamicSubtractAssign() { var tDynamic = 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 tEvent = new EventHandler<EventArgs>((@object, args) => { tTest = true; }); var tCachedInvoke = new CacheableInvocation(InvocationKind.SubtractAssign, "Event"); tDynamic.Event += tEvent; tCachedInvoke.Invoke((object) tDynamic, tEvent); tDynamic.OnEvent(null, null); Assert.AreEqual(false, tTest); tCachedInvoke.Invoke((object)tDynamic2, 4); Assert.AreEqual(-1, tDynamic2.Event); }
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 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 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 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 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 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 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 TestCacheableMethodPocoGetValue4argsTimed() { var tValue = "test 123 45 string"; var tCachedInvoke = new CacheableInvocation(InvocationKind.InvokeMember, "IndexOf", 4); var tWatch = TimeIt.Go(() => { var tOut = tCachedInvoke.Invoke(tValue,"45", 0, 14, StringComparison.InvariantCulture); }, 500000); var tMethodInfo = tValue.GetType().GetMethod("IndexOf", new Type[] { typeof(string), typeof(int), typeof(int), typeof(StringComparison) }); var tWatch2 = TimeIt.Go(() => { var tOut = tMethodInfo.Invoke(tValue, new object[] { "45", 0, 14, StringComparison.InvariantCulture }); }, 500000); 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); }