我有这样的方法:
public List<MyObjects> All<TEntity>(params LambdaExpression[] exprs)
意图是我可以这样称呼它:
All<SomeObject>(a => a.Collection1, a=> a.Collection2, a=>a.Collection3);
但是,我的方法签名似乎没有正确地使用表达式。我究竟做错了什么?如何编写方法签名以获得所需的效果?
编辑:我意识到我的示例方法调用没有准确反映我在现实生活中想要做的事情:)
谢谢!
答案 0 :(得分:1)
你的意思是什么
public List<MyObjects> All(params Action<ICollection>[] exprs)
All(a => new List<int>(), b => new List<string>(), c => new List<bool>());
答案 1 :(得分:1)
在这种情况下,最简洁的方法可能是编写扩展方法。
public static class MyExtensions
{
public static List<TEntity> All<TEntity, TResult>(
this TEntity entity,
params Func<TEntity, TResult>[] exprs)
{
if (entity == null)
{
throw new ArgumentNullException("entity");
}
if (exprs == null)
{
throw new ArgumentNullException("exprs");
}
// TODO: Implementation required
throw new NotImplementedException();
}
}
请注意,由于类型推断,在调用方法时不必指定类型参数。
class C
{
public List<string> Collection1 {get; set;}
public List<string> Collection2 {get; set;}
public List<string> Collection3 {get; set;}
// ...
}
// ...
var c = new C();
c.All(x => x.Collection1, x => x.Collection2, x => x.Collection3);