标签:new 获取 pre sele str body 表达式 type var
/// <summary>
/// 获取指定属性信息(非String类型存在装箱与拆箱)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="select"></param>
/// <returns></returns>
public
static
PropertyInfo GetPropertyInfo<T>(Expression<Func<T,dynamic>>
select
)
{
var
body =
select
.Body;
if
(body.NodeType == ExpressionType.Convert)
{
var
o = (body
as
UnaryExpression).Operand;
return
(o
as
MemberExpression).Member
as
PropertyInfo;
}
else
if
(body.NodeType == ExpressionType.MemberAccess)
{
return
(body
as
MemberExpression).Member
as
PropertyInfo;
}
return
null
;
}
/// <summary>
/// 获取指定属性信息(需要明确指定属性类型,但不存在装箱与拆箱)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="TR"></typeparam>
/// <param name="select"></param>
/// <returns></returns>
public
static
PropertyInfo GetPropertyInfo<T, TR>(Expression<Func<T, TR>>
select
)
{
var
body =
select
.Body;
if
(body.NodeType == ExpressionType.Convert)
{
var
o = (body
as
UnaryExpression).Operand;
return
(o
as
MemberExpression).Member
as
PropertyInfo;
}
else
if
(body.NodeType == ExpressionType.MemberAccess)
{
return
(body
as
MemberExpression).Member
as
PropertyInfo;
}
return
null
;
}
/// <summary>
/// 获取类型的所有属性信息
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="select"></param>
/// <returns></returns>
public
static
PropertyInfo[] GetPropertyInfos<T>(Expression<Func<T,dynamic>>
select
)
{
var
body =
select
.Body;
if
(body.NodeType == ExpressionType.Parameter)
{
return
(body
as
ParameterExpression).Type.GetProperties();
}
else
if
(body.NodeType==ExpressionType.New)
{
return
(body
as
NewExpression).Members.Select(m => m
as
PropertyInfo).ToArray();
}
return
null
;
}
标签:new 获取 pre sele str body 表达式 type var
原文地址:http://www.cnblogs.com/shitaotao/p/7648261.html