Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用反射将lambda表达式作为参数传递_C#_Linq_Reflection_Linq Expressions - Fatal编程技术网

C# 使用反射将lambda表达式作为参数传递

C# 使用反射将lambda表达式作为参数传递,c#,linq,reflection,linq-expressions,C#,Linq,Reflection,Linq Expressions,我有一个通用的存储库方法调用,如下所示 var result = Repository<MyDbClass>.Get(x => x.MyProperty1 == "Something" && (!x.MyProperty2.HasValue || x.MyProperty2 == "SomethingElse")); var result=Repository.Get(x=>x.MyProperty1==“某物” &&(!x.MyProperty2.HasVa

我有一个通用的存储库方法调用,如下所示

var result = Repository<MyDbClass>.Get(x => x.MyProperty1 == "Something"
&& (!x.MyProperty2.HasValue || x.MyProperty2 == "SomethingElse"));
var result=Repository.Get(x=>x.MyProperty1==“某物”
&&(!x.MyProperty2.HasValue | | x.MyProperty2==“SomethingElse”);
我希望使用反射调用此方法。我主要寻找一种使用反射将lambda表达式作为参数传递的方法

编辑

实际上,只有在运行时才知道我的存储库类型。所有这些存储库下的表都很相似,有一些相同的列。在这些列上应用过滤器。所以我不能传递一个表达式

public void SomeMethod<T, TR>(T repository, TR dataObject)
{
    var type = repository.GetType();
    var dataType = dataObject.GetType();
    var getMethod = type.GetMethod("Get");
    //How to invoke the method by passing the lambda as parameter(??)

}
公共方法(T存储库,TR数据对象)
{
var type=repository.GetType();
var dataType=dataObject.GetType();
var getMethod=type.getMethod(“Get”);
//如何通过将lambda作为参数(?)传递来调用该方法
}
尝试传递
Func


可能重复的我将只在运行时知道我的存储库类型。为了更清楚,我对问题进行了编辑。在编译器不知道TR是什么的情况下,我真的可以形成这样的表达式吗?MyProperty1和TR之间的关系是什么?如果编译器没有关于TR类型的任何信息,则无法归档所需内容。是否存在由可能的类型实现的任何接口?我是否可以使用TR构建表达式树,因为我知道表达式中使用的TR中的属性?您可以这样做,但如果将意外类型传递给函数,这将非常危险。您知道可以传递给函数的所有类型吗?
var method = typeof(TR).GetMethod("Get");

if (method != null)
{
    method.Invoke(new Func<TR, bool>(
        (x) => x.MyProperty1 == "Something" /* etc... */));
}
public IEnumerable<TR> Get<TR>(Func<TR, bool> func)
{
    return
        db.MyDbClassEntities.Where(func);
}