方法一的调用如下:
var propertyName = GetPropertyName<Category>(x => x.Parent.Parent.Parent.Name);
那方法二该怎么调用呢 ?就是参数(Expression<Func<T, object>>[] )该怎么传啊 ?
方法一:
protected string GetPropertyName<T>(Expression<Func<T, object>> expression)
{
return ReflectionHelper.GetPropertyNameFromExpression(expression);
}
//方法二:
protected List<string> GetPropertyNames<T>(Expression<Func<T, object>>[] expression)
{
return ReflectionHelper.GetPropertyNamesFromExpressions(expression);
}
}
}
namespace SomeNamespace
{
public class Category
{
public Categories CategoryId { get; set; }
public Category Parent { get; set; }
public string Name { get; set; }
}
public enum Categories
{
Books = 1,
Movies = 2
}
}
new Expression<Func<T, object>>[] {x => x.Parent.Parent.Parent.Name,x => x.Parent.Parent.Parent.Name}
protected U GetPropertyName<T,U>(Expression<Func<T, U>> expression)
{
return (U)(ReflectionHelper.GetPropertyNameFromExpression(expression));
}
我想知道是这个方法:
protected List<string> GetPropertyNames<T>(Expression<Func<T, object>>[] expression)
{
return ReflectionHelper.GetPropertyNamesFromExpressions(expression);
}
该怎么调用呢,Expression<Func<T, object>>[] expression 这个参数该怎么写?
var propertyNames = GetPropertyNames<Category>(new Expression<Func<Category, object>>[] { x => x.Parent.Parent.Parent.Name });
蛋疼吗?如果你可以修改方法2的话,建议加个params参数,其他不变:
protected List<string> GetPropertyNames<T>(params Expression<Func<T, object>>[] expression)
这样你就可以像方法1那样去调用方法2了,多个Expression之间用逗号隔开,而且这样的话,你的方法1甚至可以删掉不用了。