新建一个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;
}
}然后在实体类中在无参构造调用
评论区