很好用的request转换为实体方法还有判断实体所有参数不能为空的方法
/// <summary>
/// 模型辅助处理类
/// 2016-04-18
/// </summary>
public class ModelHelper
{
/// <summary>
/// 将数据转化为模型
/// 2016-04-18 基础数据类型:Int32,decimal
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t"></param>
public static void ConvertToModel<T>(ref T t)
{
System.Reflection.PropertyInfo[] properties = t.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
Type[] tempArr = new Type[] {typeof(Int32),typeof(decimal)};
object tempObject = null;
for (Int32 i = 0; i < properties.Length; i++)
{
if (ReqUtils.FormString(properties[i].Name) != null || properties[i].PropertyType.Name.ToLower() == "httpfilecollection")
{
switch (properties[i].PropertyType.Name.ToLower())
{
case "int32": tempObject = Int32.Parse(string.Format("0{0}", ReqUtils.FormString(properties[i].Name))); break;
case "decimal": tempObject = decimal.Parse(string.Format("0{0}", ReqUtils.FormString(properties[i].Name))); break;
case "httpfilecollection": tempObject = System.Web.HttpContext.Current.Request.Files; break;
default:
tempObject = string.Format("{0}", ReqUtils.FormString(properties[i].Name));
break;
}
properties[i].SetValue(t, tempObject, null);
}
}
}
/// <summary>
/// 该实体是否都为空
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public bool ModelIsNotNull(object model)
{
bool istrue = true;
Type type = model.GetType();
var flags = GetOnlyProperty();
var list = type.GetProperties(flags).ToList();
foreach (var p in list)
{
if (IsColumn(p))
{
object value = p.GetValue(model, null) ?? "";
if (string.IsNullOrWhiteSpace(value.ToString()))
{
istrue = false;
break;
}
}
}
return istrue;
}
private BindingFlags GetOnlyProperty()
{
return BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance;
}
private bool IsColumn(PropertyInfo info)
{
object[] objs = info.GetCustomAttributes(typeof(Attribute), true);
return objs.Length == 0;
}
/// <summary>
/// 获取操作提示翻译
/// </summary>
/// <param name="key"></param>
/// <param name="lan"></param>
/// <returns></returns>
public static string GetTans(string key, string lan = "")
{
try
{
if (string.IsNullOrWhiteSpace(lan))
{
lan = "cn";
}
if (lan.ToLower().Contains("cn") || lan.ToLower().Contains("zh"))
{
lan = "zh";
}
string className = "PageInfo_" + lan;
var globalResourceObject = HttpContext.GetGlobalResourceObject(className, key);
if (globalResourceObject != null)
return globalResourceObject.ToString() == "" ? key : globalResourceObject.ToString();
else
{
return key;
}
}
catch
{
return key;
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)