Ejemplo n.º 1
0
        public void GetterArrayTest()
        {
            var tArray = new int[] { 1, 2, 3 };

            IStringIntIndexer tTest = ImpromptuGet.Create <IStringIntIndexer>(tArray);

            Assert.AreEqual(tArray[2].ToString(), tTest[2]);
        }
        public void GetterVoidTest()
        {
            var tPoco = new VoidMethodPoco();

            dynamic tTest = new ImpromptuGet(tPoco);

            tTest.Action();
        }
Ejemplo n.º 3
0
        public void GetterVoidTest()
        {
            var tPoco = new VoidMethodPoco();

            dynamic tTest = new ImpromptuGet(tPoco);

            tTest.Action();
        }
Ejemplo n.º 4
0
        public void GetterEventTest()
        {
            var tActsLike = ImpromptuGet.Create <IEvent>(new PocoEvent());
            var tSet      = false;

            tActsLike.Event += (obj, args) => tSet = true;

            tActsLike.OnEvent(null, null);
            Assert.AreEqual(true, tSet);
        }
Ejemplo n.º 5
0
        public void GetterAnonTest()
        {
            var tAnon = new { Prop1 = "Test", Prop2 = 42L, Prop3 = Guid.NewGuid() };

            dynamic tTest = new ImpromptuGet(tAnon);

            Assert.AreEqual(tAnon.Prop1, tTest.Prop1);
            Assert.AreEqual(tAnon.Prop2, tTest.Prop2);
            Assert.AreEqual(tAnon.Prop3, tTest.Prop3);
        }
        public void GetterAnonTest()
        {
            var tAnon = new { Prop1 = "Test", Prop2 = 42L, Prop3 = Guid.NewGuid() };

            dynamic tTest =new ImpromptuGet(tAnon);

            Assert.AreEqual(tAnon.Prop1, tTest.Prop1);
            Assert.AreEqual(tAnon.Prop2, tTest.Prop2);
            Assert.AreEqual(tAnon.Prop3, tTest.Prop3);
        }
Ejemplo n.º 7
0
        public void GetterEventTest2()
        {
            var tActsLike = ImpromptuGet.Create <IEvent>(new PocoEvent());
            var tSet      = false;
            EventHandler <EventArgs> tActsLikeOnEvent = (obj, args) => tSet = true;

            tActsLike.Event += tActsLikeOnEvent;
            tActsLike.Event -= tActsLikeOnEvent;
            tActsLike.OnEvent(null, null);
            Assert.AreEqual(false, tSet);
        }
Ejemplo n.º 8
0
        public void GetterDynamicTest()
        {
            dynamic tNew = new ExpandoObject();

            tNew.Prop1 = "Test";
            tNew.Prop2 = 42L;
            tNew.Prop3 = Guid.NewGuid();

            dynamic tTest = new ImpromptuGet(tNew);


            Assert.AreEqual(tNew.Prop1, tTest.Prop1);
            Assert.AreEqual(tNew.Prop2, tTest.Prop2);
            Assert.AreEqual(tNew.Prop3, tTest.Prop3);
        }
Ejemplo n.º 9
0
        public void TestAnonInterface()
        {
            var tInterface = ImpromptuGet.Create <ICollection>(new
            {
                CopyArray      = ReturnVoid.Arguments <Array, int>((ar, i) => Enumerable.Range(1, 10)),
                Count          = 10,
                IsSynchronized = false,
                SyncRoot       = this,
                GetEnumerator  = Return <IEnumerator> .Arguments(() => Enumerable.Range(1, 10).GetEnumerator())
            });

            Assert.AreEqual(10, tInterface.Count);
            Assert.AreEqual(false, tInterface.IsSynchronized);
            Assert.AreEqual(this, tInterface.SyncRoot);
            Assert.AreEqual(true, tInterface.GetEnumerator().MoveNext());
        }
Ejemplo n.º 10
0
        public void DynamicAnnonymousWrapper()
        {
            var tData = new Dictionary <int, string> {
                { 1, "test" }
            };
            var tDyn = ImpromptuGet.Create(new
            {
                Test1          = 1,
                Test2          = "2",
                IsGreaterThan5 = Return <bool> .Arguments <int>(it => it > 5),
                ClearData      = ReturnVoid.Arguments(() => tData.Clear())
            });

            Assert.AreEqual(1, tDyn.Test1);
            Assert.AreEqual("2", tDyn.Test2);
            Assert.AreEqual(true, tDyn.IsGreaterThan5(6));
            Assert.AreEqual(false, tDyn.IsGreaterThan5(4));

            Assert.AreEqual(1, tData.Count);
            tDyn.ClearData();
            Assert.AreEqual(0, tData.Count);
        }
Ejemplo n.º 11
0
        public static dynamic CoerceConvert(object target, Type type)
        {
            if (target != null && !type.IsInstanceOfType(target) && DBNull.Value != target)
            {
                var delegateConversion = CoerceToDelegate(target, type);

                if (delegateConversion != null)
                {
                    return(delegateConversion);
                }


                if (type.IsInterface)
                {
                    if (target is IDictionary <string, object> && !(target is ImpromptuDictionaryBase))
                    {
                        target = new ImpromptuDictionary((IDictionary <string, object>)target);
                    }
                    else
                    {
                        target = new ImpromptuGet(target);
                    }


                    target = Impromptu.DynamicActLike(target, type);
                }
                else
                {
                    try
                    {
                        object tResult;

                        tResult = Impromptu.InvokeConvert(target, type, @explicit: true);

                        target = tResult;
                    }
                    catch (RuntimeBinderException)
                    {
                        Type tReducedType = type;
                        if (type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable <>)))
                        {
                            tReducedType = type.GetGenericArguments().First();
                        }


                        if (target is IConvertible && typeof(IConvertible).IsAssignableFrom(tReducedType) && !typeof(Enum).IsAssignableFrom(tReducedType))
                        {
                            target = Convert.ChangeType(target, tReducedType, Thread.CurrentThread.CurrentCulture);
                        }
                        else
                        {  //finally check type converter since it's the slowest.
#if !SILVERLIGHT
                            var tConverter = TypeDescriptor.GetConverter(tReducedType);
#else
                            TypeConverter tConverter  = null;
                            var           tAttributes = tReducedType.GetCustomAttributes(typeof(TypeConverterAttribute), false);
                            var           tAttribute  = tAttributes.OfType <TypeConverterAttribute>().FirstOrDefault();
                            if (tAttribute != null)
                            {
                                tConverter =
                                    Impromptu.InvokeConstructor(Type.GetType(tAttribute.ConverterTypeName));
                            }
#endif
                            if (tConverter != null && tConverter.CanConvertFrom(target.GetType()))
                            {
                                target = tConverter.ConvertFrom(target);
                            }

#if SILVERLIGHT
                            else if (target is string)
                            {
                                var tDC = new SilverConvertertDC(target as String);
                                var tFE = new SilverConverterFE
                                {
                                    DataContext = tDC
                                };


                                var tProp = SilverConverterFE.GetProperty(tReducedType);

                                tFE.SetBinding(tProp, new System.Windows.Data.Binding("StringValue"));

                                var tResult = tFE.GetValue(tProp);

                                if (tResult != null)
                                {
                                    target = tResult;
                                }
                            }
#endif
                        }
                    }
                }
            }
            else if (((target == null) || target == DBNull.Value) && type.IsValueType)
            {
                target = Impromptu.InvokeConstructor(type);
            }
            else if (!type.IsInstanceOfType(target) && DBNull.Value == target)
            {
                return(null);
            }
            return(target);
        }
        public void GetterDynamicTest()
        {
            dynamic tNew = new ExpandoObject();
            tNew.Prop1 = "Test";
            tNew.Prop2 = 42L;
            tNew.Prop3 = Guid.NewGuid();

            dynamic tTest = new ImpromptuGet(tNew);


            Assert.AreEqual(tNew.Prop1, tTest.Prop1);
            Assert.AreEqual(tNew.Prop2, tTest.Prop2);
            Assert.AreEqual(tNew.Prop3, tTest.Prop3);
        }
Ejemplo n.º 13
0
        internal static bool MassageResultBasedOnInterface(this ImpromptuObject target, string binderName, bool resultFound, ref object result)
        {
            if (result is ImpromptuForwarderAddRemove) //Don't massage AddRemove Proxies
            {
                return(true);
            }

            Type tType;
            var  tTryType = target.TryTypeForName(binderName, out tType);

            if (tTryType && tType == typeof(void))
            {
                return(true);
            }

            if (resultFound)
            {
                if (result is IDictionary <string, object> && !(result is ImpromptuDictionaryBase) &&
                    (!tTryType || tType == typeof(object)))
                {
                    result = new ImpromptuDictionary((IDictionary <string, object>)result);
                }
                else if (tTryType)
                {
                    if (result != null && !tType.IsAssignableFrom(result.GetType()))
                    {
                        if (tType.IsInterface)
                        {
                            if (result is IDictionary <string, object> && !(result is ImpromptuDictionaryBase))
                            {
                                result = new ImpromptuDictionary((IDictionary <string, object>)result);
                            }
                            else
                            {
                                result = new ImpromptuGet(result);
                            }

                            result = Impromptu.DynamicActLike(result, tType);
                        }
                        else
                        {
                            try {
                                object tResult;

                                tResult = Impromptu.InvokeConvert(target, tType, explict: true);

                                result = tResult;
                            } catch (RuntimeBinderException) {
                                Type tReducedType = tType;
                                if (tType.IsGenericType && tType.GetGenericTypeDefinition().Equals(typeof(Nullable <>)))
                                {
                                    tReducedType = tType.GetGenericArguments().First();
                                }

                                if (result is IConvertible && typeof(IConvertible).IsAssignableFrom(tReducedType))
                                {
                                    result = Convert.ChangeType(result, tReducedType, Thread.CurrentThread.CurrentCulture);
                                }
                                else
                                {
                                    //finally check type converter since it's the slowest.

#if !SILVERLIGHT
                                    var tConverter = TypeDescriptor.GetConverter(tType);
#else
                                    TypeConverter tConverter  = null;
                                    var           tAttributes = tType.GetCustomAttributes(typeof(TypeConverterAttribute), false);
                                    var           tAttribute  = tAttributes.OfType <TypeConverterAttribute>().FirstOrDefault();
                                    if (tAttribute != null)
                                    {
                                        tConverter =
                                            Impromptu.InvokeConstructor(Type.GetType(tAttribute.ConverterTypeName));
                                    }
#endif
                                    if (tConverter != null && tConverter.CanConvertFrom(result.GetType()))
                                    {
                                        result = tConverter.ConvertFrom(result);
                                    }

#if SILVERLIGHT
                                    else if (result is string)
                                    {
                                        var tDC = new SilverConvertertDC(result as String);
                                        var tFE = new SilverConverterFE
                                        {
                                            DataContext = tDC
                                        };


                                        var tProp = SilverConverterFE.GetProperty(tType);

                                        tFE.SetBinding(tProp, new System.Windows.Data.Binding("StringValue"));

                                        var tResult = tFE.GetValue(tProp);

                                        if (tResult != null)
                                        {
                                            result = tResult;
                                        }
                                    }
#endif
                                }
                            }
                        }
                    }
                    else if (result == null && tType.IsValueType)
                    {
                        result = Impromptu.InvokeConstructor(tType);
                    }
                }
            }
            else
            {
                result = null;
                if (!tTryType)
                {
                    return(false);
                }
                if (tType.IsValueType)
                {
                    result = Impromptu.InvokeConstructor(tType);
                }
            }
            return(true);
        }