C# WebApi 根据实体类检查传参或字典检查参数
根据实体类或字典检查传参,是否缺少参数并返回缺少参数
值类型必须声明可空
/// <summary>
/// 根据 Dictionary<string, string> 得到实体类的字段名称和值
/// </summary>
/// <typeparam name=”T”>实体类</typeparam>
/// <param name=”t”></param>
/// <returns></returns>
public static Dictionary<string, string> GetProperties<T>(T t)
{
var ret = new Dictionary<string, string>();
if (t == null)
{
return null;
}
var properties = t.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
if (properties.Length <= 0)
{
return null;
}
foreach (var item in properties)
{
var name = item.Name; //实体类字段名称
var value = Convert.ToString(item.GetValue(t, null)); //该字段的值
if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith(“String”))
{
ret.Add(name, value); //在此可转换value的类型
}
}
return ret;
}
/// <summary>
/// 获取实体类属性返回List列表
/// </summary>
/// <typeparam name=”T”></typeparam>
/// <param name=”t”></param>
/// <returns></returns>
public static List<string> GetPropertiesToList<T>(T t)
{
List<string> srtList = new List<string>();
if (t == null)
{
return null;
}
var properties = t.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
//properties[0].PropertyType.
foreach (var item in properties)
{
if (isType(item))
{
bool bo = item.GetType().IsGenericType;
/*
*这种也可以但必须对程序集有引用
* var cType = Type.GetType(classname + “,” + dllName);//类名完整路径+程序集名称
*/
var className = “”;
var dllName = System.IO.Path.GetFileNameWithoutExtension(item.Module.Name);//获取程序集名称
if (item.PropertyType.IsGenericType)
{
var rgx = new Regex(@”(?i)(?<=\[)(.*)(?=\])”);//中括号[]
var typeName = item.GetMethod.ReturnType.FullName;
var tmp = rgx.Match(typeName).Value;//中括号[]
var tmpArray = rgx.Match(tmp).Value.Split(‘,’);
className = tmpArray[0];//获取类名
}
else
{
className = item.PropertyType.FullName;//获取类名
}
var asmb = Assembly.Load(dllName);//加载程序集
var type = asmb.GetType(className ?? throw new InvalidOperationException(“GetPropertiesToList方法中className获取类名为空”)); // 通过类名获取类的type类型
if (type != null)
{
var propertyInfos = type.GetProperties(BindingFlags.Instance | BindingFlags.Public |
BindingFlags.IgnoreCase);
srtList.AddRange(propertyInfos.Select(info => info.Name));
}
}
srtList.Add(item.Name);
}
return srtList;
//return properties.Length <= 0 ? null : (from item in properties let name = item.Name where item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith(“String”) select name).ToList();
}
/// <summary>
/// 根据Dictionary来设置实体类值
/// </summary>
/// <typeparam name=”T”></typeparam>
/// <param name=”t”></param>
/// <returns></returns>
public static T SetProperties<T>(T t, Dictionary<string, string> d)
{
if (t == null || d == null)
{
return default(T);
}
var properties = t.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
if (properties.Length <= 0)
{
return default(T);
}
foreach (var item in properties)
{
var name = item.Name; //名称
var value = Convert.ToString(item.GetValue(t, null)); //值
if (!item.PropertyType.IsValueType && !item.PropertyType.Name.StartsWith(“String”)) continue;
var val = d.FirstOrDefault(c => c.Key == name).Value;
if (val == null || val == value) continue;
if (item.PropertyType.Name.StartsWith(“Nullable`1”))
{
item.SetValue(t, Convert.ToDateTime(val), null);
}
else
{
item.SetValue(t, val, null);
}
}
return t;
}
/// <summary>
/// 根据实体列表判断参数
/// </summary>
/// <param name=”parameterList”>参数列表</param>
/// <param name=”entityList”>实体列表</param>
/// <returns></returns>
public static List<string> IsPropertiesList(Dictionary<string, string> parameterList, List<string> entityList)
{
return entityList.Where(item => !parameterList.ContainsKey(item)).ToList();
}
/// <summary>
/// 根据实体列表判断参数
/// </summary>
/// <param name=”parameterList”>参数列表</param>
/// <param name=”entityList”>实体列表</param>
/// <returns></returns>
public static string IsDictionaryProperties(Dictionary<string, string> parameterList, List<string> entityList)
{
StringBuilder sb = new StringBuilder();
foreach (var item in entityList.Where(item => !parameterList.ContainsKey(item)))
{
sb.AppendLine(item + “,”);
}
return (sb.Length > 0
&& !”null”.Equals(sb.ToString())
&& !””.Equals(sb.ToString())) ? sb.ToString().Trim() : null;
//return entityList.Where(item => !parameterList.ContainsKey(item));
}
/// <summary>
/// 实体参数检查
/// </summary>
/// <typeparam name=”T”>接收参数的实体</typeparam>
/// <param name=”t”>接收参数的实体</param>
/// <param name=”entityList”>实体类</param>
/// <param name=”isfq”>为false时只对列表循环一次</param>
/// <returns></returns>
public static string IsModelProperties<T>(T t, List<string> entityList,bool isfq=false)
{
var fq = 0;
var sb = new StringBuilder();
//var ret = new Dictionary<object, object>();
if (t == null) { return null; }
var properties = t.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
if (properties.Length <= 0) { return null; }
foreach (PropertyInfo item in properties)
{
var name = item.Name;
var value = item.GetValue(t, null);
if (value!=null)
{
if (value.GetType().IsGenericType)
{
//var typeList = typeof(List<>);
//Type typeDataList = typeList.MakeGenericType(typeof(DateTime));//通过List<>构建出List<DateTime>
if (!(value is ICollection list)) continue;
if (list.Count <= 0) continue;
foreach (var enItem in list)
{
if (!isfq && fq>=1)
{
break;
}
fq++;
var modelEn = enItem.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
foreach (var elist in from elist in entityList
from va in modelEn
where va.Name.Equals(elist, StringComparison.OrdinalIgnoreCase)
let data = va.GetValue(enItem, null)
where data == null
select elist)
{
if (!sb.ToString().ToUpper().Contains(elist.ToUpper()))
{
sb.AppendLine(“属性名称: ” + elist + “,”);
}
}
//foreach (var elist in entityList)
//{
// foreach (var va in modelEn)
// {
// if (!va.Name.Equals(elist, StringComparison.OrdinalIgnoreCase)) continue;
// var data = va.GetValue(enItem, null);
// if (data == null)
// {
// sb.AppendLine(elist + “,”);
// }
// }
//}
}
}
else
{
foreach (var elist in entityList)
{
var cou = properties.Where(n => n.Name == elist).ToList();
if (cou.Count > 0)
{
if (item.GetValue(t, null) != null) continue;
//存在等于空则添加
if (!sb.ToString().ToUpper().Contains(elist.ToUpper()))
{
sb.AppendLine(“属性名称: ” + elist + “,”);
}
}
else
{
//不存在直接添加
if (!sb.ToString().ToUpper().Contains(elist.ToUpper()))
{
sb.AppendLine(“属性名称: ” + elist + “,”);
}
}
}
}
}
else
{
//不存在直接添加
if (!sb.ToString().ToUpper().Contains(name.ToUpper()))
{
sb.AppendLine(“属性名称: ” + name + “,”);
}
}
}
return (sb.Length > 0
&& !”null”.Equals(sb.ToString())
&& !””.Equals(sb.ToString())) ? sb.ToString().Trim() : null;
}
/// <summary>
/// object转实体
/// </summary>
/// <typeparam name=”T”></typeparam>
/// <param name=”listObj”></param>
/// <returns></returns>
private static List<T> PraseList<T>(object listObj)
{
List<T> result = new List<T>();
if (!listObj.GetType().IsGenericType)
throw new Exception(“非集合类型”);
if (listObj as System.Collections.ICollection != null)
{
var list = (System.Collections.ICollection)listObj;
if (list.Count > 0)
{
foreach (var item in list)
{
result.Add((T)item);
}
}
}
return result;
}
/// <summary>
/// Type传参转换成泛型T
/// </summary>
/// <typeparam name=”T”></typeparam>
/// <param name=”inValue”></param>
/// <returns></returns>
public static T GetValue<T>(string inValue)
{
//一般类型
return (T)Convert.ChangeType(inValue, typeof(T));
}
/// <summary>
/// 判断是否常见值类型
/// </summary>
/// <param name=”item”></param>
/// <returns></returns>
public static bool isType(PropertyInfo item)
{
return !item.PropertyType.Name.StartsWith(“string”)
&& !item.PropertyType.Name.StartsWith(“int”)
&& !item.PropertyType.Name.StartsWith(“double”)
&& !item.PropertyType.Name.StartsWith(“Int32”)
&& !item.PropertyType.Name.StartsWith(“Int64”)
&& !item.PropertyType.Name.StartsWith(“IntPtr”)
&& !item.PropertyType.Name.StartsWith(“DateTime”)
&& !item.PropertyType.Name.StartsWith(“bool”);
}