根据属性名获取或设置值

#region 根据属性名获取或设置值

        /// <summary>
        /// 根据属性名获取值
        /// </summary>
        /// <param name="model">实体</param>
        /// <param name="modelName">属性名</param>
        /// <returns></returns>
        public static object GetValueByModelName(object model, string modelName)
        {
            try
            {
                object currentModel = null;
                PropertyInfo property = GetPropertyInfoByModelName(model, modelName, ref currentModel);
                if (property != null && currentModel != null)//属性和当前属性对应的model不为空
                {
                    return property.GetValue(currentModel, null) ?? "";
                }
                else
                {
                    return "";
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog($"获取{modelName}异常", ex);
                return "";
            }
        }

        /// <summary>
        /// 根据属性名设置属性值
        /// </summary>
        /// <param name="model">实体</param>
        /// <param name="modelName">属性名</param>
        /// <param name="setValue">设置的值</param>
        public static void SetValueByModelName(object model, string modelName, string setValue)
        {
            try
            {
                object currentModel = null;
                PropertyInfo property = GetPropertyInfoByModelName(model, modelName, ref currentModel);
                if (property != null && currentModel != null)//属性和当前属性对应的model不为空
                {
                    if (property.PropertyType.Name == "CData")
                    {
                        CData cData = new CData();
                        cData.Value = setValue.ToString();
                        property.SetValue(currentModel, cData, null);
                    }
                    else
                    {
                        property.SetValue(currentModel, setValue, null);
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog($"设置{modelName}值异常", ex);
            }

        }
        /// <summary>
        /// 根据实体属性名获取属性信息
        /// </summary>
        /// <param name="model">实体</param>
        /// <param name="modelName">属性名</param>
        /// <param name="currentModel">属性对应的实体</param>
        /// <returns></returns>
        public static PropertyInfo GetPropertyInfoByModelName(object model, string modelName, ref object currentModel)
        {
            PropertyInfo result = null;//返回结果
            try
            {
                if (model != null && !string.IsNullOrEmpty(modelName))//实体和需要获取的属性名都不为空
                {
                    //先直接找,是否有改属性
                    PropertyInfo property = model.GetType().GetProperty(modelName);
                    if (property != null)
                    {
                        currentModel = model;
                        return property;
                    }
                    foreach (var item in model.GetType().GetProperties())//遍历实体里的所有公开属性
                    {
                        //不是基元类型 && 是类 && 不是泛型  && 命名空间不是System.开头的
                        if (
                            !(
                            item.PropertyType.IsPrimitive
                            || item.PropertyType.IsEnum
                            || item.PropertyType.Equals(typeof(string))
                            || item.PropertyType.Equals(typeof(CData))
                            || item.PropertyType.Equals(typeof(DateTime))
                            || item.PropertyType.Equals(typeof(Module))
                            )
                            && item.PropertyType.IsClass
                            && !(item.PropertyType.IsGenericType && item.PropertyType.GetGenericTypeDefinition() == typeof(List<>))
                            && !item.PropertyType.FullName.StartsWith("System.")
                            )
                        {
                            PropertyInfo ItmeProperty = model.GetType().GetProperty(item.Name);
                            if (ItmeProperty != null)
                            {
                                var objModel = ItmeProperty.GetValue(model, null);
                                result = GetPropertyInfoByModelName(objModel, modelName, ref currentModel);
                            }
                            if (result != null)
                            {
                                return result;
                            }
                        }
                    }
                    return null;
                }
                else
                {
                    //如果出现没有实例化子表的情况,会返回null,所以在此处理一下
                    result = null;
                }
                return result;
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog($"获取{modelName}异常", ex);
            }
            return result;
        }

        #endregion

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@HuGe

捐款买咖啡,更新飞快!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值