gpt4 book ai didi

c# - Linq 表达式获取插入的 Convert 函数

转载 作者:行者123 更新时间:2023-11-30 13:38:47 24 4
gpt4 key购买 nike

在我的某些属性的项目中,形式 (x=>x.property) 的表达式在运行时显示为 (x=>Convert(x.property)),如下所示:

enter image description here

这取决于属性类型,double 和 DateTime 似乎是罪魁祸首。适用于字符串属性(例如 Speed 和 ForeColour 都是字符串)

为什么会这样?

最佳答案

doubleDateTime 是值类型。基本上,编译器使用 Expression.Convert 来表示装箱操作。

string 已经是引用类型,不需要转换。

你可以在普通代码中看到相同的东西:

double d = 0.5;
string s = "hello";

object o1 = d;
object o2 = s;

...编译为:

// d = 0.5
IL_0001: ldc.r8 0.5
IL_000a: stloc.0

// s = "hello"
IL_000b: ldstr "hello"
IL_0010: stloc.1

// o1 = d - boxing!
IL_0011: ldloc.0
IL_0012: box [mscorlib]System.Double
IL_0017: stloc.2

// o2 = s - no boxing required!
IL_0018: ldloc.1
IL_0019: stloc.3

关于c# - Linq 表达式获取插入的 Convert 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15077803/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com