简体   繁体   English

CLR如何在内部将Double / Single转换为Decimal?

[英]How CLR converts Double/Single to Decimal internally?

I was interested to see how .NET converts Double/Single data types to Decimal , so I started Studying Decimal type struct source code that I came across to the code below. 我很想看看.NET如何将Double/Single数据类型转换为Decimal ,所以我开始研究下面的代码所涉及的Decimal类型struct源代码。

It seems All other types conversions are implemented within framework's class library except double/float which are handled externally by CLR. 似乎所有其他类型转换都是在框架的类库中实现的,除了double/float ,这是由CLR在外部处理的。

So, basically the question is How CLR does the Conversion? 因此,基本上,问题是如何进行CLR转换?

    [MethodImpl(MethodImplOptions.InternalCall)]
    public extern Decimal(float value);

    [MethodImpl(MethodImplOptions.InternalCall)]
    public extern Decimal(double value);

    public Decimal(int value)
    {
        int num = value;
        if (num < 0)
        {
            this.flags = -2147483648;
            num = -num;
        }
        else
        {
            this.flags = 0;
        }
        this.lo = num;
        this.mid = 0;
        this.hi = 0;
    }

    [CLSCompliant(false)]
    public Decimal(uint value)
    {
        this.flags = 0;
        this.lo = (int)value;
        this.mid = 0;
        this.hi = 0;
    }

    .
    .
    .

While it is not as current as the latest CLR, the general details may be confirmed in the previously released SSCLI 2 (AKA Rotor ). 虽然它不如最新的CLR最新,但一般细节可以在先前发布的SSCLI 2(AKA 转子 )中得到确认。 The native portions of System.Decimal are implemented in clr\\src\\vm\\comdecimal.cpp. System.Decimal的本机部分在clr \\ src \\ vm \\ comdecimal.cpp中实现。 Each calls VarDecFromR4 and VarDecFromR8 , respectively. 每个分别调用VarDecFromR4VarDecFromR8 These are native APIs that are exposed by OleAut32.dll. 这些是OleAut32.dll公开的本地API。

As for your next question: how does OleAut32 implement these functions? 至于下一个问题:OleAut32如何实现这些功能? You're best bet is to attach a debugger and disassemble the functions. 最好的选择是附加一个调试器并反汇编功能。 With WinDbg, you can do this with the uf command. 使用WinDbg,可以使用uf命令执行此操作。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM