一、简单谈谈GetType().GetProperty(name),一般是知道类的属性名字,再给属性赋值。
代码如下:
<span style="font-size:18px;"> /// <summary>
/// 将指定的列复制给实体对象
/// </summary>
/// <typeparam name="TEntity">实体对象类型</typeparam>
/// <param name="fromEntity">来源实体</param>
/// <param name="toEntity">需要转换的实体</param>
/// <param name="propertySelector">属性选择器</param>
/// <returns></returns>
public static TEntity Mapper<TEntity>(this TEntity fromEntity, TEntity toEntity, Expression<Func<TEntity, object>> propertySelector) where TEntity : class
{
if (propertySelector == null) return toEntity;
var propertyList = (propertySelector);
if (propertyList == null) return toEntity;
bool IsUpdate = false;
if (!("ModifiedTime"))
{
IsUpdate = true;
("ModifiedTime");
}
foreach (var name in ().Where(p => (p)))
{
object value;
if (IsUpdate && name == "ModifiedTime")
{
value = ;
}
else
{
value = ().GetProperty(name).GetValue(fromEntity, null);
}
().GetProperty(name).SetValue(toEntity, value, null);
}
return toEntity;
}</span>
二
、 Expression Lamba表达式之用,主要是用于分析Lamba表达式,想进一步对Lamba表达式进行数据操作
代码如下:
/// <summary>
/// 解析表达式的字段并返回数据库列名
/// </summary>
/// <typeparam name="TEntity">实体对象</typeparam>
/// <param name="expression">表达式树</param>
/// <returns></returns>
public static List<string> GetPropertyByExpress<TEntity>(Expression<Func<TEntity, object>> expression) where TEntity : class
{
List<string> propertyList = new List<string>();
if (expression == null) return propertyList;
if ((expression != null) && (!( is NewExpression) || (( as NewExpression). == 0)))
{
return propertyList;
}
(( as NewExpression).(info => ));
return propertyList;
}