public override bool TryConvert(System.Dynamic.ConvertBinder binder, out object result)
        {
            result = null;

            if (binder.Type.GetTypeInfo().IsInterface)
            {
                _interfaceTypes.Insert(0, binder.Type);
                result = Impromptu.DynamicActLike(Target, _interfaceTypes.ToArray());
                return(true);
            }

            if (binder.GetType().GetTypeInfo().IsInstanceOfType(Target))
            {
                result = Target;
            }

            return(false);
        }
Exemple #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);
        }