关于 .NET 源码中 Convert.ToString (Int16, Int32) 的实现方法,求指点。

发布于 2022-09-06 01:35:47 字数 2776 浏览 14 评论 0

今天用到 Convert.ToString (Int16, Int32) 转换进制,很好奇 .NET Framework 中是如何实现的,就查了 .NET 源码,我使用的 .NET 源码版本是 dotnet_v4.6.2_RS1,在 ndp 中 Convert.ToString (Int16, Int32) 方法的定义如下:

        // Convert the byte value to a string in base fromBase
        [System.Security.SecuritySafeCritical]  // auto-generated
        public static String ToString (byte value, int toBase) {
            if (toBase!=2 && toBase!=8 && toBase!=10 && toBase!=16) {
                throw new ArgumentException(Environment.GetResourceString("Arg_InvalidBase"));
            }
            Contract.EndContractBlock();
            return ParseNumbers.IntToString((int)value, toBase, -1, ' ', ParseNumbers.PrintAsI1);
        }

        // Convert the Int16 value to a string in base fromBase
        [System.Security.SecuritySafeCritical]  // auto-generated
        public static String ToString (short value, int toBase) {
            if (toBase!=2 && toBase!=8 && toBase!=10 && toBase!=16) {
                throw new ArgumentException(Environment.GetResourceString("Arg_InvalidBase"));
            }
            Contract.EndContractBlock();
            return ParseNumbers.IntToString((int)value, toBase, -1, ' ', ParseNumbers.PrintAsI2);
        }

        // Convert the Int32 value to a string in base toBase
        [System.Security.SecuritySafeCritical]  // auto-generated
        public static String ToString (int value, int toBase) {
            if (toBase!=2 && toBase!=8 && toBase!=10 && toBase!=16) {
                throw new ArgumentException(Environment.GetResourceString("Arg_InvalidBase"));
            }
            Contract.EndContractBlock();
            return ParseNumbers.IntToString(value, toBase, -1, ' ', 0);
        }

        // Convert the Int64 value to a string in base toBase
        [System.Security.SecuritySafeCritical]  // auto-generated
        public static String ToString (long value, int toBase) {
            if (toBase!=2 && toBase!=8 && toBase!=10 && toBase!=16) {
                throw new ArgumentException(Environment.GetResourceString("Arg_InvalidBase"));
            }
            Contract.EndContractBlock();
            return ParseNumbers.LongToString(value, toBase, -1, ' ', 0);
        }

ParseNumbers 是一个内部类,在该类下 IntToString 的实现是这样的:

        [System.Security.SecurityCritical]  // auto-generated
        [ResourceExposure(ResourceScope.None)]
        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        public extern static String IntToString(int l, int radix, int width, char paddingChar, int flags);

该方法使用了 extern 关键字,应该是调用了外部实现,但是具体调用的是哪里找不到,求大神们指点。

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

层林尽染 2022-09-13 01:35:47

MethodImplAttribute(MethodImplOptions.InternalCall) 说明了其实现位于 CLR 内部。在 .NET Core 之前微软的 CLR 好像是闭源的,所以基本上看不到其实现代码。但是从 .NET Core 开始,微软全面拥抱开源,其所谓 CoreCLR 也不例外,我从 CoreCLR 的 1.0.0 的源代码里找到了 IntToString 的 C++ 实现, 目前最新的 .NET Core 2.0+ (CoreCLR 2.0+)里已经改为 C# 实现.

我要还你自由 2022-09-13 01:35:47

看看特性里是不有定义

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文