Пример #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));
 }
Пример #2
0
 public static void SetValue(object target, string propertyName, object value)
 {
     if (null == target)
     {
         throw new ArgumentNullException("target object");
     }
     if (string.IsNullOrEmpty(propertyName))
     {
         throw new ArgumentNullException("propertyName");
     }
     ReflectionServices.SetValue(target, propertyName, value, false);
 }
Пример #3
0
        public static Control FindControlEx(string id, Control parent)
        {
            Control ret = parent.FindControl(id);

            if (null == ret)
            {
                foreach (Control child in parent.Controls)
                {
                    ret = ReflectionServices.FindControlEx(id, child);
                    if (null != ret)
                    {
                        break;
                    }
                }
            }
            return(ret);
        }
Пример #4
0
        public static bool SetValue(SqlParameterCollection target, string paramName, object value, bool onlyIfExists)
        {
            if (null == target)
            {
                throw new ArgumentNullException("target");
            }
            if (string.IsNullOrEmpty(paramName))
            {
                throw new ArgumentNullException("paramName");
            }
            if (!paramName.StartsWith("@"))
            {
                paramName = "@" + paramName;
            }
            SqlParameter param = target[paramName];

            return(ReflectionServices.SetValue(param, value));
        }
Пример #5
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);
        }
Пример #6
0
        public static Dictionary <string, object> ToDictionary(object target)
        {
            Dictionary <string, object> result2;

            if (null == target)
            {
                result2 = null;
            }
            else
            {
                if (target is Dictionary <string, object> )
                {
                    result2 = (target as Dictionary <string, object>);
                }
                else
                {
                    if (target is KeyValuePair <string, object> )
                    {
                        KeyValuePair <string, object> kvp = (KeyValuePair <string, object>)target;
                        result2 = new Dictionary <string, object>
                        {
                            {
                                kvp.Key,
                                kvp.Value
                            }
                        };
                    }
                    else
                    {
                        if (target is DataRowView)
                        {
                            result2 = ReflectionServices.ToDictionary((DataRowView)target);
                        }
                        else
                        {
                            if (target is DataRow)
                            {
                                result2 = ReflectionServices.ToDictionary((DataRow)target);
                            }
                            else
                            {
                                if (target is IList)
                                {
                                    result2 = ReflectionServices.ToDictionary((IList)target);
                                }
                                else
                                {
                                    if (target is IDictionary)
                                    {
                                        result2 = ReflectionServices.ToDictionary((IDictionary)target);
                                    }
                                    else
                                    {
                                        if (target is IDataRecord)
                                        {
                                            result2 = ReflectionServices.ToDictionary((IDataRecord)target);
                                        }
                                        else
                                        {
                                            if (target is IHierarchyData)
                                            {
                                                result2 = ToDictionary((IHierarchyData)target);
                                            }
                                            else
                                            {
                                                Dictionary <string, object> result3 = new Dictionary <string, object>();
                                                PropertyInfo[] properties           = target.GetType().GetProperties();
                                                for (int i = 0; i < properties.Length; i++)
                                                {
                                                    PropertyInfo propertyInfo = properties[i];
                                                    if (0 == propertyInfo.GetIndexParameters().Length)
                                                    {
                                                        result3.Add(propertyInfo.Name, propertyInfo.GetValue(target, null));
                                                    }
                                                }
                                                result2 = result3;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(result2);
        }
Пример #7
0
 public static void SetValue(DataRow target, string columnName, object value)
 {
     ReflectionServices.SetValue(target, columnName, value, false);
 }
Пример #8
0
 public static bool SetValue(DataRowView target, string columnName, object value, bool onlyIfExists)
 {
     return(ReflectionServices.SetValue(target.Row, columnName, value, onlyIfExists));
 }
Пример #9
0
 public static bool SetValue(IDictionary <string, object> target, string key, object value)
 {
     return(ReflectionServices.SetValue(target, key, value, false));
 }
Пример #10
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);
        }
Пример #11
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);
        }
Пример #12
0
 public static object ExtractValue(object source, string propertyName)
 {
     return(ReflectionServices.ExtractValue(source, propertyName, propertyName));
 }
Пример #13
0
 public static Dictionary <string, object> ToDictionary(DataRowView target)
 {
     return(ReflectionServices.ToDictionary(target.Row));
 }