|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Linq.Expressions;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace PLCCommunication.Common
|
|
|
|
|
{
|
|
|
|
|
public class ModelTools
|
|
|
|
|
{
|
|
|
|
|
public static class PubClone<TEntity, TResult>
|
|
|
|
|
{
|
|
|
|
|
private static readonly Func<TEntity, TResult> cache = GetFunc();
|
|
|
|
|
|
|
|
|
|
private static Func<TEntity, TResult> GetFunc()
|
|
|
|
|
{
|
|
|
|
|
ParameterExpression parameterExpression = Expression.Parameter(typeof(TEntity), "p");
|
|
|
|
|
List<MemberBinding> memberBindingList = new List<MemberBinding>();
|
|
|
|
|
|
|
|
|
|
foreach (var item in typeof(TResult).GetProperties())
|
|
|
|
|
{
|
|
|
|
|
if (!item.CanWrite)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var _Property = typeof(TEntity).GetProperty(item.Name);
|
|
|
|
|
if (_Property == null)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (item.PropertyType.FullName != _Property.PropertyType.FullName)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MemberExpression property = Expression.Property(parameterExpression, typeof(TEntity).GetProperty(item.Name));
|
|
|
|
|
MemberBinding memberBinding = Expression.Bind(item, property);
|
|
|
|
|
memberBindingList.Add(memberBinding);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MemberInitExpression memberInitExpression = Expression.MemberInit(Expression.New(typeof(TResult)), memberBindingList.ToArray());
|
|
|
|
|
Expression<Func<TEntity, TResult>> lambda = Expression.Lambda<Func<TEntity, TResult>>(memberInitExpression, new ParameterExpression[] { parameterExpression });
|
|
|
|
|
|
|
|
|
|
return lambda.Compile();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static TResult Trans(TEntity tIn)
|
|
|
|
|
{
|
|
|
|
|
if (tIn == null)
|
|
|
|
|
{
|
|
|
|
|
return default(TResult);
|
|
|
|
|
}
|
|
|
|
|
return cache(tIn);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static List<TResult> TransList(List<TEntity> list)
|
|
|
|
|
{
|
|
|
|
|
List<TResult> result = new List<TResult>();
|
|
|
|
|
foreach (var item in list)
|
|
|
|
|
{
|
|
|
|
|
result.Add(Trans(item));
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|