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.
131 lines
3.9 KiB
131 lines
3.9 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="TSource"></typeparam>
|
|
/// <typeparam name="TTarget"></typeparam>
|
|
public class EntityMapper<TSource, TTarget>//Mapper`2
|
|
{
|
|
private static Func<TSource, TTarget> _FUNC = null;
|
|
static EntityMapper()
|
|
{
|
|
ParameterExpression parameterExpression = Expression.Parameter(typeof(TSource), "p");
|
|
List<MemberBinding> memberBindingList = new List<MemberBinding>();
|
|
foreach (var item in typeof(TTarget).GetProperties())
|
|
{
|
|
MemberExpression property = Expression.Property(parameterExpression, typeof(TSource).GetProperty(item.Name));
|
|
MemberBinding memberBinding = Expression.Bind(item, property);
|
|
memberBindingList.Add(memberBinding);
|
|
}
|
|
foreach (var item in typeof(TTarget).GetFields())
|
|
{
|
|
MemberExpression property = Expression.Field(parameterExpression, typeof(TSource).GetField(item.Name));
|
|
MemberBinding memberBinding = Expression.Bind(item, property);
|
|
memberBindingList.Add(memberBinding);
|
|
}
|
|
MemberInitExpression memberInitExpression = Expression.MemberInit(Expression.New(typeof(TTarget)), memberBindingList.ToArray());
|
|
Expression<Func<TSource, TTarget>> lambda = Expression.Lambda<Func<TSource, TTarget>>(memberInitExpression, new ParameterExpression[]
|
|
{
|
|
parameterExpression
|
|
});
|
|
_FUNC = lambda.Compile();//拼装是一次性的
|
|
}
|
|
public static TTarget Trans(TSource tSource)
|
|
{
|
|
return _FUNC(tSource);
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 反射实现Mapper
|
|
/// </summary>
|
|
public class EntityMapper
|
|
{
|
|
public static void Trans<TSource, TTarget>(TSource tSource, TTarget tTarget, string excludeMembers = "")
|
|
{
|
|
string[] excludeMemberArr = excludeMembers.Split(",").Select(itm => itm.Trim()).ToArray();
|
|
|
|
foreach (var itemOut in tTarget.GetType().GetProperties())
|
|
{
|
|
if (excludeMemberArr.Contains(itemOut.Name))
|
|
{
|
|
continue;
|
|
}
|
|
var propIn = tSource.GetType().GetProperty(itemOut.Name);
|
|
itemOut.SetValue(tTarget, propIn.GetValue(tSource, null), null);
|
|
}
|
|
|
|
foreach (var itemOut in tTarget.GetType().GetFields())
|
|
{
|
|
if (excludeMemberArr.Contains(itemOut.Name))
|
|
{
|
|
continue;
|
|
}
|
|
var fieldIn = tSource.GetType().GetField(itemOut.Name);
|
|
itemOut.SetValue(tTarget, fieldIn.GetValue(tSource));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|