.net core 实现常用属性设置默认值

新建一个EntitieBase类

    public class EntitieBase
    {
        public EntitieBase() {
            var obj = this;
            Type t = obj.GetType();//获得该类的Type
            foreach (PropertyInfo pi in t.GetProperties())
            {
                //获得属性的名字,后面就可以根据名字判断来进行些自己想要的操作
                var name = pi.Name;
                //用pi.GetValue获得值
                var value = pi.GetValue(obj, null);
                //获得属性值的类型
                var typeValue = value?.GetType() ?? typeof(object);
                //获得属性的类型
                Type type = Type.GetType(pi.PropertyType.FullName);
                if (value != null) continue;
                if (name.IsIn("id", "ID", "Id") && type == typeof(Int32))
                {
                    pi.SetValue(obj, 0);
                }
                else
                if (name.IsIn("Status", "status", "IsDel") && type == typeof(Int32))
                {
                    pi.SetValue(obj, 0);
                }
                else
                if (name.IsIn("CreateDate", "UpdateDate") && type == typeof(DateTime))
                {
                    pi.SetValue(obj, DateTimeOffset.Now.ToString("yyyy-MM-dd HH:mm:ss fff"));
                }
                else if (name.IsIn("UID", "UId") && type == typeof(string))
                {
                    pi.SetValue(obj, javalc.Bolg.Utils.GuidHelp.GuidUtils.NewGuidFormatN());
                }
            }
        }
        //public Initialize(){}

    }


然后新建一个实体类并继承EntitieBase

    [Serializable]
    public partial class Users:Entities.EntitieBase
    {
        public Users() {

            //默认值设置
            //ObjInvoke.GetAttributeDefault(this);
        }
        public int Id { get; set; }
        public string UserName { get; set; }
    }


或者

新建一个ObjInvoke类

    /**
 * @author wulincheng
 * @date 2020-9-21 10:47:44
 * 使用反射给实体类k赋值(默认值)
 * 可以改写此类给特定的属性赋默认值
 * insert update会报null异常,为空时不能插入和更新
 */
    public class ObjInvoke
    {
        /// <summary>
        /// 通过类型赋默认值
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static object GetTypeDefault(object obj)
        {
            Type t = obj.GetType();//获得该类的Type

            foreach (PropertyInfo pi in t.GetProperties())
            {
                //获得属性的名字,后面就可以根据名字判断来进行些自己想要的操作
                var name = pi.Name;
                //用pi.GetValue获得值
                var value = pi.GetValue(obj, null);
                //获得属性的类型
                var type = (value?.GetType() ?? typeof(object)).ToString();
                if (value != null) continue;
                if (type.IsIn("string", "String"))
                {
                    // 给属性设值
                    pi.SetValue(obj, "");
                }
                else if (type.IsIn("int", "Integer", "double"))
                {
                    pi.SetValue(obj, 0);
                }
                else if (type.IsIn("long", "Long"))
                {
                    pi.SetValue(obj, 0L);
                }
                else if (type.IsIn("Date", "date"))
                {
                    pi.SetValue(obj, DateTimeOffset.Now.ToString("yyyy-MM-dd HH:mm:ss fff"));
                }
                else if (type.IsIn("Timestamp"))
                {
                    pi.SetValue(obj, DateTimeOffset.Now.ToString("yyyy-MM-dd HH:mm:ss fff"));
                }
            }
            return obj;
        }

        /// <summary>
        /// 通过指定属性赋默认值
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static object GetAttributeDefault(object obj)
        {
            Type t = obj.GetType();//获得该类的Type

            foreach (PropertyInfo pi in t.GetProperties())
            {
                //获得属性的名字,后面就可以根据名字判断来进行些自己想要的操作
                var name = pi.Name;
                //用pi.GetValue获得值
                var value = pi.GetValue(obj, null);
                //获得属性的类型
                var type = (value?.GetType() ?? typeof(object)).ToString();
                if (value != null) continue;
                if (name.IsIn("id", "ID", "Id"))
                {
                    // 给属性设值
                    pi.SetValue(obj, GuidHelp.GuidUtils.NewGuidFormatN());
                }
                else
                if (name.IsIn("Status", "status", "IsDel"))
                {
                    pi.SetValue(obj, 0);
                }
                else
                if (name.IsIn("CreateDate", "UpdateDate"))
                {
                    pi.SetValue(obj, DateTimeOffset.Now.ToString("yyyy-MM-dd HH:mm:ss fff"));
                }
                //else if (name.IsIn("NID", "NId"))
                //{
                //    pi.SetValue(obj, 1);
                //}
            }
            return obj;
        }

    }

然后在实体类中在无参构造调用


本文作者:admin

本文链接:https://www.javalc.com/post/14.html

版权声明:本篇文章于2020-09-23,由admin发表,转载请注明出处:分享你我。如有疑问,请联系我们

C# 使用NVelocity 生成代码

发表评论

取消
扫码支持