使用Reflector反射.NET源码比较Console.Write(char[].ToString()) 与Console.Write(char[])

 

文章作者:李元波

文章出处:http://blog.csdn.net/ThunderLight/archive/2009/11/04/4767889.aspx

欢迎转载,转载时请注明出处。谢谢合作。

 

 

首先看一下下面的代码,这两个输出都会是什么结果呢?

char[] datas = {'a','b','c','d'};

Console.Write(datas.ToString() );

Console.Write(datas );

 

答案是:

System.Char[]    abcd

 

问题1datas.ToString()怎么会转换成 System.Char[]

问题2Console.Write(datas )为什么能把里面的字符都显示出来?

 

l  首先来解决第一个问题:

datas.ToString()怎么会转换成 System.Char[]

dataschar[]数组,也是Array的一个实例,那么我们一起到MSDN里面找一下Array里面的ToString()方法。内容如下:  

 

ToString

返回表示当前 Object String (继承自 Object。)

   我们发现,原来数组中的ToString()继承自Object,那么objectToString()又是完成了什么工作呢?现在隆重推出一个重量级人物.NET Reflector

.NET Reflector何许人也?

这是一款功能很强大.NET反射工具,用它的创造者说就是可以“Explore, browse, and analyze .NET assemblies”,所以在这里我们可以使用这个工具轻而易举的查看C#的源代码了,因此我们可以使用这个工具来看一下Object.ToString()到底做了什么事情。

另外大家可以通过http://www.red-gate.com/products/reflector/来下载Reflector

Object.ToString() 具体代码如下:

public virtual string ToString()

{

    return this.GetType().ToString();

}

 

 

到这里我们立刻明白了,datas.ToString()之所以为 System.Char[],实际上显示的是this.GetType()的内容。 说到这里有的朋友会疑问this.GetType()后面不是还有一个ToString()吗?确实是这样不过它已经被Type 重写了。

 

 

l  接着来解决第二个问题:

Console.Write(datas )为什么能把里面的字符都显示出来?

我们依然使用Reflector去查看Console.Write(Array)或者 Console.Write(char[]),很幸运使用Reflector我们找到了Console.Write(char[]),具体代码如下:

public static void Write(char[] buffer)

{

    Out.Write(buffer);

}

 

 

然后继续查看Out.Write(buffer);

这时候我们发现:

²  OutConsole的一个TextWriter属性,原型如下:

ü   public static TextWriter Out { get; }

²  继续查看TextWriterWrite(char[] buffer)方法得到以下代码:

1.   public virtual void Write(char[] buffer)

2.   {

3.      if (buffer != null)

4.     {

5.        this.Write(buffer, 0, buffer.Length);

6.      }

7.   }

 

 

²  继续查看第7行代码中的Write(..)方法得到以下代码:

 

public virtual void Write(char[] buffer, int index, int count)

{

    //这里省略一些代码

  for (int i = 0; i < count; i++)

    {

        this.Write(buffer[index + i]);

    }

}

 

 

²  至此我们看到了原因了:这里用了一个循环遍历数组的方式来显示其数据。

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Red-Gate Reflector 7.6.0.233 VS Pro Edition Pre-cracked by HF-Death / InFiNiTy-Team Debug any .NET assembly in Visual Studio : Enable debugging for 3rd party assemblies Quickly select assemblies to debug within Visual Studio. .NET Reflector generates PDB files for decompiled assemblies, so you can debug them as easily as your own code. Debug decompiled assemblies using the Visual Studio debugger Now you can step through debugging of any decompiled assemblies as if they were your own. Set breakpoints, watch variables, set next statements, modify variable values, or use any of your other regular debugging techniques. Decompile and explore assemblies on the fly: Dynamic decompilation With Reflector VSPro, assemblies decompile seamlessly in Visual Studio. So source code is always available for your libraries, and you can go straight to the definition of any code, either from the context menu, or by hitting F12 Browse decompiled assemblies .NET Reflector VSPro adds the Reflector Object Browser into Visual Studio. Use the simple tree view to navigate through all types and members, and click to see the decompiled code. A 2-Minute Demo of .NET Reflector VSPro Debug any .NET assembly in Visual Studio: Enable debugging for 3rd party assemblies Quickly select assemblies to debug within Visual Studio. .NET Reflector generates PDB files for decompiled assemblies, so you can debug them as easily as your own code. Select assemblies to decompile and debugSelect assemblies to debug The PDB files are saved for future use, so you only need to enable debugging onc for each assembly. Debug decompiled assemblies using the Visual Studio debugger Now you can step through debugging of any decompiled assemblies as if they were your own. Set breakpoints, watch variables, set next statements, modify variable values, or use any of your other regular debugging techniques. Debug decompiled assemblies using the Visual Studio debuggerDebug decompiled assemblies using the Visual Studio debugger Decompile and explore assemblies on the fly: Dynamic decompilation With Reflector VSPro, assemblies decompile seamlessly in Visual Studio. So source code is always available for your libraries, and you can go straight to the definition of any code, either from the context menu, or by hitting F12 Decompile any referenced assemblyDecompile any referenced assembly Browse decompiled assemblies .NET Reflector VSPro adds the Reflector Object Browser into Visual Studio. Use the simple tree view to navigate through all types and members, and click to see the decompiled code. Browse decompiled assembliesBrowse decompiled assemblies Back to top The world’s most advanced .NET decompiler: .NET Reflector VS can decompile any .NET assembly to C#, VB.NET or IL. .NET Reflector VS will decompile to high level C# features such as Iterator blocks, Lambda expressions, and LINQ queries. .NET Reflector also supports the C# 4 dynamic type. DotNET Reflector 7.5 Changes: * Many string changes to make things easier to understand * Assorted design changes to dialogs and screens * Improvements to the installation experience of the package * Improved the speed of type by type decompilation * Improved handling of the case where the add-in is superseded by the package (we remove the old menu items) * Error reporting experience is more fine-tuned. Users have options to be notified of work-arounds and fixes * Hide menu items such as “enable debugging”, instead of just disabling them * Make “enable debugging” work on all items of the tree, rather than just the top level assembly item * "Go to decompiled definition" more commonly enabled. * Setting a break point causes the necessary PDB file to be automatically generated * Decompiled code can now be stepped-into like any other code * Attempting to step into inaccessible code triggers instructions on how to debug it correctly * Multiple PDBs can now be generated in parallel o PDB generation can currently take a little time, especially if you’re generating several or the assemblies are particularly large * The trial dialog screen has been updated to be more informative and clear * Re-introduced “Go to Decompiled Definition” right-click menu item * Improved support for ‘Go to Definition’(F12) in code without source * A number of simple usability enhancements * A number of licensing and installation bug fixes * Added support for ‘Go to Definition’(F12) in code without source * Improved path to decompiled code * Improved path to pdb generation for code you want to debug * Completed move to a VS Package * Started transition from a VS Add-in to a VS Package * Added support for VS11 * Turned on SmartAssembly feature usage reporting on by default for all EA builds * Added loaded project references to the Reflector Object Browser (ROB) * Double click on any type in the ‘ROB’ to decompile in a new VS editor pane * Added a Reflector ‘Go to Definition’ context menu item to navigate through code without source * Added a new ‘Reflector Object Browser’(ROB) into Visual Studio (will eventually offer decompilation by type inside VS) * Changed the way Reflector shows the version number to display the correct build number * Bug fixes: o “Open in Reflector” context menu not working. o XAML Source Code is wrong delimited. o Missing end tag in XAML Translation. o Code generation for different versions of the same assembly o ‘Flatten Namespaces’ dialog o Various decompilation and assembly-loading problems have been fixed – drop us a note if you’d like to know more. DotNET Reflector 7.6.0.233 VS Pro Edition Cracked HF-Death / InFiNiTy-Team
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值