コード例 #1
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);
        }
コード例 #2
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);
        }