You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
138 lines
4.0 KiB
138 lines
4.0 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Linq.Expressions;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Wood.Util
|
|
{
|
|
public static class CommonHelper
|
|
{
|
|
public static Guid NewGuid
|
|
{
|
|
get
|
|
{
|
|
return Guid.NewGuid();
|
|
}
|
|
}
|
|
|
|
public static string NewGuidStr
|
|
{
|
|
get
|
|
{
|
|
return Guid.NewGuid().ToString();
|
|
}
|
|
}
|
|
|
|
public static DateTime CurrentTime
|
|
{
|
|
get
|
|
{
|
|
return DateTime.Now;
|
|
}
|
|
}
|
|
|
|
public static string CurrentTimeStr
|
|
{
|
|
get
|
|
{
|
|
DateTime dt = CurrentTime;
|
|
return dt.ToString("yyyy-MM-dd HH:mm:ss");
|
|
}
|
|
}
|
|
|
|
public static string CurrentMachineName
|
|
{
|
|
get
|
|
{
|
|
return Environment.MachineName;
|
|
}
|
|
}
|
|
|
|
public static string UserName
|
|
{
|
|
get
|
|
{
|
|
return "Admin";
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Expression动态拼接+泛型缓存
|
|
/// </summary>
|
|
/// <typeparam name="TIn"></typeparam>
|
|
/// <typeparam name="TOut"></typeparam>
|
|
public class EntityMapper<TIn, TOut>//Mapper`2
|
|
{
|
|
private static Func<TIn, TOut> _FUNC = null;
|
|
static EntityMapper()
|
|
{
|
|
ParameterExpression parameterExpression = Expression.Parameter(typeof(TIn), "p");
|
|
List<MemberBinding> memberBindingList = new List<MemberBinding>();
|
|
foreach (var item in typeof(TOut).GetProperties())
|
|
{
|
|
MemberExpression property = Expression.Property(parameterExpression, typeof(TIn).GetProperty(item.Name));
|
|
MemberBinding memberBinding = Expression.Bind(item, property);
|
|
memberBindingList.Add(memberBinding);
|
|
}
|
|
foreach (var item in typeof(TOut).GetFields())
|
|
{
|
|
MemberExpression property = Expression.Field(parameterExpression, typeof(TIn).GetField(item.Name));
|
|
MemberBinding memberBinding = Expression.Bind(item, property);
|
|
memberBindingList.Add(memberBinding);
|
|
}
|
|
MemberInitExpression memberInitExpression = Expression.MemberInit(Expression.New(typeof(TOut)), memberBindingList.ToArray());
|
|
Expression<Func<TIn, TOut>> lambda = Expression.Lambda<Func<TIn, TOut>>(memberInitExpression, new ParameterExpression[]
|
|
{
|
|
parameterExpression
|
|
});
|
|
_FUNC = lambda.Compile();//拼装是一次性的
|
|
}
|
|
public static TOut Trans(TIn t)
|
|
{
|
|
return _FUNC(t);
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 反射实现Mapper
|
|
/// </summary>
|
|
public class EntityMapper
|
|
{
|
|
/// <summary>
|
|
/// 反射
|
|
/// </summary>
|
|
/// <typeparam name="TIn"></typeparam>
|
|
/// <typeparam name="TOut"></typeparam>
|
|
/// <param name="tIn"></param>
|
|
/// <returns></returns>
|
|
public static void Trans<TIn, TOut>(TIn tIn, TOut tOut, string excludeMembers = "")
|
|
{
|
|
string[] excludeMemberArr = excludeMembers.Split(",").Select(itm => itm.Trim()).ToArray();
|
|
|
|
foreach (var itemOut in tOut.GetType().GetProperties())
|
|
{
|
|
if (excludeMemberArr.Contains(itemOut.Name))
|
|
{
|
|
continue;
|
|
}
|
|
var propIn = tIn.GetType().GetProperty(itemOut.Name);
|
|
itemOut.SetValue(tOut, propIn.GetValue(tIn, null), null);
|
|
}
|
|
|
|
foreach (var itemOut in tOut.GetType().GetFields())
|
|
{
|
|
if (excludeMemberArr.Contains(itemOut.Name))
|
|
{
|
|
continue;
|
|
}
|
|
var fieldIn = tIn.GetType().GetField(itemOut.Name);
|
|
itemOut.SetValue(tOut, fieldIn.GetValue(tIn));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|