首页 新闻 赞助 找找看

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

0
悬赏园豆:30 [已解决问题] 解决于 2018-02-07 15:58

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

复制代码
 1  // Convert the byte value to a string in base fromBase
 2         [System.Security.SecuritySafeCritical]  // auto-generated
 3         public static String ToString (byte value, int toBase) {
 4             if (toBase!=2 && toBase!=8 && toBase!=10 && toBase!=16) {
 5                 throw new ArgumentException(Environment.GetResourceString("Arg_InvalidBase"));
 6             }
 7             Contract.EndContractBlock();
 8             return ParseNumbers.IntToString((int)value, toBase, -1, ' ', ParseNumbers.PrintAsI1);
 9         }
10 
11         // Convert the Int16 value to a string in base fromBase
12         [System.Security.SecuritySafeCritical]  // auto-generated
13         public static String ToString (short value, int toBase) {
14             if (toBase!=2 && toBase!=8 && toBase!=10 && toBase!=16) {
15                 throw new ArgumentException(Environment.GetResourceString("Arg_InvalidBase"));
16             }
17             Contract.EndContractBlock();
18             return ParseNumbers.IntToString((int)value, toBase, -1, ' ', ParseNumbers.PrintAsI2);
19         }
20 
21         // Convert the Int32 value to a string in base toBase
22         [System.Security.SecuritySafeCritical]  // auto-generated
23         public static String ToString (int value, int toBase) {
24             if (toBase!=2 && toBase!=8 && toBase!=10 && toBase!=16) {
25                 throw new ArgumentException(Environment.GetResourceString("Arg_InvalidBase"));
26             }
27             Contract.EndContractBlock();
28             return ParseNumbers.IntToString(value, toBase, -1, ' ', 0);
29         }
30 
31         // Convert the Int64 value to a string in base toBase
32         [System.Security.SecuritySafeCritical]  // auto-generated
33         public static String ToString (long value, int toBase) {
34             if (toBase!=2 && toBase!=8 && toBase!=10 && toBase!=16) {
35                 throw new ArgumentException(Environment.GetResourceString("Arg_InvalidBase"));
36             }
37             Contract.EndContractBlock();
38             return ParseNumbers.LongToString(value, toBase, -1, ' ', 0);
39         }
复制代码

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

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

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

Charles Zhang的主页 Charles Zhang | 初学一级 | 园豆:38
提问于:2017-10-10 21:32
< >
分享
最佳答案
1

CLR内部方法,没有源码。

收获园豆:30
空明流光 | 初学一级 |园豆:106 | 2017-10-11 13:38
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册