示例#1
0
        public void TestStaticDateTimeMethod()
        {
            object tDateDyn = "01/20/2009";
            var    tDate    = Impromptu.InvokeMember(typeof(DateTime).WithStaticContext(), "Parse", tDateDyn);

            Assert.AreEqual(new DateTime(2009, 1, 20), tDate);
        }
示例#2
0
        public void TestMethodPocoOverloadingPassAndGetValueNull()
        {
            var tPoco = new OverloadingMethPoco();

            var tValue = 1;

            var tOut = Impromptu.InvokeMember(tPoco, "Func", tValue);

            Assert.AreEqual("int", tOut);

            Assert.AreEqual("int", (object)tOut); //should still be int because this uses runtime type


            var tOut2 = Impromptu.InvokeMember(tPoco, "Func", 1m);

            Assert.AreEqual("object", tOut2);

            var tOut3 = Impromptu.InvokeMember(tPoco, "Func", null);

            Assert.AreEqual("object", tOut3);

            var tOut4 = Impromptu.InvokeMember(tPoco, "Func", null, null, "test", null, null, null);

            Assert.AreEqual("object 6", tOut4);

            var tOut5 = Impromptu.InvokeMember(tPoco, "Func", null, null, null, null, null, null);

            Assert.AreEqual("object 6", tOut5);
        }
示例#3
0
        public void TestStaticCall()
        {
            var tOut = Impromptu.InvokeMember(typeof(StaticType).WithStaticContext(),
                                              "Create".WithGenericArgs(typeof(bool)), 1);

            Assert.AreEqual(false, tOut);
        }
示例#4
0
        public void TestMethodPocoGetValuePassNullDoubleCallTimed()
        {
#if DEBUG
            Assert.Ignore("Visual Studio slows down dynamic too much in debug mode");
#endif
            var tValue = new OverloadingMethPoco();


            var tInteration = 500000;
            var tWatch      = TimeIt.Go(() => {
                var tOut  = Impromptu.InvokeMember(tValue, "Func", null);
                var tOut2 = Impromptu.InvokeMember(tValue, "Func", 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);
        }
示例#5
0
        /// <summary>
        /// Forwards the invoke operation.
        /// </summary>
        /// <param name="binder">the binder</param>
        /// <param name="args">the arguments</param>
        /// <param name="result">the result</param>
        /// <returns>true when successfull</returns>
        public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
        {
            if (CallTarget == null)
            {
                result = null;
                return(false);
            }

            object[] tArgs = Util.NameArgsIfNecessary(binder.CallInfo, args);

            try
            {
                result = Impromptu.InvokeMember(CallTarget, binder.Name, tArgs);
            }
            catch (RuntimeBinderException)
            {
                result = null;
                try
                {
                    Impromptu.InvokeMemberAction(CallTarget, binder.Name, tArgs);
                }
                catch (RuntimeBinderException)
                {
                    return(false);
                }
            }
            return(true);
        }
示例#6
0
        public async Task <ReturnType> InvokeWithRetry <ReturnType>(string methodName, object[] args)
        {
            Func <Task <ReturnType> > retryCall = () => { return(Impromptu.InvokeMember(wrappedObject, methodName, args)); };
            var retryInterceptor = new RetryInterceptor <ReturnType>(context, retryOptions, retryCall);

            return(await retryInterceptor.Invoke());
        }
示例#7
0
        private void HelpTestPocoPassAndGetValue(string tValue, string tParam)
        {
            var tExpected = tValue.StartsWith(tParam);

            var tOut = Impromptu.InvokeMember(tValue, "StartsWith", tParam);

            Assert.AreEqual(tExpected, tOut);
        }
示例#8
0
        public void TestMethodPocoGetValue()
        {
            var tValue = 1;

            var tOut = Impromptu.InvokeMember(tValue, "ToString");

            Assert.AreEqual(tValue.ToString(), tOut);
        }
示例#9
0
        public void InvokeMemberContainsNameWithImpromptuInvoke()
        {
            var clay   = new Clay(new TestBehavior());
            var result = Impromptu.InvokeMember(clay, "Help", "Test");

            Assert.IsTrue(result.Contains("[name:Help]"), "Does Not Match Argument Name");
            Assert.IsTrue(result.Contains("[count:1]"), "Does Not Match Argument Count");
        }
示例#10
0
        public void Should_store_operation_meta_with_correct_method(string operationMethodName, string expectedMethod)
        {
            // when
            Impromptu.InvokeMember(this.builder, operationMethodName);

            // then
            this.operations.Should().Contain(meta => meta.Method == expectedMethod);
        }
 private static dynamic GetContextFromMethod(object entity)
 {
     try
     {
         return(Impromptu.InvokeMember(entity.GetType().WithStaticContext(), "GetContext", entity));
     }
     catch (RuntimeBinderException)
     {
         return(null);
     }
 }
示例#12
0
        public void TestMethodDynamicPassAndGetValue()
        {
            dynamic tExpando = new ExpandoObject();

            tExpando.Func = new Func <int, string>(it => it.ToString());

            var tValue = 1;

            var tOut = Impromptu.InvokeMember(tExpando, "Func", tValue);

            Assert.AreEqual(tValue.ToString(), tOut);
        }
示例#13
0
        public void TestMethodPocoOverloadingPass2AndGetValueArgOptional()
        {
            var tPoco = new OverloadingMethPoco();

            var tValue = 1;

            var arg = InvokeArg.Create;

            var tOut = Impromptu.InvokeMember(tPoco, "Func", arg("two", tValue), arg("one", tValue));

            Assert.AreEqual("object named", tOut);

            Assert.AreEqual("object named", (object)tOut);
        }
示例#14
0
        public void TestMethodStaticMethodValueTimed()
        {
            var    tStaticType = typeof(DateTime);
            var    tTarget     = tStaticType.WithStaticContext();
            string tDate       = "01/20/2009";
            var    tWatch      = TimeIt.Go(() => { var tOut = Impromptu.InvokeMember(tTarget, "Parse", tDate); }, 500000);
            var    tMethodInfo = typeof(DateTime).GetMethod("Parse", new[] { typeof(string) });
            var    tWatch2     = TimeIt.Go(() =>
            {
                var tOut = tMethodInfo.Invoke(tStaticType, new object[] { tDate });
            }, 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);
        }
 internal void PrecacheBaseTypes(IEnumberable <Type> types)
 {
     if (TypeOfDataContract == null)
     {
         TypeOfDataContract = Type.GetType("System.Runtime.Serialization.DataContract, System.Runtime.Serialization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
     }
     lock (xmlNames)
     {
         var remaining = new Queue <Type>(types.Except(xmlNames.Values));
         while (remaining.Count > 0)
         {
             Type          next   = remaining.Dequeue();
             var           dc     = Impromptu.InvokeMember(TypeOfDataContract.WithStaticContext(), "GetDataContract", next);
             IDataContract result = Impromptu.ActLike <IDataContract>(dc);
             xmlNames.Add(new System.Xml.XmlQualifiedName(result.Name.Value, result.Namespace.Value), next);
         }
     }
 }
示例#16
0
        public void TestMethodPocoGetValue4argsTimed()
        {
            var tValue = "test 123 45 string";



            var tWatch      = TimeIt.Go(() => { var tOut = Impromptu.InvokeMember(tValue, "IndexOf", "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);
        }
示例#17
0
        public void TestMethodPocoGetValueTimed()
        {
            var tValue = 1;



            var tWatch      = TimeIt.Go(() => { var tOut = Impromptu.InvokeMember(tValue, "ToString"); }, 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);
        }
示例#18
0
        public void TestMethodPocoOverloadingPassAndGetValueArg()
        {
            var tPoco = new OverloadingMethPoco();

            var tValue = 1;

            var tOut = Impromptu.InvokeMember(tPoco, "Func", new InvokeArg("arg", tValue));

            Assert.AreEqual("int", tOut);

            Assert.AreEqual("int", (object)tOut); //should still be int because this uses runtime type


            var tOut2 = Impromptu.InvokeMember(tPoco, "Func", 1m);

            Assert.AreEqual("object", tOut2);

            var tOut3 = Impromptu.InvokeMember(tPoco, "Func", new { Anon = 1 });

            Assert.AreEqual("object", tOut3);
        }
示例#19
0
 /// <summary>
 /// Gets an exported value of type T
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <returns></returns>
 public T Get <T>() where T : class
 {
     return(Impromptu.InvokeMember(_staticContext, new InvokeMemberName("Resolve", typeof(T)), _container));
 }
示例#20
0
 /// <summary>
 /// Gets a list of exported values of type T
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <returns></returns>
 public IEnumerable <T> GetMany <T>() where T : class
 {
     return(Impromptu.InvokeMember(_staticContext, new InvokeMemberName("ResolveAll", typeof(T)), _container));
 }
示例#21
0
        protected object InvokeStaticMethod(String_OR_InvokeMemberName name, object[] args)
        {
            var staticType = InvokeContext.CreateStatic;
            var nameArgs   = InvokeMemberName.Create;

            var tList = new List <object> {
                CallTarget
            };

            tList.AddRange(args);

            object result        = null;
            var    sucess        = false;
            var    exceptionList = new List <Exception>();

            var tGenericPossibles = new List <Type[]>();

            if (name.GenericArgs != null && name.GenericArgs.Length > 0)
            {
                var tInterface    = CallTarget.GetType().GetInterface(_extendedType.Name, false);
                var tTypeGenerics = (tInterface.IsGenericType ? tInterface.GetGenericArguments()
                                            : new Type[] { }).Concat(name.GenericArgs).ToArray();

                tGenericPossibles.Add(tTypeGenerics);
                tGenericPossibles.Add(name.GenericArgs);
            }
            else
            {
                tGenericPossibles.Add(null);
            }



            foreach (var sType in _staticTypes)
            {
                foreach (var tGenericPossible in tGenericPossibles)
                {
                    try
                    {
                        result = Impromptu.InvokeMember(staticType(sType), nameArgs(name.Name, tGenericPossible), tList.ToArray());
                        sucess = true;
                        break;
                    }
                    catch (RuntimeBinderException ex)
                    {
                        exceptionList.Add(ex);
                    }
                }
            }

            if (!sucess)
            {
#if SILVERLIGHT && !SILVERLIGHT5
                throw exceptionList.First();
#else
                throw new AggregateException(exceptionList);
#endif
            }


            Type tOutType;
            if (TryTypeForName(name.Name, out tOutType))
            {
                if (tOutType.IsInterface)
                {
                    var tIsGeneric = tOutType.IsGenericType;
                    if (tOutType.IsGenericType)
                    {
                        tOutType = tOutType.GetGenericTypeDefinition();
                    }

                    if (InstanceHints.Select(it => tIsGeneric && it.IsGenericType ? it.GetGenericTypeDefinition() : it)
                        .Contains(tOutType))
                    {
                        result = CreateSelf(result, _extendedType, _staticTypes, _instanceHints);
                    }
                }
            }
            else
            {
                if (IsExtendedType(result))
                {
                    result = CreateSelf(result, _extendedType, _staticTypes, _instanceHints);
                }
            }

            return(result);
        }
示例#22
0
        /// <summary>
        /// Gets a list of exported values of type T
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public IEnumerable <T> GetMany <T>() where T : class
        {
            var name = InvokeMemberName.Create;

            return(Impromptu.InvokeMember(_staticContext, name("GetAll", new[] { typeof(T) }), _kernel));
        }
示例#23
0
        /// <summary>
        /// Gets an exported value of type T
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public T Get <T>() where T : class
        {
            var name = InvokeMemberName.Create;

            return(Impromptu.InvokeMember(_staticContext, name("Get", new[] { typeof(T) }), _kernel));
        }
示例#24
0
        /// <summary>
        /// Invokes the invocation on specified target with specific args.
        /// </summary>
        /// <param name="target">The target.</param>
        /// <param name="args">The args.</param>
        /// <returns></returns>
        public virtual object Invoke(object target, params object[] args)
        {
            switch (Kind)
            {
            case InvocationKind.Constructor:
                return(Impromptu.InvokeConstructor((Type)target, args));

            case InvocationKind.Convert:
                bool tExplict = false;
                if (Args.Length == 2)
                {
                    tExplict = (bool)args[1];
                }
                return(Impromptu.InvokeConvert(target, (Type)args[0], tExplict));

            case InvocationKind.Get:
                return(Impromptu.InvokeGet(target, Name.Name));

            case InvocationKind.Set:
                Impromptu.InvokeSet(target, Name.Name, args.FirstOrDefault());
                return(null);

            case InvocationKind.GetIndex:
                return(Impromptu.InvokeGetIndex(target, args));

            case InvocationKind.SetIndex:
                Impromptu.InvokeSetIndex(target, args);
                return(null);

            case InvocationKind.InvokeMember:
                return(Impromptu.InvokeMember(target, Name, args));

            case InvocationKind.InvokeMemberAction:
                Impromptu.InvokeMemberAction(target, Name, args);
                return(null);

            case InvocationKind.InvokeMemberUnknown:
            {
                try
                {
                    return(Impromptu.InvokeMember(target, Name, args));
                }
                catch (RuntimeBinderException)
                {
                    Impromptu.InvokeMemberAction(target, Name, args);
                    return(null);
                }
            }

            case InvocationKind.Invoke:
                return(Impromptu.Invoke(target, args));

            case InvocationKind.InvokeAction:
                Impromptu.InvokeAction(target, args);
                return(null);

            case InvocationKind.InvokeUnknown:
            {
                try
                {
                    return(Impromptu.Invoke(target, args));
                }
                catch (RuntimeBinderException)
                {
                    Impromptu.InvokeAction(target, args);
                    return(null);
                }
            }

            case InvocationKind.AddAssign:
                Impromptu.InvokeAddAssignMember(target, Name.Name, args.FirstOrDefault());
                return(null);

            case InvocationKind.SubtractAssign:
                Impromptu.InvokeSubtractAssignMember(target, Name.Name, args.FirstOrDefault());
                return(null);

            case InvocationKind.IsEvent:
                return(Impromptu.InvokeIsEvent(target, Name.Name));

            default:
                throw new InvalidOperationException("Unknown Invocation Kind: " + Kind);
            }
        }
            public object Invoke(object arg)
            {
                var curObj = arg;

                foreach (var member in referenceChain)
                {
                    curObj = member.MemberType == MemberType.PropertyOrField
                                                        ? Impromptu.InvokeGet(curObj, member.Name) : Impromptu.InvokeMember(curObj, member.Name);
                }
                return(curObj);
            }
示例#26
0
 /// <summary>
 /// Gets an exported value by export name
 /// </summary>
 /// <param name="name"></param>
 /// <returns></returns>
 public dynamic Get(string name)
 {
     return(Impromptu.InvokeMember(_staticContext, new InvokeMemberName("Resolve", typeof(object)), _container, name));
 }
示例#27
0
        public void TestInvokeDoNotExposePrivateMethod()
        {
            var tTest = new TestWithPrivateMethod();

            AssertException <RuntimeBinderException>(() => Impromptu.InvokeMember(tTest.WithContext(this), "Test"));
        }
示例#28
0
        public void TestInvokeInternalTypeMethodAcrossAssemblyBoundries()
        {
            var tTest = PublicType.InternalInstance;

            Assert.AreEqual(true, Impromptu.InvokeMember(tTest, "InternalMethod", 3));
        }
示例#29
0
        public void TestInvokePrivateMethodAcrossAssemblyBoundries()
        {
            var tTest = new PublicType();

            Assert.AreEqual(true, Impromptu.InvokeMember(tTest, "PrivateMethod", 3));
        }
示例#30
0
        public void TestInvokePrivateMethod()
        {
            var tTest = new TestWithPrivateMethod();

            Assert.AreEqual(3, Impromptu.InvokeMember(tTest, "Test"));
        }