简体   繁体   English

c# 创建具有变量类型的表达式函数

[英]c# Creating Expression Func with variable type

Expression selector = expression.ConvertToSelector();
Type _returnType = expression.returnType;
var genericExpression = selector as Expression<Func<TEntity, _returnType>>;

I am trying to pass the type but cant find a way of doing it efficently.我正在尝试传递类型,但找不到有效的方法。 Is there any easy way of doing it?有什么简单的方法吗?

Error Image:错误图片:
错误图片

This is not possible and to understand why, you need to look into what generic types are and what happens on build.这是不可能的,要理解原因,您需要研究什么是泛型类型以及构建时会发生什么。 Generics are compile time feature. Generics 是编译时特征。 Meaning whatever it does, it does at time of compiling (before you run application).意思是无论它做什么,它都会在编译时执行(在运行应用程序之前)。 Behind the scenes, .Net will create classes and instructions based on your code.在幕后,.Net 将根据您的代码创建类和指令。

In the sample you have shown, the variable is a runtime entity.在您显示的示例中,变量是一个运行时实体。 Compiler does not know what to do with it since the variable does not exist at compile time.编译器不知道如何处理它,因为该变量在编译时不存在。

If you know the return type of your variable while writing code, you that Type instead or if the return types could be multiple, explore inheritance or interfacing if that could be used.如果您在编写代码时知道变量的返回类型,那么您可以使用 Type 代替,或者如果返回类型可以是多个,请探索 inheritance 或接口(如果可以使用)。

In general, with any compile time feature ask this: Do I know all the types which this piece of code uses?一般来说,对于任何编译时特性,都要问这个问题:我知道这段代码使用的所有类型吗? And is there any place where my answer is "oh it could be type A or B or C which cannot be base classed or interfaced*"?有没有什么地方我的回答是“哦,它可能是类型 A 或 B 或 C,不能进行基类分类或接口*”? Unless your answers are yes and no, there is very little chance this code will compile.除非您的回答是肯定的和否定的,否则这段代码编译的可能性很小。

*Is that even a word? *这甚至是一个词吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将表达式转换为Func <Type, bool> 在c#? - Converting Expression to Func<Type, bool> in c#? C# MethodInfo 到 Callable Func - 具有通用返回类型的表达式树 - C# MethodInfo to Callable Func - Expression Tree with generic return type 通过反射创建表达式<Func <T,object >>变量 - Creating an Expression<Func<T, object>> variable by reflection C# 如何转换表达式<func<sometype> > 到表达式<func<othertype> > </func<othertype></func<sometype> - C# How to convert an Expression<Func<SomeType>> to an Expression<Func<OtherType>> c#中的Universal Func &lt;&gt;类型 - Universal Func<> type in c# Lambda帮助在C#中进行分组和汇总,需要一个Func <IGrouping<int, anonymous type, int> 或表达 <Func<IGrouping<int, anonymous type> ,十进制&gt;&gt; - Lambda Help Grouping and Summing in C#, need a Func<IGrouping<int, anonymous type, int> or Expression<Func<IGrouping<int, anonymous type>, decimal>> 将通用函数传递为 Linq 表达式 C# - Passing generic func as Linq expression C# C#生成表达式 <Func<Foo, object> &gt; - C# Generate Expression<Func<Foo, object>> C#并非所有代码路径都会在func类型的lambda表达式中返回有效值 - C# Not all code paths return a valid in lambda expression of type func 构造函数不能接受输入类型表达式<Func<T, TResult> &gt; 在 C# 中 - Constructor can't accept input type Expression<Func<T, TResult>> in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM