Esempio n. 1
0
        public void DynamiteyDictionaryNullMethodsTest()
        {
            dynamic tNew = new DynObj.Dictionary();

            ISimpleStringMethod tActsLike = Impromptu.ActLike <ISimpleStringMethod>(tNew);

            Assert.AreEqual(false, tActsLike.StartsWith("Te"));
        }
Esempio n. 2
0
        /// <summary>
        /// Goes the extra mile to convert target to type.
        /// </summary>
        /// <param name="target">The target.</param>
        /// <param name="type">The type.</param>
        /// <returns></returns>
        public static dynamic CoerceConvert(object target, Type type)
        {
            var typeInfo = type.GetTypeInfo();

            if (target != null && !typeInfo.IsInstanceOfType(target) && !IsDBNull(target))
            {
                var delegateConversion = CoerceToDelegate(target, type);

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


                if (typeInfo.IsInterface && Impromptu.IsAvailable)
                {
                    if (target is IDictionary <string, object> tDict && !(tDict is DynamicObjects.BaseObject))
                    {
                        target = new DynamicObjects.Dictionary(tDict);
                    }
                    else if (!(target is DynamicObjects.BaseObject))
                    {
                        target = new DynamicObjects.Get(target);
                    }


                    target = Impromptu.DynamicActLike(target, type);
                }
                else
                {
                    try
                    {
                        object tResult = Dynamic.InvokeConvert(target, type, @explicit: true);

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

                        if (typeof(Enum).GetTypeInfo().IsAssignableFrom(tReducedType) && target is string sVal)
                        {
                            target = Enum.Parse(tReducedType, sVal, true);
                        }
                        else if (target is IConvertible && typeof(IConvertible).GetTypeInfo().IsAssignableFrom(tReducedType))
                        {
                            target = Convert.ChangeType(target, tReducedType, Net40.GetDefaultThreadCurrentCulture());
                        }
                        else
                        {
                            try
                            {
                                dynamic converter = null;
                                if (TypeDescriptor.IsAvailable)
                                {
                                    converter = TypeDescriptor.GetConverter(tReducedType);
                                }
                                else if (TypeConverterAttributeSL != null)
                                {
                                    var tAttributes =
                                        tReducedType.GetTypeInfo().GetCustomAttributes(TypeConverterAttributeSL, false);
                                    dynamic attribute = tAttributes.FirstOrDefault();
                                    if (attribute != null)
                                    {
                                        converter =
                                            Impromptu.InvokeConstructor(Type.GetType(attribute.ConverterTypeName));
                                    }
                                }


                                if (converter != null && converter.CanConvertFrom(target.GetType()))
                                {
                                    target = converter.ConvertFrom(target);
                                }
                            }
                            catch (RuntimeBinderException)
                            {
                                //This runtime converter block is a hail mary
                                //lgtm [cs/empty-catch-block]
                            }
                        }
                    }
                }
            }
        public void DynamiteyDictionaryNullMethodsTest()
        {

            dynamic tNew = new DynObj.Dictionary();

            ISimpleStringMethod tActsLike = Impromptu.ActLike<ISimpleStringMethod>(tNew);

            Assert.AreEqual(false, tActsLike.StartsWith("Te"));



        }