/// <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); }
/// <summary> /// Initializes a new instance of the <see cref="ImpromptuRuntimePropertyInfo"/> class. /// </summary> /// <param name="declaringType">Type of the declaring.</param> /// <param name="propertyName">Name of the property.</param> public ImpromptuRuntimePropertyInfo(Type declaringType, string propertyName) { _declaringType = declaringType; _propertyName = propertyName; _cachedGet = new CacheableInvocation(InvocationKind.Get, propertyName); _cachedSet = new CacheableInvocation(InvocationKind.Set, propertyName); }
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)); }
/// <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); }
/// <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 PartialApplyInvocation(Target, tNewArgs, MemberName, 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 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); }
/// <summary> /// Equalses the specified other. /// </summary> /// <param name="other">The other.</param> /// <returns></returns> public bool Equals(CacheableInvocation other) { if (ReferenceEquals(null, other)) { return(false); } if (ReferenceEquals(this, other)) { return(true); } return(base.Equals(other) && other._argCount == _argCount && Equals(other._argNames, _argNames) && other._staticContext.Equals(_staticContext) && Equals(other._context, _context) && other._convertExplicit.Equals(_convertExplicit) && Equals(other._convertType, _convertType)); }
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 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); }
/// <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 PartialApplyInvocation(Target, tNewArgs, MemberName, 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; }
/// <summary> /// Initializes a new instance of the <see cref="ImpromptuPropertyDescriptor"/> class. /// </summary> /// <param name="name">The name.</param> public ImpromptuPropertyDescriptor(string name) : base(name, null) { _invokeGet = new CacheableInvocation(InvocationKind.Get, name); _invokeSet = new CacheableInvocation(InvocationKind.Set, name); }
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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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); }
/// <summary> /// Initializes a new instance of the <see cref="BinderEventHandlerMemberName"/> class. /// </summary> /// <param name="name">The name.</param> public BinderEventHandlerMemberName(string name) { _invocation = new CacheableInvocation(InvocationKind.InvokeMemberAction, name, 2); }
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 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 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); }