Effective C# 原则48:了解更多的工具和资源
Item 48: Learn About Tools and Resources
对于C#以及.Net来说这是激动人心的时候。这些工具目前还是比较新的,整个社区都在学习如何使用这些工具。一些资源可以帮助你提高你的知识,以及为.Net和C#创建一个更大的知识社区。这些工具是我每天都向C#开发人员推荐的。关于C#实践的全部内容还在写作当中,跟进它们而且不断了解相关的内容。
第一个应该在每一个C#开发人员的工具箱的工具是NUnit, 它可以在www.nunit.org网站上找到。NUnit是一个自动进行单元测试的工具,功能和JUnit很像。和其它大多数开发人员一样,我讨厌写测试代码并且自己测试。NUnit让这些进程都变得很高效,在你有规律的使用这些工具后,可以保证你会习惯测试你所有的C#类。不管什么时候当我创建了一个类库工程时,我都会添加一个NUnit测试工程,而且把自动生成的测试做为一部分添加进来。我添加一个以创建和运行测试的配置,这样可以在每次编译时进行测试。然后,我可以转换活动的配置来控制是否要让单元测试做为正规程序的一部份存在。默认情况下,我运行它们。当我须要进行UI测试时,我会转换到另一个配置上。
在附带的使用NUnit时,你可以通过检测NUnit的源代码学到一些有意思的技术。NUnit使用一些高级的反射习惯来加载和测试你的程序集。它使用特性来查找测试包,测试用例,以及每个测试用例的期望结果(参见原则42)。这是一个非常不错的例子,可以告诉你如何使用这些技术来创建可以自己动态配置的工具,而且它可以广泛的应用。
接来下是FXCop,这是一个免费的工具,可以从GotDotNet(www.gotdotnet.com)上得到。FXCop 分析你的程序集里的IL,看它是否与实践的原则相违背,以及报告这些违例的地方。每一个原则都有一个可靠的公制规范,以及使用这一原则的原因。如本书里所有推荐的原则一样,一些文档中有一个关于某一原则的简短理由。你可以断定这些实际的问题是否遵守这些建议。同样你也可以配置是否把每一个原则都应用到项目中。我并不赞成FXCop 中的一些原则,而且我在本书前面已经说明了原因。然而,像NUnit一样,FXCop 可以成为你创建的正规程序的一部分。每次编译后,可以有一个编译后步骤,可以用FXCop 来分析你选择的原则。图6.1展示了一个从FXCop里输出的例子。尽管一些推荐并不是我喜欢的(例如有一个是让每一个程序集应该是让COM见的),但它确实是一个有用的工具,因为它让你思考很多你已经默认的决定。
图6.1,FXCop分析的一个项目:
Figure 6.1. FXCop analyzing a project.
[View full size image]
ILDasm是一个IL反汇编器,在本书不同的地方,我已经演示了一些IL代码,它们就是编译器为不同的C#结构生成的。尽管我不相信很多人会在有高级语言存在的情况下,还选择写IL代码,但你应该熟悉它。知道从不同的C#结构上生成的IL代码,可以帮助你成为一个更好的开发者。你可以为你自己的程序集检测IL代码,或者是.Net框架里的程序集。这就是使用ILDAsm,而且它是和.Net框架的SDK一起发布的。IL对于所有开发者来说都是可用的。ILDAsm可以让你看到你的程序集的中间语言。不管怎样,这是一个好的方法来学习.Net框架程序集,这也是得到的原始资料。
这些只是你正式工具箱中的一部份,但拥有这些工具只是提高你技能的一个方面。大量在线的资源以及交流社区可以让你参与和学习,以及增加你自己的C#和.net框架知识。首先也是最重要的就是GotDotNet 网站(www.gotdotnet.com),这是.Net组的官方网站。C#小组在MSDN上有一个站点,目前是在msdn.microsoft.com/vcsharp/ (它偶然在MSDN网站有变动而被重新组织)。如果你的工作主要是基于网络的,试着访问www.asp.net,这是为ASP.Net组提供的。如果你的工作主要是基于Windows Form的,试着看看www.windowsforms.net,这是Windows Form组的官方网站。这些网站包含很多常规编程的引用和实现,这些可能是你的应用程序中想要的。它们都是还源文件的组件,所以你可以检测和修改这些,让它们成为你想要的。最后也是最重要的位置应该要了解就是在MS模式和实践的网页。这个网页目前在 www.microsoft.com/resources/practices/,从这个地方,你可以查到一些常用的设计模式以及一些最好的模式的初始代码。
而且这个地方经常更新一些新的例子的代码以及库,这可能帮助你解决常规的编程问题。在写这些时,你已经可以使用10个不同的应用程序块来实现一些常规的程序要求,我确信当你阅读到这些时,这些地方已经有更多的内容了。
我还要推荐一些C#组的FAQ的订阅:http://blogs.msdn.com/csharpfaq,附带的在这个上,有几个C#组的成员用博客讨论一些C#问题。你可以在这里找到最新的列表: http://msdn.microsoft.com/vcsharp/team/blogs/
如果你学习更多的而且对语言和环境想得到更深入的了解,你可以检测共享的CLI(code-named rotor)。这包含.net框架以及C#编译器的一些核心内容。你可阅读这些资料来对C#语言的每一个功能和.Net框架得到更深入的理解。并不是所有的.net商业框架都有可用的共享资料:例如,特殊的Windows代码并没有发布共享代码。然而,这些已经发布了的子集,同样够你学习更多的关于CLR和C#语言内部的东西。
C#编译器已经和共享的CLI资料一起发布,这是用C++写的,它做为底层的CLR代码存在。你须要对C++有很深的背景知识,以及对编译器设计有清楚的认识才能很好的理解它。现代的语言编译器是复杂的软件块,但CLR资料是一个有用的工具,来理解.Net框架的核心功能是如何实现的。
这里只是给出了一个简单的列表, 我只是在众多资料中介绍了一个表面。很多资料你都可以多MS上得到,或者其它在线网站,或者是书。你越是多的使用这些工具,你就可以得到越多的知识。整个.Net以及C#社区是在前进的,因为它发展的很快,这些列出的资源也可能不断的在改变。你可以自己学习和自己写稿。
============================
Item 48: Learn About Tools and Resources
These are exciting times for C# and .NET. These tools are still new enough that the entire community is learning how best to use them. Several resources are available to help you improve your knowledge and build a larger community of knowledge for .NET and C#. These are the tools that I use daily and recommend to other C# developers. The full set of C# best practices is still being written. Keep up, and get involved.
The first tool that should be in every C# developer's toolbox is NUnit, available on the web at www.nunit.org. NUnit is an automated unit-test tool, functionally similar to JUnit. Like most developers, I hate writing tests and testing my code. NUnit makes that process so efficient that using it regularly ensures that you will be in the habit of testing all your C# classes. Whenever I make a class library project, I add an NUnit test project and include executing the tests as part of the automated build. I add new configurations that include build and test, which run the tests on every compile. Then I can switch the active configuration to control whether unit tests are run as part of the regular build process. By default, I run them. I switch to the other configuration when I'm running tests that require the UI.
In addition to using NUnit, you can learn several interesting techniques by examining the NUnit source code. NUnit uses some advanced reflection idioms to load and test your assemblies. It uses attributes to find test suites, test cases, and expected results from each test case (see Item 42). It's a great example of how to use these techniques to build a tool that configures itself dynamically, and it can be used in a wide variety of ways.
Next is FXCop, a free tool available at GotDotNet (www.gotdotnet.com). FXCop analyzes the IL in your assembly against a set of rules and best practices, and reports violations. Each rule has a reliability metric and a reason for the rule. As with all the recommendations in this book, the rule documentation has a brief justification for the advice. You can then determine whether the advice fits your particular problem space. You can also configure whether each rule is applied in your project. I disagree with some of the FXCop rules, and I've said as much earlier in this book. However, like NUnit, FXCop can become part of your regular build process. Each build can have a post-build step that analyzes the code using FXCop with your chosen rules. Figure 6.1 shows a sample output from FXCop. Although some of the recommendations are not to my liking (such as the one that every assembly should be COM visible), it's a useful tool because it makes you think about many decisions you might have made by default.
Figure 6.1. FXCop analyzing a project.
[View full size image]
IldAsm is an IL disassembler. In different locations in this book, I've shown the IL that the compiler generates for different C# constructs. Although I don't believe that many people are choosing to write IL in favor of any high-level language, you should be familiar with it. Knowing the IL that gets generated from different C# constructs will help you be a better developer. You can examine the IL for your own assemblies or for assemblies in the .NET Framework by using IldAsm, which comes with the .NET Framework SDK. The IL is available to all developers. IldAsm lets you see the intermediate language for your assemblies. However, a better way to learn about the .NET Framework assemblies you use is to get the source.
Those are the tools that are part of your regular toolbox. But having the tools is only one way to improve your skill. A variety of online resources and communities enables you to participate and learn and increase your knowledge of C# and the .NET Framework. First and foremost is the GotDotNet site (www.gotdotnet.com), the official site of the .NET team. The C# team has a page on the MSDN site, currently located at msdn.microsoft.com/vcsharp/ (it moves occasionally as the MSDN site gets reorganized). If your work is primarily web-based, try www.asp.net, the site for the ASP.NET team. If your work is Windows Formsbased, try www.windowsforms.net, the official site of the Windows forms team. These sites contain reference implementations of many common idioms that you will want to make use of in your applications. They all come with source components, so you can examine and modify them as you need to for your purposes. The last and most important location to become familiar with is the MS Patterns & Practices page. This page is currently located at www.microsoft.com/resources/practices/. From this location, you will find common patterns and starter code for those best practices. This area continues to be updated with more sample code and libraries that will help you solve common programming problems. At this writing, you can use 10 different application blocks to implement common programming requirements; I'm sure there are already more by the time you read this.
I also recommend subscribing to the C# Team FAQ: http://blogs.msdn.com/csharpfaq. In addition to that one, several of the C# team members have blogs where they discuss C# issues. You can find the up-to-date list at http://msdn.microsoft.com/vcsharp/team/blogs/.
If you want to learn more and get an even deeper understanding of the language and the environment, examine the shared source CLI (code-named rotor). This includes the core .NET Framework and a C# compiler. You can read the source code to gain an even deeper understanding of each feature in the C# language and the .NET Framework. Not every assembly in the commercial .NET Framework is available in the shared source version: For example, the Windows-specific code is not delivered with the shared source code. However, what is delivered is a rich subset that you can use to learn much about the inner workings of the CLR and the C# language.
The C# compiler delivered with the shared source CLI is written in C++, as is part of the low-level CLR code. You need to have a strong background in C++ and a strong understanding of compiler design to understand it thoroughly. Modern-language compilers are intricate pieces of software, but the CLR source is a valuable tool to understand how the core features in the .NET Framework are implemented.
This is intentionally a small list. I've only touched the surface of the many resources that are available to you from Microsoft, online at other sites, and in books. The more you use these tools, the more knowledgeable you will be. The entire C# and .NET community is moving forward. Because it's moving forward quickly, the list of resources changes constantly. Learn and contribute yourself.