Пример #1
0
        public static object StrongTypeValue(object value, string typeAssemblyQualifiedName)
        {
            if (string.IsNullOrEmpty(typeAssemblyQualifiedName))
            {
                throw new ArgumentNullException("typeAssemblyQualifiedName");
            }
            Type   type = Type.GetType(typeAssemblyQualifiedName);
            object result;

            if (null != type)
            {
                result = ReflectionServices.StrongTypeValue(value, type);
            }
            else
            {
                result = value;
            }
            return(result);
        }
Пример #2
0
        public static bool SetValue(object target, string propertyName, object value, bool onlyIfExists)
        {
            if (null == target)
            {
                throw new ArgumentNullException("target object");
            }
            if (string.IsNullOrEmpty(propertyName))
            {
                throw new ArgumentNullException("propertyName");
            }

            Type   targetType    = target.GetType();
            string childProperty = null;

            if (propertyName.StartsWith("'"))
            {
                propertyName  = propertyName.Substring(1);
                childProperty = propertyName.Substring(propertyName.IndexOf("'") + 1);
                if (childProperty.StartsWith("."))
                {
                    childProperty = childProperty.Substring(1);
                }

                propertyName = propertyName.Substring(0, propertyName.IndexOf("'"));
            }
            else
            {
                if (propertyName.Contains("."))
                {
                    childProperty = propertyName.Substring(propertyName.IndexOf(".") + 1);
                    propertyName  = propertyName.Substring(0, propertyName.IndexOf("."));
                }
            }

            if (string.IsNullOrEmpty(childProperty))
            {
                if (target is IDictionary <string, object> )
                {
                    return(ReflectionServices.SetValue((IDictionary <string, object>)target, propertyName, value, onlyIfExists));
                }

                if (target is IDictionary)
                {
                    ((IDictionary)target)[propertyName] = value;
                }
                else
                {
                    if (target is DataRowView)
                    {
                        return(ReflectionServices.SetValue((DataRowView)target, propertyName, value, onlyIfExists));
                    }
                    if (target is DataRow)
                    {
                        return(ReflectionServices.SetValue((DataRow)target, propertyName, value, onlyIfExists));
                    }
                    if (target is ParameterCollection)
                    {
                        return(ReflectionServices.SetValue((ParameterCollection)target, propertyName, value, onlyIfExists));
                    }
                    if (target is SqlParameterCollection)
                    {
                        return(ReflectionServices.SetValue((SqlParameterCollection)target, propertyName, value, onlyIfExists));
                    }
                    if (target is SqlParameter && "Value" == propertyName)
                    {
                        return(ReflectionServices.SetValue((SqlParameter)target, value));
                    }

                    PropertyInfo targetPropertyInfo = targetType.GetProperty(propertyName);

                    bool isIndexer = false;
                    if (null == targetPropertyInfo)
                    {
                        // try to find an indexer with one string param
                        foreach (PropertyInfo pi in targetType.GetProperties())
                        {
                            ParameterInfo[] parami = pi.GetIndexParameters();
                            if (parami.Length == 1 && parami[0].ParameterType == typeof(string))
                            {
                                targetPropertyInfo = pi;
                                isIndexer          = true;
                                break;
                            }
                        }
                    }

                    if (null == targetPropertyInfo)
                    {
                        if (onlyIfExists)
                        {
                            return(false);
                        }

                        throw new InvalidOperationException("Can't find property " + propertyName);
                    }
                    else
                    {
                        if (!targetPropertyInfo.CanWrite)
                        {
                            return(false);
                        }

                        if (DBNull.Value == value)
                        {
                            value = null;
                        }
                        if (value is string && targetPropertyInfo.PropertyType != typeof(string) && string.IsNullOrEmpty((string)value))
                        {
                            value = null;
                        }

                        object convertedValue = ReflectionServices.StrongTypeValue(value, targetPropertyInfo.PropertyType);
                        if (isIndexer)
                        {
                            targetPropertyInfo.SetValue(target, convertedValue, new object[] { propertyName });
                        }
                        else
                        {
                            targetPropertyInfo.SetValue(target, convertedValue, null);
                        }
                    }
                }
            }
            else
            {
                ReflectionServices.SetValue(ReflectionServices.ExtractValue(target, "'" + propertyName + "'"), childProperty, value);
            }

            return(true);
        }