예제 #1
0
        public static object GetDefaultValue(this Type type)
        {
            if (type == typeof(DBNull))
            {
                return(DBNull.Value);
            }

            if (type == null || type.IsClass || type.IsInterface || type == typeof(Nullable <>) || TypeExtension.IsAssignableFrom(typeof(Nullable <>), type))
            {
                return(null);
            }

            if (type.IsEnum)
            {
                var attribute = (DefaultValueAttribute)Attribute.GetCustomAttribute(type, typeof(DefaultValueAttribute), true);

                if (attribute != null && attribute.Value != null)
                {
                    if (attribute.Value is string)
                    {
                        return(Enum.Parse(type, attribute.Value.ToString(), true));
                    }
                    else
                    {
                        return(Enum.ToObject(type, attribute.Value));
                    }
                }

                Array values = Enum.GetValues(type);

                if (values.Length > 0)
                {
                    return(System.Convert.ChangeType(values.GetValue(0), type));
                }
            }

            return(System.Activator.CreateInstance(type));
        }
예제 #2
0
 public static object ConvertValue(object value, Type conversionType)
 {
     return(ConvertValue(value, conversionType, () => TypeExtension.GetDefaultValue(conversionType)));
 }
예제 #3
0
            public object Create(Type type, object argument, Func <ActivatorParameterDescriptor, bool> binder = null)
            {
                object result = null;

                if (type.IsArray)
                {
                    var elementType = type.GetElementType();

                    if (argument != null && argument is IEnumerable && result is IList)
                    {
                        var list = (IList)System.Activator.CreateInstance(typeof(List <>).MakeGenericType(elementType));

                        foreach (var entry in (IEnumerable)argument)
                        {
                            list.Add(Convert.ConvertValue(entry, elementType));
                        }

                        result = Array.CreateInstance(elementType, list.Count);

                        for (int i = 0; i < list.Count; i++)
                        {
                            ((Array)result).SetValue(list[i], i);
                        }

                        return(result);
                    }

                    return(Array.CreateInstance(elementType, 0));
                }

                if (TypeExtension.IsAssignableFrom(typeof(IDictionary <,>), type))
                {
                    var types = type.GetGenericArguments();

                    if (type.IsInterface)
                    {
                        result = System.Activator.CreateInstance(typeof(Dictionary <,>).MakeGenericType(types));
                    }
                    else
                    {
                        result = System.Activator.CreateInstance(type);
                    }

                    if (argument != null && result is IDictionary)
                    {
                        var entries = Collections.DictionaryExtension.ToDictionary((IEnumerable)argument);

                        foreach (var entry in entries)
                        {
                            ((IDictionary)result).Add(
                                Convert.ConvertValue(entry.Key, types[0]),
                                Convert.ConvertValue(entry.Value, types[1]));
                        }
                    }
                }

                if (typeof(IDictionary).IsAssignableFrom(type))
                {
                    if (type.IsInterface)
                    {
                        result = System.Activator.CreateInstance <Dictionary <object, object> >();
                    }
                    else
                    {
                        result = System.Activator.CreateInstance(type);
                    }

                    if (argument != null && result is IDictionary)
                    {
                        var entries = Collections.DictionaryExtension.ToDictionary((IEnumerable)argument);

                        foreach (var entry in entries)
                        {
                            ((IDictionary)result).Add(entry.Key, entry.Value);
                        }
                    }
                }

                if (TypeExtension.IsAssignableFrom(typeof(ICollection <>), type))
                {
                    var types = type.GetGenericArguments();

                    if (type.IsInterface)
                    {
                        result = System.Activator.CreateInstance(typeof(List <>).MakeGenericType(types));
                    }
                    else
                    {
                        result = System.Activator.CreateInstance(type);
                    }

                    if (argument != null && argument is IEnumerable && result is IList)
                    {
                        foreach (var entry in (IEnumerable)argument)
                        {
                            ((IList)result).Add(Convert.ConvertValue(entry, types[0]));
                        }
                    }
                }

                if (typeof(IList).IsAssignableFrom(type))
                {
                    if (type.IsInterface)
                    {
                        result = System.Activator.CreateInstance <List <object> >();
                    }
                    else
                    {
                        result = System.Activator.CreateInstance(type);
                    }

                    if (argument != null && argument is IEnumerable)
                    {
                        foreach (var entry in (IEnumerable)argument)
                        {
                            ((IList)result).Add(entry);
                        }
                    }
                }

                if (typeof(ICollection).IsAssignableFrom(type))
                {
                    if (type.IsInterface)
                    {
                        result = System.Activator.CreateInstance <List <object> >();
                    }
                    else
                    {
                        result = System.Activator.CreateInstance(type);
                    }
                }

                return(result);
            }