Пример #1
0
        public void TestSetTimed()
        {
            // Compares PB duck-typing with plain reflection and ImpromptuInterfaces (InvokeSet and using an ActLike<> invocation)
            // So far, PB-duck-typing is about twice as slow (when profiled) as II. Perhaps this can get better... Though the penalty
            // may be worth it when considering the extra features (like fuzzy-ducking)
            var tPoco = new PropPoco();

            var tSetValue = "1";

            var tWatch = TimeIt.Go(() =>
            {
                Impromptu.InvokeSet(tPoco, "Prop1", tSetValue);
            }, 500000);
            var tPropertyInfo = tPoco.GetType().GetProperty("Prop1");
            var tWatch2       = TimeIt.Go(() => tPropertyInfo.SetValue(tPoco, tSetValue, new object[] {}), TIMES);
            var ducked        = tPoco.DuckAs <IPropPoco>();

            Assert.IsNotNull(ducked);
            var tWatch3 = TimeIt.Go(() => ducked.Prop1 = tSetValue, TIMES);


            TestContext.WriteLine("InvokeSet: " + tWatch.Elapsed);
            TestContext.WriteLine("Reflection: " + tWatch2.Elapsed);
            TestContext.WriteLine("Ducked: " + tWatch3.Elapsed);
        }
Пример #2
0
        public void TestStaticSet()
        {
            int tValue = 12;

            Impromptu.InvokeSet(typeof(StaticType).WithStaticContext(), "TestSet", tValue);
            Assert.AreEqual(tValue, StaticType.TestSet);
        }
Пример #3
0
        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            if (CallTarget == null)
            {
                return(false);
            }

            if (Impromptu.InvokeIsEvent(CallTarget, binder.Name) && value is ImpromptuForwarderAddRemove)
            {
                var tValue = value as ImpromptuForwarderAddRemove;

                if (tValue.IsAdding)
                {
                    Impromptu.InvokeAddAssign(CallTarget, binder.Name, tValue.Delegate);
                }
                else
                {
                    Impromptu.InvokeSubtractAssign(CallTarget, binder.Name, tValue.Delegate);
                }

                return(true);
            }

            Impromptu.InvokeSet(CallTarget, binder.Name, value);

            return(true);
        }
Пример #4
0
        public void TestStructSet()
        {
            object tPoco = new PropStruct();

            var tSetValue = "1";

            Impromptu.InvokeSet(tPoco, "Prop1", tSetValue);

            Assert.AreEqual(tSetValue, ((PropStruct)tPoco).Prop1);
        }
Пример #5
0
        public void TestPocoSet()
        {
            var tPoco = new PropPoco();

            var tSetValue = "1";

            Impromptu.InvokeSet(tPoco, "Prop1", tSetValue);

            Assert.AreEqual(tSetValue, tPoco.Prop1);
        }
Пример #6
0
        public void TestDynamicSet()
        {
            dynamic tExpando = new ExpandoObject();

            var tSetValue = "1";

            Impromptu.InvokeSet(tExpando, "Test", tSetValue);

            Assert.AreEqual(tSetValue, tExpando.Test);
        }
Пример #7
0
        public void FillCommandProperties(IEnumerable <string> arguments, ICommand command)
        {
            foreach (var argument in command.GetCommandProperties())
            {
                if (argument.CanWrite)
                {
                    var arg = GetArgument(arguments, argument.Name);

                    if (arg != null && TypeProviders.ContainsKey(argument.PropertyType.FullName))
                    {
                        var value = TypeProviders[argument.PropertyType.FullName](arg);
                        Impromptu.InvokeSet(command, argument.Name, value);
                    }
                }
            }
        }
Пример #8
0
        public static T ToStatic <T>(dynamic source)
        {
            var entity = Activator.CreateInstance <T>();

            var properties = Impromptu.GetMemberNames(source);

            if (properties == null)
            {
                return(entity);
            }

            foreach (var entry in properties)
            {
                var value = Impromptu.InvokeGet(source, entry);
                Impromptu.InvokeSet(entity, entry, value);
            }

            return(entity);
        }
Пример #9
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);
            }
        }
Пример #10
0
        public void SetUp()
        {
            var staticContext = InvokeContext.CreateStatic;

            Impromptu.InvokeSet(staticContext(typeof(IoC)), "Container", null);
        }
Пример #11
0
 /// <summary>设置ViewBag的值
 /// </summary>
 /// <param name="key">键</param>
 /// <param name="value">值</param>
 internal void AddViewBageValues(string key, object value)
 {
     Impromptu.InvokeSet(ViewBag, key, value);
 }