/// <summary>
/// 自动设置属性相同的值
/// </summary>
/// <typeparam name="TClass">要获取值的类</typeparam>
/// <typeparam name="T">要添加的类</typeparam>
/// <param name="objModel">要获取值的类数据</param>
/// <param name="OldObjModel">要获取值的类未修改时的数据</param>
/// <param name="_unitOfWork"></param>
/// <param name="AutoInsert">是否自动添加</param>
/// <returns></returns>
public static T AutosetSameProtity<TClass, T>(object objModel, object OldObjModel, Repository.Pattern.UnitOfWork.IUnitOfWorkAsync _unitOfWork, bool AutoInsert, bool IngoreFieldCase = false) where T : class, new()
{
//实例化 需要新增的类
T TobjModel = new T();
//try
//{
if (objModel != null)
{
#region 赋值相同项
System.Reflection.PropertyInfo[] TClass_PropertyInfos = objModel == null ? new System.Reflection.PropertyInfo[] { } : objModel.GetType().GetProperties(System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
System.Reflection.PropertyInfo[] OldTClass_PropertyInfos = OldObjModel == null ? new System.Reflection.PropertyInfo[] { } : OldObjModel.GetType().GetProperties(System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
System.Reflection.PropertyInfo[] Change_PropertyInfos = TobjModel == null ? new System.Reflection.PropertyInfo[] { } : TobjModel.GetType().GetProperties(System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
var _ChangeProtitys = Change_PropertyInfos.Where(x => x.Name.EndsWith("_Change"));
//遍历该model实体的所有字段
foreach (System.Reflection.PropertyInfo fi in TClass_PropertyInfos)
{
string DataType = fi.PropertyType.Name;
//泛型
if (fi.PropertyType.IsGenericType && fi.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
var Arguments = fi.PropertyType.GetGenericArguments();
if (Arguments.Count() == 1)
{
Type ChildType = Arguments[0];
DataType = Arguments[0].Name;
if (ChildType != null)
{
if (ChildType == typeof(DateTime) || ChildType == typeof(int) || ChildType == typeof(decimal) ||
ChildType == typeof(double) || ChildType == typeof(float) || ChildType == typeof(bool))
{
var Changefi_s = Change_PropertyInfos.Where(x => IngoreFieldCase ? (x.Name.ToUpper() == fi.Name.ToUpper()) : x.Name == fi.Name);// && x.PropertyType == fi.PropertyType
if (Changefi_s.Any())
{
var Changefi = Changefi_s.First();
object objval = fi.GetValue(objModel);
//Changefi.SetValue(TobjModel, objval);
setProtityValue(TobjModel, Changefi, objval);
}
}
}
}
}
判断是否派生自IEnumerable
//else if (fi.PropertyType.GetInterface("IEnumerable", false) != null && DataType.ToLower().IndexOf("string") < 0)
//{
//}
else
{
var Changefi_s = Change_PropertyInfos.Where(x => IngoreFieldCase ? (x.Name.ToUpper() == fi.Name.ToUpper()) : x.Name == fi.Name);// && x.PropertyType == fi.PropertyType
if (Changefi_s.Any())
{
//带_Change的为变更后数据
var Where_ChangeProtitys = _ChangeProtitys.Where(x => IngoreFieldCase ? (x.Name.ToUpper() == (fi.Name + "_Change").ToUpper()) : x.Name == (fi.Name + "_Change"));
if (Where_ChangeProtitys.Any())
{
var Changefi = Where_ChangeProtitys.First();
Changefi.SetValue(TobjModel, fi.GetValue(objModel));
var WhereOldTClass_PropertyInfos = OldTClass_PropertyInfos.Where(x => x.Name == fi.Name);
if (WhereOldTClass_PropertyInfos.Any())
{
Changefi = Changefi_s.First();
object objval = WhereOldTClass_PropertyInfos.FirstOrDefault().GetValue(OldObjModel);
//Changefi.SetValue(TobjModel, objval);
setProtityValue(TobjModel, Changefi, objval);
}
}
else
{
var Changefi = Changefi_s.First();
object objval = fi.GetValue(objModel);
//Changefi.SetValue(TobjModel, objval);
setProtityValue(TobjModel, Changefi, objval);
}
}
}
}
#endregion
if (AutoInsert)
{
#region 自动新增
//类的Type
Type type = typeof(T);
//UnitOfWork类的Type
Type UnitOfWorkType = _unitOfWork.GetType();
//获取UnitOfWork类的Repository泛型方法
MethodInfo Method = UnitOfWorkType.GetMethod("Repository");
//为Repository泛型方法 添加泛型类
Method = Method.MakeGenericMethod(type);
//类的IRepository对象
var Rep = Method.Invoke(_unitOfWork, null);
Type RepType = Rep.GetType();
//类的IRepository对象的所有方法
MethodInfo[] Methods = RepType.GetMethods();
MethodInfo InsertMethod = Methods.Where(x => x.Name == "Insert").FirstOrDefault();
if (InsertMethod != null)
{
#region 判断必填项 是否已填写
foreach (System.Reflection.PropertyInfo Changefi in Change_PropertyInfos)
{
//属性数据类型
string DataType = Changefi.PropertyType.Name;
if (GetAttributeRequired(Changefi) || GetMetaRequired(Changefi))
{
//属性值
var valobj = Changefi.GetValue(TobjModel);
if (valobj == null)
{
if (Changefi.Name == "ID")
{
Changefi.SetValue(TobjModel, 0);
}
if (Changefi.Name == "EMS_ORG_Type")
{
var EMS_ORG_Type = typeof(TClass).ToString();
var index = 0;
index = EMS_ORG_Type.LastIndexOf('.');
Changefi.SetValue(TobjModel, index > 0 ? EMS_ORG_Type.Substring(index + 1) : EMS_ORG_Type);
}
}
else
{
if (valobj.ToString() == "")
{
if (Changefi.Name == "ID")
{
Changefi.SetValue(TobjModel, 0);
}
if (Changefi.Name == "EMS_ORG_Type")
{
var EMS_ORG_Type = typeof(TClass).ToString();
var index = 0;
index = EMS_ORG_Type.LastIndexOf('.');
Changefi.SetValue(TobjModel, index > 0 ? EMS_ORG_Type.Substring(index + 1) : EMS_ORG_Type);
}
}
}
}
//去除编辑状态字段值
if (SetEditPropertyNames.Contains(Changefi.Name))
{
if (DataType.IndexOf("String") >= 0)
Changefi.SetValue(TobjModel, "");
if (DataType.IndexOf("DateTime") >= 0)
Changefi.SetValue(TobjModel, null);
}
}
#endregion
List<object> ArrParam = new List<object>() { TobjModel };
InsertMethod.Invoke(Rep, ArrParam.ToArray());
}
#endregion
}
}
//}
//catch(Exception ex)
//{
//}
return TobjModel;
}
/// <summary>
/// 自动设置属性相同的值
/// </summary>
/// <param name="SetobjModel">要设置的Model</param>
/// <param name="GetobjModel">读取数据的Model</param>
/// <returns></returns>
public static TSet SetSamaProtity<TSet, TGet>(TSet SetobjModel, TGet GetobjModel, bool IngoreFieldCase = false, System.Reflection.PropertyInfo[] Set_PropertyInfos = null, System.Reflection.PropertyInfo[] Get_PropertyInfos = null)
where TSet : class, new()
where TGet : class, new()
{
#region 赋值相同项
if (SetobjModel == null)
SetobjModel = new TSet();
if (Set_PropertyInfos == null)
Set_PropertyInfos = SetobjModel == null ? new System.Reflection.PropertyInfo[] { } : SetobjModel.GetType().GetProperties(System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
if (Get_PropertyInfos == null)
Get_PropertyInfos = GetobjModel == null ? new System.Reflection.PropertyInfo[] { } : GetobjModel.GetType().GetProperties(System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
//遍历该model实体的所有字段
foreach (System.Reflection.PropertyInfo fi in Set_PropertyInfos)
{
string DataType = fi.PropertyType.Name;
//泛型
if (fi.PropertyType.IsGenericType && fi.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
//实体泛型的类型
var Arguments = fi.PropertyType.GetGenericArguments();
if (Arguments.Count() == 1)
{
Type ChildType = Arguments[0];
DataType = Arguments[0].Name;
if (ChildType != null)
{
if (ChildType == typeof(DateTime) || ChildType == typeof(int) || ChildType == typeof(decimal) ||
ChildType == typeof(double) || ChildType == typeof(float) || ChildType == typeof(bool))
{
var WhereGetfi_s = Get_PropertyInfos.Where(x => IngoreFieldCase ? (x.Name.ToUpper() == fi.Name.ToUpper()) : x.Name == fi.Name);
if (WhereGetfi_s.Any())
{
var Getfi = WhereGetfi_s.First();
KSWECDS.Web.Extensions.Common.setProtityValue(SetobjModel, fi, Getfi.GetValue(GetobjModel));
}
}
}
}
}
判断是否派生自IEnumerable
//else if (fi.PropertyType.GetInterface("IEnumerable", false) != null && DataType.ToLower().IndexOf("string") < 0)
//{
//}
else
{
var WhereGetfi_s = Get_PropertyInfos.Where(x => IngoreFieldCase ? (x.Name.ToUpper() == fi.Name.ToUpper()) : x.Name == fi.Name);
if (WhereGetfi_s.Any())
{
var Getfi = WhereGetfi_s.First();
setProtityValue(SetobjModel, fi, Getfi.GetValue(GetobjModel));
}
}
}
#endregion
return SetobjModel;
}
/// <summary>
/// 设置相同属性值
/// </summary>
/// <typeparam name="SetType">要设置的类型 Type</typeparam>
/// <typeparam name="GetobjModel">获取相同值的 数据</typeparam>
/// <param name="IngoreFieldCase">是否区分大小写</param>
/// <returns></returns>
public static object SetSamaProtity(Type SetType, Object GetobjModel, System.Reflection.Assembly assembly = null, bool IngoreFieldCase = false, System.Reflection.PropertyInfo[] Set_PropertyInfos = null, System.Reflection.PropertyInfo[] Get_PropertyInfos = null)
{
if (assembly == null)
assembly = Assembly;
#region 赋值相同项
object SetobjModel = null;
if (SetType != null)
{
SetobjModel = Activator.CreateInstance(SetType);
}
else
return null;
if (Set_PropertyInfos == null)
Set_PropertyInfos = SetobjModel == null ? new System.Reflection.PropertyInfo[] { } : SetobjModel.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
if (Get_PropertyInfos == null)
Get_PropertyInfos = new System.Reflection.PropertyInfo[] { };
Type Get_Type = GetobjModel.GetType();
bool IsDynamic = false;
if (Get_Type.FullName.IndexOf("Dynamic") > 0)
{
IsDynamic = true;
}
else
{
if (GetobjModel != null)
{
Get_PropertyInfos = GetobjModel.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
}
else
{
return null;
}
}
if (!Set_PropertyInfos.Any())
{
System.Reflection.FieldInfo[] Set_FieldInfos = SetobjModel == null ? new System.Reflection.FieldInfo[] { } : SetobjModel.GetType().GetFields().Where(x => x.MemberType == MemberTypes.Field).ToArray();
//遍历该model实体的所有字段
foreach (System.Reflection.FieldInfo fi in Set_FieldInfos)
{
//设置值
var SetObjVal = fi.GetValue(SetobjModel);
string DataType = fi.FieldType.Name;
//获取值
object GetObjVal = null;
if (IsDynamic)
{
var WhereGetfi_s = ((IDictionary<string, object>)GetobjModel).Where(x => IngoreFieldCase ? (x.Key.ToUpper() == fi.Name.ToUpper()) : x.Key == fi.Name);
if (WhereGetfi_s.Any())
{
var Getfi = WhereGetfi_s.First();
GetObjVal = Getfi.Value;
}
}
else
{
var WhereGetfi_s = Get_PropertyInfos.Where(x => IngoreFieldCase ? (x.Name.ToUpper() == fi.Name.ToUpper()) : x.Name == fi.Name);
if (WhereGetfi_s.Any())
{
var Getfi = WhereGetfi_s.First();
GetObjVal = Getfi.GetValue(GetobjModel);
}
}
if (GetObjVal == null)
continue;
//泛型
if (fi.FieldType.IsGenericType && fi.FieldType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
//泛型 类型
var Arguments = fi.FieldType.GetGenericArguments();
if (Arguments.Count() == 1)
{
Type ChildType = Arguments[0];
DataType = Arguments[0].Name;
if (ChildType != null)
{
if (ChildType == typeof(DateTime) || ChildType == typeof(int) || ChildType == typeof(decimal) ||
ChildType == typeof(double) || ChildType == typeof(float) || ChildType == typeof(bool))
{
KSWECDS.Web.Extensions.Common.setProtityValue(SetobjModel, fi, GetObjVal);
}
}
}
}
//判断是否派生自IEnumerable(string 是特殊的数组)
else //if (fi.FieldType.GetInterface("IEnumerable", false) != null && DataType.ToLower().IndexOf("string") < 0)
if (fi.FieldType.GetInterface("IEnumerable", false) != null &&
(fi.FieldType.Name.ToLower().IndexOf("string") < 0 ||
(fi.FieldType.Name.ToLower().IndexOf("string") >= 0 &&
(fi.FieldType.Name.ToLower().IndexOf("[]") > 0 || fi.FieldType.Name.ToLower().IndexOf("<") > 0))))
{
var Arrobjval = GetObjVal as System.Collections.IEnumerable;
//是List数组还是Array数组
bool IsList = true;
#region 创建List<T> 实例 并赋值
Type ListTType = null;//泛型类
var IEnumerableTypes = fi.FieldType.GetGenericArguments();
if (IEnumerableTypes.Any())
{
//List<> 数组
ListTType = IEnumerableTypes[0];
}
else
{
//数组
ListTType = null;//数组类型
ListTType = assembly.GetType(fi.FieldType.FullName.Replace("[]", ""));
IsList = false;
}
Type ListType = typeof(List<>);
ListType = ListType.MakeGenericType(ListTType);
//创建List数组实例
var ObjListT = Activator.CreateInstance(ListType);
Type argsType = GetObjVal.GetType();
//if (argsType.GetInterface("IEnumerable", false) != null && (argsType.Name.ToLower().IndexOf("string") < 0 && argsType.Name.ToLower().IndexOf("[]") < 0 && argsType.Name.ToLower().IndexOf("<") < 0))
if (argsType.GetInterface("IEnumerable", false) != null &&
(argsType.Name.ToLower().IndexOf("string") < 0 ||
(argsType.Name.ToLower().IndexOf("string") >= 0 &&
(argsType.Name.ToLower().IndexOf("[]") > 0 || argsType.Name.ToLower().IndexOf("<") > 0))))
{
MethodInfo AddMethodInfo = ListType.GetMethod("Add");
if (AddMethodInfo != null)
{
foreach (var item in Arrobjval)
{
var obj = SetSamaProtity(ListTType, item, assembly, IngoreFieldCase);
AddMethodInfo.Invoke(ObjListT, new object[] { obj });
}
}
if (IsList)
{
KSWECDS.Web.Extensions.Common.setProtityValue(SetobjModel, fi, ObjListT);
}
else
{
MethodInfo ToArrayMethodInfo = ListType.GetMethod("ToArray");
if (ToArrayMethodInfo != null)
{
var ArrObj = ToArrayMethodInfo.Invoke(ObjListT, null);
KSWECDS.Web.Extensions.Common.setProtityValue(SetobjModel, fi, ArrObj);
}
}
}
#endregion
}
else if (fi.FieldType.IsClass && !fi.FieldType.IsPrimitive && fi.FieldType.Name.ToLower().IndexOf("string") < 0)
{
var obj = SetSamaProtity(fi.FieldType, GetObjVal, assembly, IngoreFieldCase);
KSWECDS.Web.Extensions.Common.setProtityValue(SetobjModel, fi, obj);
}
else
{
KSWECDS.Web.Extensions.Common.setProtityValue(SetobjModel, fi, GetObjVal);
}
}
}
else
{
//遍历该model实体的所有字段
foreach (System.Reflection.PropertyInfo fi in Set_PropertyInfos)
{
//设置值
var SetObjVal = fi.GetValue(SetobjModel);
string DataType = fi.PropertyType.Name;
//获取值
object GetObjVal = null;
if (IsDynamic)
{
var WhereGetfi_s = ((IDictionary<string, object>)GetobjModel).Where(x => IngoreFieldCase ? (x.Key.ToUpper() == fi.Name.ToUpper()) : x.Key == fi.Name);
if (WhereGetfi_s.Any())
{
var Getfi = WhereGetfi_s.First();
GetObjVal = Getfi.Value;
}
}
else
{
var WhereGetfi_s = Get_PropertyInfos.Where(x => IngoreFieldCase ? (x.Name.ToUpper() == fi.Name.ToUpper()) : x.Name == fi.Name);
if (WhereGetfi_s.Any())
{
var Getfi = WhereGetfi_s.First();
GetObjVal = Getfi.GetValue(GetobjModel);
}
}
if (GetObjVal == null)
continue;
//泛型
if (fi.PropertyType.IsGenericType && fi.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
//泛型 类型
var Arguments = fi.PropertyType.GetGenericArguments();
if (Arguments.Count() == 1)
{
Type ChildType = Arguments[0];
DataType = Arguments[0].Name;
if (ChildType != null)
{
if (ChildType == typeof(DateTime) || ChildType == typeof(int) || ChildType == typeof(decimal) ||
ChildType == typeof(double) || ChildType == typeof(float) || ChildType == typeof(bool))
{
KSWECDS.Web.Extensions.Common.setProtityValue(SetobjModel, fi, GetObjVal);
}
}
}
}
//判断是否派生自IEnumerable(string 是特殊的数组)
else //if (fi.PropertyType.GetInterface("IEnumerable", false) != null && DataType.ToLower().IndexOf("string") < 0)
if (fi.PropertyType.GetInterface("IEnumerable", false) != null &&
(fi.PropertyType.Name.ToLower().IndexOf("string") < 0 ||
(fi.PropertyType.Name.ToLower().IndexOf("string") >= 0 &&
(fi.PropertyType.Name.ToLower().IndexOf("[]") > 0 || fi.PropertyType.Name.ToLower().IndexOf("<") > 0))))
{
var Arrobjval = GetObjVal as System.Collections.IEnumerable;
//是List数组还是Array数组
bool IsList = true;
#region 创建List<T> 实例 并赋值
Type ListTType = null;//泛型类
var IEnumerableTypes = fi.PropertyType.GetGenericArguments();
if (IEnumerableTypes.Any())
{
//List<> 数组
ListTType = IEnumerableTypes[0];
}
else
{
//数组
ListTType = null;//数组类型
ListTType = assembly.GetType(fi.PropertyType.FullName.Replace("[]", ""));
IsList = false;
}
Type ListType = typeof(List<>);
ListType = ListType.MakeGenericType(ListTType);
//创建List数组实例
var ObjListT = Activator.CreateInstance(ListType);
Type argsType = GetObjVal.GetType();
//if (argsType.GetInterface("IEnumerable", false) != null && (argsType.Name.ToLower().IndexOf("string") < 0 && argsType.Name.ToLower().IndexOf("[]") < 0 && argsType.Name.ToLower().IndexOf("<") < 0))
if (argsType.GetInterface("IEnumerable", false) != null &&
(argsType.Name.ToLower().IndexOf("string") < 0 ||
(argsType.Name.ToLower().IndexOf("string") >= 0 &&
(argsType.Name.ToLower().IndexOf("[]") > 0 || argsType.Name.ToLower().IndexOf("<") > 0))))
{
MethodInfo AddMethodInfo = ListType.GetMethod("Add");
if (AddMethodInfo != null)
{
foreach (var item in Arrobjval)
{
var obj = SetSamaProtity(ListTType, item, assembly, IngoreFieldCase);
AddMethodInfo.Invoke(ObjListT, new object[] { obj });
}
}
if (IsList)
{
KSWECDS.Web.Extensions.Common.setProtityValue(SetobjModel, fi, ObjListT);
}
else
{
MethodInfo ToArrayMethodInfo = ListType.GetMethod("ToArray");
if (ToArrayMethodInfo != null)
{
var ArrObj = ToArrayMethodInfo.Invoke(ObjListT, null);
KSWECDS.Web.Extensions.Common.setProtityValue(SetobjModel, fi, ArrObj);
}
}
}
#endregion
}
else if (fi.PropertyType.IsClass && !fi.PropertyType.IsPrimitive && fi.PropertyType.Name.ToLower().IndexOf("string") < 0)
{
var obj = SetSamaProtity(fi.PropertyType, GetObjVal, assembly, IngoreFieldCase);
KSWECDS.Web.Extensions.Common.setProtityValue(SetobjModel, fi, obj);
}
else
{
KSWECDS.Web.Extensions.Common.setProtityValue(SetobjModel, fi, GetObjVal);
}
}
}
#endregion
return SetobjModel;
}
/// <summary>
/// 根据 类 和 字段 设置值
/// </summary>
/// <param name="TableClass"></param>
/// <param name="FiledName"></param>
/// <param name="DefaultValue"></param>
public static void setProtityValue(Object TableClass = null, string FiledName = "", object DefaultValue = null)
{
try
{
if (TableClass == null || string.IsNullOrEmpty(FiledName))
return;
System.Reflection.PropertyInfo[] PropertyInfos = TableClass.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
//遍历该model实体的所有字段
foreach (System.Reflection.PropertyInfo fi in PropertyInfos)
{
//获取字段名,用于查找该字段对应的display数据,来源List<ColumValue>
String _FiledName = fi.Name;
object s = fi.GetValue(TableClass, null);
string DataType = "";
if (fi.Name.ToLower() == FiledName.ToLower())
{
DataType = fi.PropertyType.Name;
//如果是 泛型 decimal?、List<T> 等等
if (fi.PropertyType.IsGenericType)
{
//如果是decimal?等泛型
if (fi.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
var Arguments = fi.PropertyType.GetGenericArguments();
if (Arguments.Any())
{
if (Arguments.Count() == 1)
{
DataType = Arguments[0].Name;
}
}
}
}
switch (DataType.ToLower())
{
case "int":
int Dftint = 0;
if (int.TryParse(DefaultValue.ToString(), out Dftint))
{
fi.SetValue(TableClass, Dftint, null);
}
break;
case "int32":
int Dftint32 = 0;
if (int.TryParse(DefaultValue.ToString(), out Dftint32))
{
fi.SetValue(TableClass, Dftint32, null);
}
break;
case "int64":
int Dftint64 = 0;
if (int.TryParse(DefaultValue.ToString(), out Dftint64))
{
fi.SetValue(TableClass, Dftint64, null);
}
break;
case "decimal":
decimal Dftdecimal = 0;
if (decimal.TryParse(DefaultValue.ToString(), out Dftdecimal))
{
fi.SetValue(TableClass, Dftdecimal, null);
}
break;
case "double":
double Dftdouble = 0;
if (double.TryParse(DefaultValue.ToString(), out Dftdouble))
{
fi.SetValue(TableClass, Dftdouble, null);
}
break;
case "float":
float Dftfloat = 0;
if (float.TryParse(DefaultValue.ToString(), out Dftfloat))
{
fi.SetValue(TableClass, Dftfloat, null);
}
break;
case "string":
fi.SetValue(TableClass, DefaultValue.ToString(), null);
break;
case "datetime":
int TDatetime = 0;
if (int.TryParse(DefaultValue.ToString(), out TDatetime))
{
fi.SetValue(TableClass, DateTime.Now.AddDays(TDatetime), null);
}
else
{
DateTime DftDateTime = new DateTime();
if (DateTime.TryParse(DefaultValue.ToString(), out DftDateTime))
{
fi.SetValue(TableClass, DftDateTime, null);
}
}
break;
case "bool":
bool Dftbool = false;
if (bool.TryParse(DefaultValue.ToString(), out Dftbool))
{
fi.SetValue(TableClass, Dftbool, null);
}
break;
default:
fi.SetValue(TableClass, DefaultValue, null);
break;
}
break;
}
}
}
catch
{
}
}
/// <summary>
/// 根据 类 和 字段 设置值
/// </summary>
/// <param name="TableClass"></param>
/// <param name="fi"></param>
/// <param name="DefaultValue"></param>
public static void setProtityValue(Object TableClass = null, PropertyInfo fi = null, object DefaultValue = null)
{
try
{
if (fi != null)
{
//获取字段名,用于查找该字段对应的display数据,来源List<ColumValue>
String _FiledName = fi.Name;
object s = fi.GetValue(TableClass, null);
string DataType = "";
DataType = fi.PropertyType.Name;
//如果是 泛型 decimal?、List<T> 等等
if (fi.PropertyType.IsGenericType)
{
//如果是decimal?等泛型
if (fi.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
var Arguments = fi.PropertyType.GetGenericArguments();
if (Arguments.Any())
{
if (Arguments.Count() == 1)
{
DataType = Arguments[0].Name;
}
}
}
}
switch (DataType.ToLower())
{
case "int":
int Dftint = 0;
if (int.TryParse(DefaultValue.ToString(), out Dftint))
{
fi.SetValue(TableClass, Dftint, null);
}
break;
case "int32":
int Dftint32 = 0;
if (int.TryParse(DefaultValue.ToString(), out Dftint32))
{
fi.SetValue(TableClass, Dftint32, null);
}
break;
case "int64":
int Dftint64 = 0;
if (int.TryParse(DefaultValue.ToString(), out Dftint64))
{
fi.SetValue(TableClass, Dftint64, null);
}
break;
case "decimal":
decimal Dftdecimal = 0;
if (decimal.TryParse(DefaultValue.ToString(), out Dftdecimal))
{
fi.SetValue(TableClass, Dftdecimal, null);
}
break;
case "double":
double Dftdouble = 0;
if (double.TryParse(DefaultValue.ToString(), out Dftdouble))
{
fi.SetValue(TableClass, Dftdouble, null);
}
break;
case "float":
float Dftfloat = 0;
if (float.TryParse(DefaultValue.ToString(), out Dftfloat))
{
fi.SetValue(TableClass, Dftfloat, null);
}
break;
case "string":
fi.SetValue(TableClass, DefaultValue.ToString(), null);
break;
case "datetime":
int TDatetime = 0;
if (int.TryParse(DefaultValue.ToString(), out TDatetime))
{
fi.SetValue(TableClass, DateTime.Now.AddDays(TDatetime), null);
}
else
{
DateTime DftDateTime = new DateTime();
if (DateTime.TryParse(DefaultValue.ToString(), out DftDateTime))
{
fi.SetValue(TableClass, DftDateTime, null);
}
}
break;
case "bool":
bool Dftbool = false;
if (bool.TryParse(DefaultValue.ToString(), out Dftbool))
{
fi.SetValue(TableClass, Dftbool, null);
}
break;
default:
fi.SetValue(TableClass, DefaultValue, null);
break;
}
}
}
catch
{
}
}
/// <summary>
/// 根据 类 和 字段 设置值
/// </summary>
/// <param name="TableClass"></param>
/// <param name="fi"></param>
/// <param name="DefaultValue"></param>
public static void setProtityValue(Object TableClass = null, FieldInfo fi = null, object DefaultValue = null)
{
try
{
if (fi != null)
{
//获取字段名,用于查找该字段对应的display数据,来源List<ColumValue>
String _FiledName = fi.Name;
object s = fi.GetValue(TableClass);
string DataType = "";
DataType = fi.FieldType.Name;
//如果是 泛型 decimal?、List<T> 等等
if (fi.FieldType.IsGenericType)
{
//如果是decimal?等泛型
if (fi.FieldType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
var Arguments = fi.FieldType.GetGenericArguments();
if (Arguments.Any())
{
if (Arguments.Count() == 1)
{
DataType = Arguments[0].Name;
}
}
}
}
switch (DataType.ToLower())
{
case "int":
int Dftint = 0;
if (int.TryParse(DefaultValue.ToString(), out Dftint))
{
fi.SetValue(TableClass, Dftint);
}
break;
case "int32":
int Dftint32 = 0;
if (int.TryParse(DefaultValue.ToString(), out Dftint32))
{
fi.SetValue(TableClass, Dftint32);
}
break;
case "int64":
int Dftint64 = 0;
if (int.TryParse(DefaultValue.ToString(), out Dftint64))
{
fi.SetValue(TableClass, Dftint64);
}
break;
case "decimal":
decimal Dftdecimal = 0;
if (decimal.TryParse(DefaultValue.ToString(), out Dftdecimal))
{
fi.SetValue(TableClass, Dftdecimal);
}
break;
case "double":
double Dftdouble = 0;
if (double.TryParse(DefaultValue.ToString(), out Dftdouble))
{
fi.SetValue(TableClass, Dftdouble);
}
break;
case "float":
float Dftfloat = 0;
if (float.TryParse(DefaultValue.ToString(), out Dftfloat))
{
fi.SetValue(TableClass, Dftfloat);
}
break;
case "string":
fi.SetValue(TableClass, DefaultValue.ToString());
break;
case "datetime":
int TDatetime = 0;
if (int.TryParse(DefaultValue.ToString(), out TDatetime))
{
fi.SetValue(TableClass, DateTime.Now.AddDays(TDatetime));
}
else
{
DateTime DftDateTime = new DateTime();
if (DateTime.TryParse(DefaultValue.ToString(), out DftDateTime))
{
fi.SetValue(TableClass, DftDateTime);
}
}
break;
case "bool":
bool Dftbool = false;
if (bool.TryParse(DefaultValue.ToString(), out Dftbool))
{
fi.SetValue(TableClass, Dftbool);
}
break;
default:
fi.SetValue(TableClass, DefaultValue);
break;
}
}
}
catch
{
}
}
/// <summary>
/// 根据 类 和 字段 获取值
/// </summary>
/// <param name="TableClass"></param>
/// <param name="FiledName"></param>
/// <param name="IngoreFieldCase">不区分 字段名 大小写</param>
/// <returns></returns>
public static object GetProtityValue(Object TableClass = null, string FiledName = "", bool IngoreFieldCase = true)
{
try
{
object retValue = "";
Dictionary<string, object> dict = new Dictionary<string, object>();
if (TableClass == null || string.IsNullOrEmpty(FiledName))
return null;
System.Reflection.PropertyInfo[] PropertyInfos = TableClass.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
//遍历该model实体的所有字段
foreach (System.Reflection.PropertyInfo fi in PropertyInfos)
{
//获取字段名,用于查找该字段对应的display数据,来源List<ColumValue>
String _FiledName = fi.Name;
object fival = fi.GetValue(TableClass, null);
string DataType = "";
if (IngoreFieldCase ? fi.Name.ToLower() == FiledName.ToLower() : fi.Name == FiledName)
{
var retVal = fi.GetValue(TableClass);
DataType = fi.PropertyType.Name;
//判断是否是泛型
if (fi.PropertyType.IsGenericType && fi.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
var Arguments = fi.PropertyType.GetGenericArguments();
if (Arguments.Any())
{
if (Arguments.Count() == 1)
{
DataType = Arguments[0].Name;
}
}
}
switch (DataType.ToLower())
{
case "int":
int Dftint = 0;
if (int.TryParse(retVal.ToString(), out Dftint))
{
retVal = (object)Dftint;
retValue = retVal;
}
break;
case "int32":
int Dftint32 = 0;
if (int.TryParse(retVal.ToString(), out Dftint32))
{
retVal = (object)Dftint32;
retValue = retVal;
}
break;
case "int64":
int Dftint64 = 0;
if (int.TryParse(retVal.ToString(), out Dftint64))
{
retVal = (object)Dftint64;
retValue = retVal;
}
break;
case "string":
retValue = retVal;
break;
case "datetime":
int TDatetime = 0;
if (int.TryParse(retVal.ToString(), out TDatetime))
{
retVal = (object)TDatetime;
retValue = retVal;
}
else
{
DateTime DftDateTime = new DateTime();
if (DateTime.TryParse(retVal.ToString(), out DftDateTime))
{
retVal = (object)DftDateTime;
retValue = retVal;
}
}
break;
case "bool":
bool Dftbool = false;
if (bool.TryParse(retVal.ToString(), out Dftbool))
{
retVal = (object)Dftbool;
retValue = retVal;
}
break;
case "decimal":
decimal Dftdecimal = 0;
if (decimal.TryParse(retVal.ToString(), out Dftdecimal))
{
string str = Dftdecimal.ToString("f2");
retVal = (object)str;
retValue = retVal;
}
break;
case "double":
double Dftdouble = 0;
if (double.TryParse(retVal.ToString(), out Dftdouble))
{
string str = Dftdouble.ToString("f2");
retVal = (object)str;
retValue = retVal;
}
break;
case "float":
float Dftfloat = 0;
if (float.TryParse(retVal.ToString(), out Dftfloat))
{
string str = Dftfloat.ToString("f2");
retVal = (object)str;
retValue = retVal;
}
break;
default:
retValue = retVal;
break;
}
dict.Add(DataType, fival);
break;
}
}
return retValue;// dict;
}
catch
{
return null;
}
}