コード例 #1
0
        public static void SetValue <T>(object propertyObject, string propertyName, T value, bool includeField = false, bool throwNoProperty = true, Type baseType = null)
        {
            if (propertyObject == null)
            {
                throw new ArgumentException("PropertyObject argument must not be null");
            }

            if (includeField)
            {
                throwNoProperty = false;
            }

            var type = baseType == null?propertyObject.GetType() : baseType;

            var property = GetProperty(type, propertyName, throwNoProperty);

            if (includeField && ((property == null) || (property != null && !property.CanWrite)))
            {
                var field = GetField(type, propertyName, throwNoProperty);

                if (field != null)
                {
                    field.SetValue(propertyObject, value);
                }
            }
            else
            {
                if (type.IsValueType)
                {
                    var delDecltype = Expression.GetDelegateType(new Type[] { type.MakeByRefType(), property.PropertyType, typeof(void) });
                    var del         = Delegate.CreateDelegate(delDecltype, property.GetSetMethod()) as dynamic;

                    del(ref propertyObject, value);
                }
                else
                {
                    var isNullable   = property.PropertyType.IsGenericType ? property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>) ? true : false : false;
                    var nullableType = isNullable ? property.PropertyType.GetGenericArguments()[0] : null;
                    var propertyType = !isNullable ? property.PropertyType : nullableType;

                    if (EnumTextValueHelper.IsEnumMember(propertyType) && value != null && propertyType != value.GetType())
                    {
                        property.SetValue(propertyObject, EnumTextValueHelper.GetEnumValue(propertyType, value.ToString()), BindingFlags.Public | BindingFlags.Instance, null, null, null);
                    }
                    else
                    {
                        var valueType = value != null?value.GetType() : null;

                        if (valueType != null && valueType != propertyType)
                        {
                            if (isNullable && (value == null || object.Equals(value, string.Empty)))
                            {
                                property.SetValue(propertyObject, null, BindingFlags.Public | BindingFlags.Instance, null, null, null);
                            }
                            else
                            {
                                property.SetValue(propertyObject, TypeHelper.ChangeType(propertyType, value), BindingFlags.Public | BindingFlags.Instance, null, null, null);
                            }
                        }
                        else
                        {
                            property.SetValue(propertyObject, value, BindingFlags.Public | BindingFlags.Instance, null, null, null);
                        }
                    }
                }
            }
        }
コード例 #2
0
        public static object ChangeType(Type type, object value)
        {
            if (type.IsEnum)
            {
                if (EnumTextValueHelper.IsEnumMember(type))
                {
                    var listEnum = EnumTextValueHelper.GetEnums(type);

                    if (listEnum != null)
                    {
                        var enumMember = listEnum.FirstOrDefault(x => x.Value != null && object.Equals(x.Value, value));

                        if (enumMember != null)
                        {
                            return(enumMember.EnumValueType);
                        }
                    }
                }
                else if (value is string)
                {
                    object returnValue = null;

                    returnValue = Enum.Parse(type, value as string);

                    return(returnValue);
                }
                else
                {
                    return(Enum.ToObject(type, value));
                }
            }
            else if (!type.IsInterface && type.IsGenericType)
            {
                Type innerType  = type.GetGenericArguments()[0];
                var  innerValue = default(object);

                if (type.GetGenericTypeDefinition() == typeof(Nullable <>) && (value == null || value.ToString() == ""))
                {
                    innerValue = null;

                    return(Activator.CreateInstance(typeof(Nullable <>).MakeGenericType(innerType), new[] { innerValue }));
                }
                else
                {
                    if (innerType.IsEnum)
                    {
                        if (EnumTextValueHelper.IsEnumMember(innerType))
                        {
                            var listEnum = EnumTextValueHelper.GetEnums(innerType);

                            if (listEnum != null)
                            {
                                var enumMember = listEnum.FirstOrDefault(x => x.Value != null && object.Equals(x.Value, value));

                                if (enumMember != null)
                                {
                                    return(enumMember.EnumValueType);
                                }
                            }
                        }
                        else if (value is string)
                        {
                            return(Enum.Parse(innerType, value as string));
                        }
                        else
                        {
                            return(Enum.ToObject(innerType, value));
                        }
                    }
                    else
                    {
                        innerValue = Convert.ChangeType(value, innerType);
                    }
                }

                return(Activator.CreateInstance(type, new object[] { innerValue }));
            }
            else if (type == typeof(bool) && !(value is bool))
            {
                if (value == null)
                {
                    return(null);
                }
                else if (value.Equals("1"))
                {
                    return(true);
                }
                else if (value.Equals("0"))
                {
                    return(false);
                }
                else if (value.Equals(true))
                {
                    return(true);
                }
                else if (value.Equals(false))
                {
                    return(false);
                }
                else
                {
                    throw new Exception($"Value not correponding a type bool ({value})");
                }
            }
            else if (type == typeof(double))
            {
                if (value == null)
                {
                    return(null);
                }
                else if (value.ToString().Contains("."))
                {
                    return(Convert.ToDouble(value, new CultureInfo("en-US")));
                }
                else
                {
                    return(System.Convert.ChangeType(value, type));
                }
            }

            if (value is System.Drawing.Bitmap && type.Equals(typeof(System.Drawing.Image)))
            {
                return(value);
            }

            if (value is IConvertible)
            {
                value = System.Convert.ChangeType(value, type);
            }

            if (type.Equals(typeof(DBNull)) && value == null)
            {
                return(DBNull.Value);
            }

            var defaultValue = ReflectionHelper.GetDefaultValue(type);

            if (value == null && value != defaultValue)
            {
                return(defaultValue);
            }

            return(value);
        }