Ejemplo n.º 1
0
        /// <summary>
        /// 设置一个对象的某个字段的值
        /// </summary>
        /// <param name="from">要设置值的对象</param>
        /// <param name="fieldName">要设置值的字段名</param>
        /// <param name="fieldValue">要设置的值</param>
        public static void Set(object from, string fieldName, object fieldValue)
        {
            //如果来源对象为空,则无法设置值
            if (from == null)
            {
                return;
            }
            //获取到来源对象的类型
            Type fromType = from.GetType();

            //如果来源类型是值类型或枚举类型,也无法设置值
            if (fromType.IsValueType || fromType.IsEnum)
            {
                return;
            }
            //如果来源类型是键值对,直接设置即可
            else if (from is IDictionary)
            {
                (from as IDictionary)[fieldName] = fieldValue;
            }
            //如果来源对象是数据行,则调用RowUtil进行设置
            else if (from is DataRow)
            {
                RowUtil.Set(from as DataRow, fieldName, fieldValue);
            }
            //如果来源对象是Json对象,则将值转换为JValue然后进行设置
            else if (from is JObject)
            {
                (from as JObject)[fieldName] = new JValue(fieldValue);
            }
            //如果来源对象是一个类,则根据类型进行设置值
            else if (fromType.IsClass)
            {
                Set(fromType, from, fieldName, fieldValue);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 获取一个对象的值
        /// </summary>
        /// <param name="from">要取值的对象</param>
        /// <param name="fieldName">要取值的字段</param>
        /// <returns></returns>
        public static object Get(object from, string fieldName)
        {
            //初始化返回值
            object result = null;

            //如果来源对象不为空就开始取值
            if (from != null)
            {
                //获取来源对象的类型
                Type fromType = from.GetType();
                //如果是值类型或者枚举类型,是不包含key的,无法获取
                if (fromType.IsValueType || fromType.IsEnum)
                {
                    result = null;
                }
                //如果是键值对类型,则用DictionaryUtil获取
                else if (from is IDictionary)
                {
                    result = DictionaryUtil.Get(from as IDictionary, fieldName);
                }
                //如果是数据行,则用RowUtil获取
                else if (from is DataRow)
                {
                    result = RowUtil.Get(from as DataRow, fieldName);
                }
                //如果是Json对象,则直接以集合的形式获取
                else if (from is JObject)
                {
                    result = Get((from as JObject)[fieldName]);
                }
                //如果是一个类
                else if (fromType.IsClass)
                {
                    //反射获取到和要取值的字段名一样的属性
                    PropertyInfo property = fromType.GetProperty(fieldName);
                    //如果属性存在,就获取属性的值
                    if (property != null)
                    {
                        result = property.GetValue(from);
                    }
                    else
                    {
                        //否则获取同名字段
                        FieldInfo field = fromType.GetField(fieldName);
                        //如果字段存在,就获取字段的值
                        if (field != null)
                        {
                            result = field.GetValue(from);
                        }
                    }
                }
                //当到这一步,如果返回结果不是空的话
                if (result != null)
                {
                    //获取到返回结果的类型
                    Type resultType  = result.GetType();
                    Type genericType = null;
                    //如果返回结果是泛型,再获取泛型的类型
                    if (resultType.IsGenericType)
                    {
                        genericType = resultType.GetGenericArguments().FirstOrDefault();
                    }
                    //如果返回结果是Json数组或者数组,或者泛型是Json对象,则把返回结果转换为List的形式,并把数组中的Json对象转换为键值对
                    if (result is JArray || result is ArrayList || (genericType != null && (genericType == typeof(JObject) || genericType.FullName == "System.Object")))
                    {
                        IList fromList = result as IList;
                        IList toList   = ConvertList(fromList);

                        result = toList;
                    }
                    //如果返回结果就是个Json对象,则把Json对象转换为键值对
                    else if (result is JObject)
                    {
                        result = result.ToDictionary();
                    }
                }
            }
            return(result);
        }