Ten tools!

Source: Internet
Author: User
Tags dotnet reflector

This article discusses:

Nunit used to write unit tests

Used to createCodeNdoc of document materials

Nant used to generate the solution

Codesmith used to generate code

Fxcop used to monitor code

Snippet compiler used to compile a small amount of code

Two different converter tools: ASP. NET version converter and Visual Studio. Net project Converter

Regulator used to generate a regular expression

Used for analysisProgramSet. Net Reflector

This article uses the following technologies:

. Net, C #, Visual Basic. net, Visual Studio. NET

Unless you use the best available tools, you cannot expect to generate first-class applications. In addition to well-known tools such as Visual Studio. NETCommunityObtain many small and unknown tools. In this article, I will introduce you to some of the best free tools available for. NET development. I will guide you through a quick tutorial on how to use each of these tools-some tools can save you a minute in many cases, other tools may completely change the way you write code. Because I wantArticleSo many different tools are introduced in, so I cannot discuss each of them in detail, but you should know enough information about each tool to determine which tools are useful to your project.

Snippet Compiler

Snippet compiler is a Windows-based small application that allows you to write, compile, and run code. This tool is useful if you have a small code segment and you do not want to create a complete Visual Studio. NET project (and all files that accompany the project) for it.

For example, suppose I want to explain how to use Microsoft ?. . NET Framework to start another application. In snippet compiler, I will start by creating a file that can create a small console application. You can create code snippets in the main method of the console application, which is exactly what I want to do here. The following code snippet demonstrates how to create a notepad instance from the. NET Framework:

 
System. Diagnostics. Process proc = new system. Diagnostics. Process (); Proc. startinfo. filename = "notepad.exe"; Proc. Start (); Proc. waitforexit ();

Of course, the code snippet itself cannot be compiled, and this is exactly where snippet compiler is used. Figure 1 shows the sample code in snippet compiler.


Figure1Snippet Compiler

To test the code snippet, you only need to press the play (run) button (green triangle) and it will run in debug mode. This code snippet generates a pop-up console application and displays notepad. When you close notepad, the console application is also closed.

Personally, when I try to create a small example for someone who asks for help, I find that snippet compiler is so valuable-if you do not use this tool, I usually have to create a new project to make sure that each part can be compiled, then send the code snippet to the helper, and delete the project. Snippet compiler makes the process easier and more enjoyable.

Snippet compiler is written by Jeff key and can be downloaded from the http://www.sliver.com/dotnet/SnippetCompiler.

Regulator

Regulator is the last added to my first-class tool list. It is a distinctive tool that makes it easy to generate and test regular expressions. People are interested in regular expressions because they are well supported in the. NET Framework. Regular expressions are used to define the pattern in a string based on characters, frequencies, and character sequence. They are most commonly used as a means to verify the validity of user input or as a method to search for strings in a large string-for example, searching for URLs or email addresses on a web page.

Regulator allows you to enter a regular expression and some input content for running the expression. In this way, before implementing this regular expression in an application, you can understand what effect it will produce and what types of matching items it will return. Figure 2 shows the regulator with a simple regular expression.

Figure 2

This regular expression is included in the document-in this example, it is [0-9] * and should match any number of numbers in a row. The lower-right box contains the input for this regular expression, while the lower-left box shows the matching items found in this regular expression. Writing and testing Regular Expressions in such a separate application is much easier than trying to process them in your application.

One of the best functions of regulator is to search for the online Regular Expression Library in regexlib.com. For example, if you enter the string "phone" in the search box, you will find more than 20 regular expressions that match different phone numbers, including expressions used in the UK and Australia and many other phone numbers. Regulator is written by Roy osherove and can be downloaded from the http://royo.is-a-geek.com/regulator.

Codesmith

Codesmith is a template-based code generation tool that uses syntax similar to ASP. NET to generate any type of code or text. Unlike many other code generation tools, codesmith does not require you to subscribe to specific application designs or architectures. Codesmith can be used to generate anything including a simple set of strong types and a complete application.

When you generate an application, you often need to repeat some specific tasks, such as writing data access code or generating a custom set. Codesmith is especially useful in these cases because you can write templates to automatically complete these tasks, which not only improves your work efficiency, but also automatically complete the most boring tasks. Codesmith comes with many templates, including templates corresponding to all. Net Collection types and templates used to generate stored procedures. But the real power of this tool is the ability to create custom templates. To help you get started, I will quickly introduce how to generate custom templates.

Generate custom templates

The codesmith template is only a text file that can be created in any text editor. Their only requirement is to use the. CST file extension to save them. The sample template I will generate will accept a string and then generate a Class Based on the string. The first step to create a template is to add a template header, which can declare the template language, target language, and brief template description:

 
<% @ Codetemplate Language = "C #" targetlanguage = "C #" Description = "Car template" %>

The next part of the template is the attribute declaration. Here, you can declare the attributes that will be specified during each execution of the template. For this template, the unique attribute I want to use is only a string, so the attribute declaration is as follows:

 
<% @ Property name = "classname" type = "string" Category = "context" Description = "Class Name" %>

This attribute Declaration will make the classname attribute appear in the codesmith attribute window, so that you can specify it when the template is running. The next step is to actually generate the Template subject, which is very similar to encoding with ASP. NET. The subject of the template is as follows:

Custom template

 
Public sealed class <% = classname %> {Private Static volatile <% = classname %> _ instance; private <% = classname %> () {} Private Static readonly object _ syncroot = new object (); public static <% = classname %> value {get {If (_ instance = NULL) {lock (_ syncroot) {If (_ instance = NULL) {_ instance = new <% = classname %> () ;}} return _ instance ;}}}

Singletonclass

Public sealed class singletonclass {Private Static volatile singletonclass _ instance; private singletonclass () {} Private Static readonly object _ syncroot = new object (); public static singletonclass value {get {If (_ instance = NULL) {lock (_ syncroot) {If (_ instance = NULL) {_ instance = new singletonclass () ;}} return _ instance ;}}}

 

As you can see, this template accepts string input and uses this class name to generate a separate class. In the template body, use the same start and end tags as ASP. NET. In this template, I only insert attribute values, but you can also use any type of. Net code within these tags. After the template is complete, you can double-click it or open it from the codesmith application to load it into codesmith. Figure 4 shows the template that has been loaded to codesmith.

Figure 4

You can see that the attribute on the left is exactly the attribute I declared in this template. If I enter "singletonclass" as the class name and click the generate button, the class displayed at the bottom of the template code is generated.

Codesmith is quite easy to use and can produce some incredible results if it can be correctly applied. One of the most common parts of Code-oriented applications is the data access layer. Codesmith includes a special Assembly named schemaexplorer that can be used from tables, stored procedures, or almost any other SQL server? Object generation template.

Codesmith is written by Eric J. Smith and can be downloaded from the http://www.ericjsmith.net/codesmith.

Nunit

Nunit is open for. NET Framework generationSource codeUnit test framework. Nunit allows you to write tests in your preferred language to test specific functions of your application. When you write the code for the first time, unit testing is a good way to test the code function. It also provides a regression test method for the application. The nunit application provides a framework for compiling unit tests and a graphical interface for running these tests and viewing results.

Compile nunit Test

As an example, I will test the hashtable class function in the. NET Framework to determine whether two objects can be added and then retrieved. My first step is to add a reference to the nunit. Framework Assembly, which grants me access to nunit framework attributes and methods. Next, I will create a class and mark it with the testfixture property. This attribute enables nunit to know that the class contains nunit test:

Using system; using system. collections; using nunit. Framework; namespace nunitexample {[testfixture] public class hashtabletest {public hashtabletest (){}}}

Next, I will create a method and mark it with the [test] property so that nunit knows that this method is a test. Then, I will create a hashtable and add two values to it, and then use assert. the areequal method is used to check whether I can retrieve the same value as the value I added to hashtable, as shown in the following code:

 
[Test] public void hashtableaddtest () {hashtable ht = new hashtable (); ht. add ("key1", "value1"); ht. add ("key2", "value2"); assert. areequal ("value1", HT ["key1"], "wrong object returned! "); Assert. areequal (" value2 ", HT [" key2 "]," wrong object returned! ");}

This will confirm that I can first add a value to hashtable and then retrieve the corresponding value-this is a simple test, but it can present nunit functionality. There are many test types and various assert methods that can be used to test each part of the code.

To run this test, I need to generate a project, open the generated assembly in the nunit application, and then click the run button. Figure 5 shows the result. When I saw the big green stripe, I felt excited and dizzy, because it made me know that the test has passed. This simple example shows how convenient and powerful nunit and unit testing are. Because you can write a unit test that can be saved, and you can re-run the unit test whenever you change the code, not only can you detect defects in the Code more easily, and ultimately deliver better applications.


Figure5Nunit

Nunit is an open source project and can be downloaded from a http://www.nunit.org. There is also an excellent nunit Visual Studio. net external program that allows you to run unit tests directly from Visual Studio. You can find it in the http://sourceforge.net/projects/nunitaddin. For more information about nunit and its position in test-driven development, see the article "test-driven C #: improve the design and flexibility of your project with extreme programming techniques "(Msdn MagazinePublished in April 2004 ).

Fxcop

The. NET Framework is very powerful, which means there is a great possibility of creating excellent applications, but there is also the possibility of creating inferior programs. Fxcop is one of the tools that help you create better applications. It uses the following methods: enables you to analyze the Assembly and use different rules to check whether it complies with these rules. Fxcop is accompanied by a fixed number of rules created by Microsoft, but you can also create and include your own rules. For example, if you decide that all classes should have a default constructor without any parameters, you can write a rule to ensure that each class of the Assembly has a constructor. In this way, no matter who writes the code, you will get a certain degree of consistency. For more information about creating custom rules, see the bugslayer topic for John Robbins (Msdn MagazinePublished in June 2004 ).

So let's take a look at the actually running fxcop and see what errors it has found in the nunitexample program I 've been working on. When you open fxcop, you first need to create a fxcop project and then add the assembly to be tested. After the Assembly is added to the project, you can press analyze and fxcop will analyze the assembly. Figure 6 shows the errors and warnings found in the dataset.

Figure 6

Fxcop found several problems in my program set. You can double-click an error to view details, including the rule description and where to find more information. (One interesting thing you can do is to run fxcop on the Framework Assembly and view what happened .)

Fxcop can help you create better and more consistent code, but it cannot compensate for poor application design or very simple and poor programming. Fxcop cannot replace peer-to-peer code check, but because it can capture a large number of errors before code check, you can spend more time solving serious problems without worrying about naming conventions. Fxcop is developed by Microsoft and can be downloaded from the http://www.gotdotnet.com/team/fxcop.

Lutz roeder. Net Reflector

The next essential tool is. Net reflector. It is a class browser and anti-compiler that can analyze an assembly and show you all its secrets .. The Net Framework introduces reflection concepts that can be used to analyze any. Net-based code (whether it is a single class or a complete assembly) all over the world. Reflection can also be used to retrieve information about various types, methods, and attributes contained in a specific set of programs. Use. net reflector, you can browse the Assembly classes and methods, you can analyze the Microsoft intermediate language (msil) generated by these classes and methods ), in addition, you can decompile these classes and methods and view C # Or Visual Basic.. net.

To demonstrate how. Net reflector works, I will load and analyze the nunitexample Assembly shown earlier. Figure 7 shows the Assembly loaded in. Net reflector.


Figure7Nunitexample assembly

In. Net reflector, there are various tools available to further analyze the assembly. To view the msil that constitutes a method, click the method and select discycler from the menu.

In addition to msil, you can select decompiler under the Tools menu to view the C # format of this method. By changing your selection under the ages menu, you can also view the form in which this method is decompiled to Visual Basic. Net or Delphi. The code generated by. Net reflector is as follows:

Public void hashtableaddtest () {hashtable hashtable1; hashtable1 = new hashtable (); hashtable1.add ("key1", "value1"); hashtable1.add ("key2", "value2"); assert. areequal ("value1", hashtable1 ["key1"], "wrong object returned! "); Assert. areequal (" value2 ", hashtable1 [" key2 "]," wrong object returned! ");}

The previous code looks like the code I actually wrote for this method. The actual code in this Assembly is as follows:

 
Public void hashtableaddtest () {hashtable ht = new hashtable (); ht. add ("key1", "value1"); ht. add ("key2", "value2"); assert. areequal ("value1", HT ["key1"], "wrong object returned! "); Assert. areequal (" value2 ", HT [" key2 "]," wrong object returned! ");}

Although there are some minor differences in the above Code, they are all functional in the same way.

Although this example is a good way to show the comparison between the actual code and the decompiled code, it does not represent to me. net reflector has the best use-analysis.. NET Framework Assembly and method .. . NET Framework provides many different methods to execute similar operations. For example, if you want to read a group of data from XML, there are multiple methods to do this using xmldocument, xpathnavigator, or xmlreader. By using. Net reflector, you can view what Microsoft uses when writing the dataset's readxml method, or what work they did when reading data from the configuration file .. Net reflector is also an excellent way to understand the following best implementation policies: creating objects such as httphandlers or configuring handlers, because you can understand how Microsoft workgroup actually generates these objects in the framework.

. Net reflector is written by Lutz roeder and can be downloaded from http://www.aisto.com/roeder/dotnet.

Ndoc

Writing code documents is almost always a daunting task. What I'm talking about is not an early design document, or even a more detailed design document; what I'm talking about is various methods and attributes in the record class. The ndoc tool can use reflection to analyze the Assembly and automatically generate documentation for the code using the XML generated from the C # xml annotation. XML annotation is only applicable to C #, but there is a Visual Studio. net power toy named vbcommenter, which can do similar work for Visual Basic. net. In addition, the next version of Visual Studio supports XML annotations for more languages.

When ndoc is used, you are still writing the technical documentation of the code, but you have completed the compilation of the documentation (in XML annotations) while writing the code, which is more tolerable. When using ndoc, the first step is to enable the XML annotation generation function for your assembly. Right-click the project, select Properties, configuration properties, and build, and enter the path to save the XML file in the XML documentation file option. When the project is generated, an XML file containing all XML annotations is created. In the nunit example, a document is written in XML:

 
/// <Summary> // This test adds a number of values to the hashtable collection // and then retrieves those values and checks if they match. /// </Summary> [test] public void hashtableaddtest () {// method body here}

XML documents about this method will be extracted and saved in the XML file, as shown below:

 
<Member name = "M: nunitexample. hashtabletest. hashtableaddtest "> <summary> This test adds a number of values to the hashtable collection and then retrieves those values and checks if they match. </Summary> </member>

Ndoc uses reflection to evaluate your Assembly, read the XML in the document, and match them. Ndoc uses this data to create any number of different document formats, including the HTML Help File (CHM ). After the XML file is generated, the next step is to load the Assembly and XML files into ndoc so that they can be processed. You can easily complete this task by opening ndoc and clicking the Add button.

After you load the Assembly and XML files to ndoc and use the available property range to customize the output, click Generate to start the document generation process. By using the default attributes, ndoc can generate some very attractive and practical. html and. CHM files to automatically complete the originally tedious tasks in a fast and effective manner.

Ndoc is an open source project and can be downloaded from the http://ndoc.sourceforge.net.

Nant

Nant is A. Net-based generation tool. Unlike the current version of Visual Studio. NET, it makes the creation and generation process of your project very easy. When you have a large number of developers engaged in a single project, you cannot rely on generating from the seat of a single user. You do not want to manually generate the project on a regular basis. You are more willing to create an automatic generation process that runs every night. Nant allows you to generate solutions, copy files, run nunit tests, send emails, and so on. Unfortunately, Nant lacks a beautiful graphical interface, but it does have console applications and XML files that can specify which tasks should be completed during the generation process. Note that msbuild (a new generation platform of Visual Studio 2005) Prepares for each robust generation scheme and is driven in a similar way by XML-based project files.

Actually running Nant

In this example, I will create an Nant version file for the previously created nunitexample solution. First, I need to create an XML file with the. Build extension, put it in the root directory of my project, and then add an XML declaration to the top of the file. The first tag I need to add to this file is the project Tag:

<? XML version = "1.0"?> <Project name = "nunit example" default = "build" basedir = "."> <description> the nunit example project </description> </Project>

The project tag is also used to set the project name, default target, and base directory. The description mark is a brief description of the project.

Next, I will add the property tag, which can be used to store the settings to a single location (which can then be accessed from any location in the file ). In this example, I will create an attribute named Debug. I can then set it to true or false to reflect whether I want to compile the project under the debug configuration. (Finally, this specific property does not really affect how to generate the project; it is just a variable you set and will be read when you are actually sure how to generate the project .)

Next, I need to create a target tag. A project can contain multiple targets that can be specified during Nant running. If no target is specified, the default target is used (the target set in the project element ). In this example, the default target is build. Let's take a look at the target element, which will contain most of the generated information:

 
<Target name = "build" Description = "compiles the source code"> </Target>

In the target element, I will set the target name to build and create instructions on what the target will do. I will also create a CSC element to specify the data that should be passed to the CSC C # compiler. Let's take a look at the CSC element:

<CSC target = "library" output = ". /bin/debug/nunitexample. DLL "DEBUG =" $ {debug} "> <references> <shortdes name =" C:/program files/nunit V2.1/bin/nunit. framework. DLL "/> </References> <sources> <shortdes name =" hashtabletest. CS "/> </sources> </CSC>

First, you must set the target of the CSC element. In this example, I will create a. dll file, so I set target to library. Next, I must set the output of the CSC element, which is the location of the. dll file to be created. Finally, I need to set the debug attribute to determine whether to compile the project in debugging. Because I created an attribute to store this value, I can use the following string to access the value of this attribute: $ {debug }. The CSC element also contains child elements. I need to create two elements: the references element will tell Nant which assembly should be referenced for this project, and the sources element will tell Nant what files should be included during the generation process. In this example, I reference the nunit. Framework. dll assembly and include the hashtabletest. CS file. The following code shows the complete file generation. (You usually need to create a clean target to delete the generated file, but I have omitted it for the sake of simplicity .)

 
    
    
    
      the nunit example project 
     
     
     
      
       
        
       
       
        
       
      
     
    

To generate this file, I need to go to the root directory of my project (where the generated file is located), and then execute nant.exe from this location. If the file is successfully generated, you can find the. dll and. PDB files in the bin directory of the application. Although Nant is certainly not as simple as clicking build in Visual Studio, it is still a very powerful tool that can be used to develop a build process that runs on an automatic schedule. Nant also includes some useful functions, such as the ability to run unit tests or copy additional files (these functions are not supported by the current Visual Studio generation process ).

Nant is an open source project and can be downloaded from a http://nant.sourceforge.net.

Conversion Tool

I have already integrated two independent tools under the title "Conversion Tool. Both tools are very simple, but may be extremely useful. The first tool is the ASP. NET version converter, which can be used to convert the version of ASP. NET (the virtual directory runs under it. The second tool is Visual Studio converter, which can be used to convert a project file from Visual Studio. NET 2002 to Visual Studio. NET 2003.

When IIS processes a request, it will view the extension of the file being requested, and then delegate the request to the ISAPI extension or process the request based on the extension ing of the web site or virtual directory. This is exactly how ASP. NET works. It registers extension mappings for all ASP. NET extensions and directs these extension mappings to aspnet_isapi.dll. This method works perfectly unless you have installed ASP. NET 1.1-it will update the extension ing to the new version of aspnet_isapi.dll. This can cause errors when an application generated on ASP. NET 1.0 tries to run on version 1.1. To solve this problem, you can reconvert all extension mappings to aspnet_isapi.dll of version 1.0. However, it is boring to manually complete this task because of 18 extension mappings. This is exactly when ASP. NET version converters can play a role. Using this small utility, you can convert the version of the. NET Framework used by any single ASP. NET application.


Figure9ASP. NET version Converter

Figure 9 shows the actually running ASP. NET version converter. It is easy to use. You only need to select the corresponding application, and then select the. NET Framework version you want the application to use. The tool will then use the aspnet_regiis.exe command line tool to convert the application to the framework of the selected version. With the release of ASP. NET and. Net frameworks in future versions, this tool will become more useful.

The ASP. Net version converter is written by Denis Bauer and can be downloaded from the http://www.denisbauer.com/NETTools/ASPNETVersionSwitcher.aspx.

The Visual Studio. NET project converter (see figure 10) is very similar to the ASP. NET version converter. The difference is that it is used to convert the version of the Visual Studio project file. However.. NET Framework Version 1.0 and 1.1 are only slightly different, but once the project file is removed from Visual Studio.. NET 2002 to Visual Studio. NET 2003, and it cannot be converted back. Although this may not be a problem in most cases (because in. net Framework Version 1.0 and Version 1.1 have almost no disruptive changes), but at some point you may need to switch the project back. This converter can save any solution or project file from Visual Studio 7.1 (Visual Studio.. NET 2003) to Visual Studio 7.0 (Visual Studio. NET 2002), and perform reverse conversion if necessary.


Figure10Visual Studio. NET project converter

The Visual Studio. Net project converter is written by dacris software. This tool can be downloaded from the http://www.codeproject.com/macro/vsconvert.asp.

 

Summary

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.