Ejemplo n.º 1
0
 public static object ExtractValue(DataRowView source, string columnName)
 {
     if (null == source)
     {
         throw new ArgumentNullException("source");
     }
     if (string.IsNullOrEmpty(columnName))
     {
         throw new ArgumentException("columnName is required");
     }
     return(ReflectionServices.ExtractValue(source.Row, columnName));
 }
Ejemplo n.º 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);
        }
Ejemplo n.º 3
0
        public static object ExtractValue(object source, string propertyName, string originalPropertyName)
        {
            if (null == source)
            {
                throw new ArgumentNullException("source object");
            }
            if (string.IsNullOrEmpty(propertyName))
            {
                throw new ArgumentNullException("source property name");
            }
            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("."));
                }
            }
            object val = null;

            if (source is DataRowView)
            {
                val = ReflectionServices.ExtractValue((DataRowView)source, propertyName);
            }
            else
            {
                if (source is DataRow)
                {
                    val = ReflectionServices.ExtractValue((DataRow)source, propertyName);
                }
                else
                {
                    if (source is ParameterCollection)
                    {
                        val = ReflectionServices.ExtractValue((ParameterCollection)source, propertyName);
                    }
                    else if (source is SqlParameterCollection)
                    {
                        string paramName = propertyName;
                        if (!propertyName.StartsWith("@"))
                        {
                            paramName = "@" + propertyName;
                        }
                        val = ((SqlParameterCollection)source)[paramName];
                    }
                    else
                    {
                        if (source is IDataRecord)
                        {
                            val = ReflectionServices.ExtractValue((IDataRecord)source, propertyName);
                        }
                        else
                        {
                            if (source is IList)
                            {
                                val = ((IList)source)[int.Parse(propertyName)];
                            }
                            else
                            {
                                if (source is IDictionary)
                                {
                                    val = ((IDictionary)source)[propertyName];
                                }
                                else
                                {
                                    if (source is ControlCollection)
                                    {
                                        val = ((ControlCollection)source)[int.Parse(propertyName)];
                                    }
                                    else
                                    {
                                        if (source is WebPartCollection)
                                        {
                                            val = ((WebPartCollection)source)[propertyName];
                                        }
                                        else
                                        {
                                            if (source is DataRowCollection)
                                            {
                                                val = ((DataRowCollection)source)[int.Parse(propertyName)];
                                            }
                                            else
                                            {
                                                if (source is IHierarchyData)
                                                {
                                                    val = ExtractValue((IHierarchyData)source, propertyName);
                                                }
                                                else
                                                {
                                                    Type         sourceType         = source.GetType();
                                                    PropertyInfo sourcePropertyInfo = null;
                                                    try
                                                    {
                                                        sourcePropertyInfo = sourceType.GetProperty(propertyName);
                                                    }
                                                    catch (AmbiguousMatchException ex)
                                                    {
                                                        sourcePropertyInfo = sourceType.GetProperty(propertyName, BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public);
                                                    }

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

                                                    if (null == sourcePropertyInfo)
                                                    {
                                                        throw new InvalidOperationException(string.Format("Could not find property '{0}' from '{1}'. Remaining property loop is '{2}'", propertyName, originalPropertyName, childProperty));
                                                    }

                                                    if (isIndexer)
                                                    {
                                                        val = sourcePropertyInfo.GetValue(source, new object[] { propertyName });
                                                    }
                                                    else
                                                    {
                                                        val = sourcePropertyInfo.GetValue(source, null);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            object result;

            if (string.IsNullOrEmpty(childProperty))
            {
                result = val;
            }
            else
            {
                if (null == val)
                {
                    throw new InvalidOperationException(string.Format("Found empty property '{0}' from '{1}'. Remaining property loop is '{2}'", propertyName, originalPropertyName, childProperty));
                }
                result = ReflectionServices.ExtractValue(val, childProperty, originalPropertyName);
            }
            return(result);
        }
Ejemplo n.º 4
0
 public static object ExtractValue(object source, string propertyName)
 {
     return(ReflectionServices.ExtractValue(source, propertyName, propertyName));
 }