十一月 9, 2007
 
public   bool   IsCorrenctIP(string ip)

  {  
   string pattrn=@”(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])”;
   if(System.Text.RegularExpressions.Regex.IsMatch(ip,pattrn)) 

   {
    return   true;  
   }
   else
   {
    return  
false;  
   
   }
  }

  public   bool   IsValidIP(string   ip)  
  {  
   if  
(System.Text.RegularExpressions.Regex.IsMatch(ip,”[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}”))  

   {  
    string[]   ips   =   ip.Split(’.');  
    if  
(ips.Length   ==   4   ||   ips.Length   ==   6)  
    {  

     if(System.Int32.Parse(ips[0])   <   256   &&  
System.Int32.Parse(ips[1])   <   256   &   System.Int32.Parse(ips[2])  
<   256   &   System.Int32.Parse(ips[3])   <   256)  

      return   true;  
     else  
      return   false;  

    }  
    else  
     return   false; 
   
   }  

   else  
    return   false;  
  }

Powered by ScribeFire.

十月 25, 2007

2ndgatechina的笔试project:写一个.NET API,能使其他devs能够调用他来将任意时区的时间转换为当前时间或者将当前时间转换为任意时区的时间。

咋一看,好像比较简单,真做的时候才发现我怎么去获取我系统的当前时区呢?上http://www.pinvoke.net上查,发现需要使用kernel32.dll中的GetTimeZoneInformation的系统API。

现成的例子参看这里,我就不详细说了:http://www.pinvoke.net/default.aspx/kernel32.GetTimeZoneInformation

在这个基础上我们来扩展一下:如何去取得windows所给我们设定的全部时区呢?

其实windows所设定的时区全部保存在 \KEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones的目录里,那么很自然我们有代码:

1 RegistryKey key = Registry.LocalMachine.OpenSubKey(
2 @”SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones” ) )
3 {
4 string[] zoneNames = key.GetSubKeyNames();
5
6 foreach ( string zoneName in zoneNames )
7 {
8 using ( RegistryKey subKey = key.OpenSubKey( zoneName ) )
等等,这里的TZI是二进制的,这种又代表什么呢?仍然是上面的pinvoke的链接,可以将这些二进制数据转换为里面所定义的结构体。那么可以通过如下方法枚举出Time Zones的所有子键信息:

1public static TimeZoneInformation[] EnumZones()
2 {
3 if ( s_zones == null )
4 {
5 lock( s_lockZones )
6 {
7 if ( s_zones == null )
8 {
9 ArrayList zones = new ArrayList();
10
11 using ( RegistryKey key =
12 Registry.LocalMachine.OpenSubKey(
13 @”SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones” ) )
14 {
15 string[] zoneNames = key.GetSubKeyNames();
16
17 foreach ( string zoneName in zoneNames )
18 {
19 using ( RegistryKey subKey = key.OpenSubKey( zoneName ) )
20 {
21 TimeZoneInformation tzi = new TimeZoneInformation();
22 tzi.m_name = zoneName;
23 tzi.m_displayName = (string) subKey.GetValue( “Display” );
24 tzi.m_standardName = (string) subKey.GetValue( “Std” );
25 tzi.m_daylightName = (string) subKey.GetValue( “Dlt” );
26 tzi.m_index = (int)( subKey.GetValue( “Index” ) );
27
28 tzi.InitTzi( (byte[]) subKey.GetValue( “Tzi” ) );
29
30 zones.Add( tzi );
31 }
32 }
33 }
34
35 s_zones = new TimeZoneInformation[ zones.Count ];
36
37 zones.CopyTo( s_zones );
38 }
39 }
40 }
41
42 return s_zones;
43 }

这样我们便获取到了windows所给我们定义好的时区信息,其中TimeZoneInformation.bias代表我做API中所需要的时间偏移量(单位:分钟),至此,我们就可以做出windows所提供的时区之间的任意转换了。

给一个很好的与此相关的代码资料:http://www.codeproject.com/dotnet/WorldClock.asp

参考资料:
http://www.pinvoke.net
http://www.codeproject.com/dotnet/WorldClock.asp

Powered by ScribeFire.

十月 23, 2007

C#格式化数值结果表(格式化字符串)

字符

说明

示例

输出

C 货币 string.Format(”{0:C3}”, 2) $2.000
D 十进制 string.Format(”{0:D3}”, 2) 002
E 科学计数法 1.20E+001 1.20E+001
G 常规 string.Format(”{0:G}”, 2) 2
N 用分号隔开的数字 string.Format(”{0:N}”, 250000) 250,000.00
X 十六进制 string.Format(”{0:X000}”, 12) C


string.Format(”{0:000.000}”, 12.2) 012.200

Strings

There really isn’t any formatting
within a strong, beyond it’s alignment. Alignment works for any
argument being printed in a String.Format call.

 

Sample Generates
String.Format(”->{1,10}<-”, “Hello”); -> Hello<-
String.Format(”->{1,-10}<-”, “Hello”); ->Hello <-

Numbers

Basic number formatting specifiers:

 

Specifier Type Format

Output
(Passed
Double 1.42)

Output
(Passed
Int -12400)

c Currency {0:c} $1.42 -$12,400
d Decimal (Whole number) {0:d} System.
FormatException
-12400
e Scientific {0:e} 1.420000e+000 -1.240000e+004
f Fixed point {0:f} 1.42 -12400.00
g General {0:g} 1.42 -12400
n Number with commas for thousands {0:n} 1.42 -12,400
r Round trippable {0:r} 1.42 System.
FormatException
x Hexadecimal {0:x4} System.
FormatException
cf90

Custom number formatting:

 

Specifier Type Example Output (Passed Double 1500.42) Note
0 Zero placeholder {0:00.0000} 1500.4200 Pads with zeroes.
# Digit placeholder {0:(#).##} (1500).42
. Decimal point {0:0.0} 1500.4
, Thousand separator {0:0,0} 1,500 Must be between two zeroes.
,. Number scaling {0:0,.} 2 Comma adjacent to Period scales by 1000.
% Percent {0:0%} 150042% Multiplies by 100, adds % sign.
e Exponent placeholder {0:00e+0} 15e+2 Many exponent formats available.
; Group separator see below  

The group separator is especially useful for formatting currency
values which require that negative values be enclosed in parentheses.
This currency formatting example at the bottom of this document makes
it obvious:

Dates

Note that date formatting is especially dependant on the system’s
regional settings; the example strings here are from my local locale.

 

Specifier Type Example (Passed System.DateTime.Now)
d Short date 10/12/2002
D Long date December 10, 2002
t Short time 10:11 PM
T Long time 10:11:29 PM
f Full date & time December 10, 2002 10:11 PM
F Full date & time (long) December 10, 2002 10:11:29 PM
g Default date & time 10/12/2002 10:11 PM
G Default date & time (long) 10/12/2002 10:11:29 PM
M Month day pattern December 10
r RFC1123 date string Tue, 10 Dec 2002 22:11:29 GMT
s Sortable date string 2002-12-10T22:11:29
u Universal sortable, local time 2002-12-10 22:13:50Z
U Universal sortable, GMT December 11, 2002 3:13:50 AM
Y Year month pattern December, 2002

The ‘U’ specifier seems broken; that string certainly isn’t sortable.

Custom date formatting:

 

Specifier Type Example Example Output
dd Day {0:dd} 10
ddd Day name {0:ddd} Tue
dddd Full day name {0:dddd} Tuesday
f, ff, … Second fractions {0:fff} 932
gg, … Era {0:gg} A.D.
hh 2 digit hour {0:hh} 10
HH 2 digit hour, 24hr format {0:HH} 22
mm Minute 00-59 {0:mm} 38
MM Month 01-12 {0:MM} 12
MMM Month abbreviation {0:MMM} Dec
MMMM Full month name {0:MMMM} December
ss Seconds 00-59 {0:ss} 46
tt AM or PM {0:tt} PM
yy Year, 2 digits {0:yy} 02
yyyy Year {0:yyyy} 2002
zz Timezone offset, 2 digits {0:zz} -05
zzz Full timezone offset {0:zzz} -05:00
: Separator {0:hh:mm:ss} 10:43:20
/ Separator {0:dd/MM/yyyy} 10/12/2002

Enumerations

 

Specifier Type
g Default (Flag names if available, otherwise decimal)
f Flags always
d Integer always
x Eight digit hex.

Some Useful Examples

String.Format(”{0:$#,##0.00;($#,##0.00);Zero}”, value);

This will output “$1,240.00″ if passed 1243.50. It will output the
same format but in parentheses if the number is negative, and will
output the string “Zero” if the number is zero.

String.Format(”{0:(###) ###-####}”, 18005551212);

This will output “(800) 555-1212″.

 

变量.ToString()

字符型转换 转为字符串
12345.ToString(”n”); //生成 12,345.00
12345.ToString(”C”); //生成 ¥12,345.00
12345.ToString(”e”); //生成 1.234500e+004
12345.ToString(”f4″); //生成 12345.0000
12345.ToString(”x”); //生成 3039 (16进制)
12345.ToString(”p”); //生成 1,234,500.00%

Powered by ScribeFire.

十月 20, 2007
09.25.07 | 38 Comments

After some extensive researching (Googling), I’ve compiled this list of 15+
FREE Add-ins for Visual Studio 2005. Some of these add-ins will help improve the
quality of your code, while others will help you code faster, however, none are
guaranteed to help you get with the ladies.

If there’s an add-in that I’ve missed and you think that it should be added
to this list, just post a comment with the name, URL, and a brief description.
But before doing so, make sure that it meets the following strict requirements:
a) It must run within Visual Studio 2005. b) It must be free.

So without further ado, here’s my list of 15+ Free Visual Studio 2005 Add-Ins

 Update -
September 25, 2007:

The images for the Add-ins have been
temporarily removed.

AnkhSVN

This is an Add-in for the the Subversion version control system, which will
allow you to perform most of the common version control operations from within
Visual Studio.

 

C# SortCode
Macro

This nifty little macro will alphabetically sort all members of a type (class
or struct), grouped by their member type. I think this is a great little tool,
however, I’m not a big fan of its sort order, but I’m sure that this can easily
be modified in the macro. This will run in VS2005, even though the title says
that it is for Visual Studio.NET 2003. When you load the macro, you will receive
a warning saying that it was created in VS2002. Just click on ‘Yes’.

 

CodeKeep
Add-in

This is an add-in for CodeKeep.net, that will allow you to manage your
snippets of code, and also search for other snippets all from within Visual
Studio. Before installing, you will need to create an account on the
CodeKeep.net web site. After you have installed the ad-in, make sure to read the
Readme.txt file. There are three files that need to be copied over to the
Add-ins folder.

 

Code Style Enforcer

Need a proof reader for your code? If yes, then this is the add-in for you.
What it does, is it checks your code in real time against IDesign’s C# coding
standards and best practices. When it finds something that it doesn’t agree
with, it will underline it with what looks like a red pen. It is really picky,
however, the rules can be modified. For example, you may want to change some
rules to allow the variable names “e” for EventArgs and “ex” for Exceptions.
Before installing Code Style Enforcer, you will first need to install DXCore from Developer Express.

 

CopySourceAsHtml

I’ll give you three guesses as to what this add-in does. Give up? Well,
CopySourceAsHtml does exactly what it says. This add-in it will allow you to
copy your source code along with the syntax highlighting and line numbers, to
HTML. As their site says, “If Visual Studio can highlight it, CSAH can copy it
…”.

I did have one small issue with this plug-in. It seems that when I pasted my
code, it forgot to close of a , and another was out of
place. However, I’m sure that I’ll be using this tool a few times for some
future posts.

 

EncapsulateAllNonPrivateFields macro

Here’s another little macro from Reflection IT. This macro generates private
fields, and public properties. To use it, all you have to do is declare a few
public variables, highlight them, then run the macro, and VOILA! You now have a
property and a private field for each variable that you highlighted.

If you plan on using this macro, and have their C# SortCode macro loaded, you
must unload it first.

 

GhostDoc
2.1.1

According to its site, GhostDoc “automatically generates XML
documentation comments for C#. Either by using existing documentation inherited
from base classes or implemented interfaces, or by deducing comments from name
and type of e.g. methods, properties or parameters.
” I couldn’t have said
it any better.

 

Koders

This add-in, like CodeKeep, allows you to search for code from within Visual
Studio. From what I can tell, that is where the similarities end. I was unable
to find any way to save my snippets of code, without upgrading to the Pro
version, which is $9.99/month or $99/year.

 

Microsoft
Popfly Explorer Alpha

This add-in allows you create, modify, and share Visual Studio solutions from
your Popfly Space. You must be a registered user to access Popfly.

 

Modeling Power
Toys

If you use the Class Designer in Visual Studio, then I suggest that you
download and install this add-in.

From their web site, here is a partial list of enhancements that it provides:

Design Tools Enhancements
- Diagram Search
- Formatting Commands

- Floating Property Grid
- Pan/Zoom Window
- Design Surface
Scrolling Improvements
- Create Comments with Double-Click
- Design
Surface Grid

Class Designer Enhancements
- Export Diagrams for Web
- Display Xml
Comment Command
- Documentation Tool Window
- Filtering Appearance
-
Filtering Lines
- Filtering Members
- MSDN Help on System Types
-
Fast Navigation
- Interface Lollipop Labels Commands
- Inheritance
Visualization Commands
- Show Type Command
- Association and Inheritance
Line Dongles
- Create Tests Command
- Type Creation Commands
- Add
Member Commands
- Synchronization with Code Definition Window
- View
Class Diagram Command Improvements
- Creating Custom Add-ins

 

NHibernate

This is a .Net port of the popular Hibernate Core for Java. Since I can’t
clearly explain what it is, I’ll give you their explanation from their own
site.

 

NHibernate handles persisting your .NET objects to and from an underlying
relational database. Rather than you having to write SQL to get your objects in
and out of the database, NHibernate takes care of this for you. Your code only
needs to be concerned with your objects, NHibernate generates the SQL and makes
sure that things end up in the correct tables and columns.

 

Pinvoke.Net Addin

This add-in, owned by Red Gate, is similar to CodeKeep, but instead of
searching and adding snippets of code, Pinvoke.Net allows developers to find,
edit and add PInvoke signatures, user-defined types, and any other information
related to calling Win32 and other unmanaged APIs from managed code (written in
languages such as C# or VB.NET).

 

Refactor!™ for ASP.NET

Looking for a real time saving add-in? Well look no further. Refactor! comes
with 29 time saving refactorings, with 10 of those just for ASP.Net.

 

Regular Expression Visualizers

Visual Studio already has a Text Visualizer, an XML Visualizer and an HTML
Visualizer built in. But now you can also visualize Regular expressions. It
allows you to visualize System.String, RegularExpressions.Regex,
RegularExpresions.Match and RegularExpressions.MatchCollection.

 

Regions
Add-In

This add-in makes creating and managing regions a lot easier. To download the
source code and/or installer for Visual Studio 2005, go to http://janyou.bokee.com/index.html.

If you’ve never used regions before, check out this short posting: http://trackerrealm.com/blogs/2007/04/make-use-of-regions-when-developing-c.html

 

SlickEdit®
Gadgets

The gadgets are a collection of several utilities, some a bit more useless
than others.

- Editor Gadgets: This, in my opinion, is the most useless
set of utilities that SlickEdit Gadgets offers.

Included is a “ruler”, which highlights the line that your cursor is on. Not
sure why you would need a ruler to measure your code. Each tick mark on the
ruler represents your editor’s tab space.

Also, there’s a vertical dashed line that shows you indentation level of the
line that your cursor is on.

Thirdly, they have what they call “Auto-copy selection”, which is similar to
mIrc’s copying feature. If this is enabled, every time you highlight something,
it automatically gets copied. To do paste, you would click on your mouse’s
middle button (scroll wheel).

This has to be the most useless feature of the useless set of features, the
“Editor Graphic”. By enabling this feature, you can select a graphic from your
personal collection, and it will be shown in your editor, like below. In the
settings, there’s an option to enable animated graphics, however, I tried two
different animated GIFs, and neither worked for me.

- The Command Spy: This little utility won’t improve your
coding, but it will help you become more familiar with Visual Studio’s
shortcuts. When ever you perform an action, such as Save, Copy, File Open,
Build, or pretty much anything else that you can click on through the toolbars
or menu, Command Spy will capture that event and display it, along with it’s
shortcut if there is one.

- File Explorer: Windows Explorer for Visual Studio ….

- The Data Object Analyzer: Ever wanted to add clipboard or
drag-and-drop integration into your application? If yes, then this utility might
be for you.

Data Object Analyzer inspects the contents of any clipboard operation, or
drag-and-drop operation, from any other application, and then can be used to
automatically generate functions to handle any clipboard or drag-and-drop
data.

- The SLOC Report: Now, if you’re going to brag that your
code is bigger than someone else’s (why else would they include a
ruler?
), you better use the SLOC Report. What it does is it breaks your
code down into three categories, Whitespace, Comments, and Code, and displays it
in a pie graph..

 

Smart Paster 1.1

Smart Paster is a simple little tool, that will give you the option to paste
copied text as either a Comment, String, StringBuilder, or a region.

A perfect example where this could save a lot of time would be if you copied
a chuck of JavaScript code that you need to paste into your own VB.Net or C#
code. Instead of doing the usual paste, then manually wrapping each line in a
StringBuilder.AppendLine(), or in quotes for a regular String, you could just
use the “Paste as StringBuilder”, or “Paste as String” option, and let it do the
dirty work for you.

 

SonicFileFinder

This add-in allows you to quickly search for files within a solution, by
either using the full name (MyClass.cs), part of it (*Class), or even initials
(MC). The search is done while you type, just like Mac OS X’s
Spotlight
, Vista’s search.

 

Update -
September 25, 2007:

Regionerate

This add-in (pronounced ri-jeh-neh-rate), is an open-source tool that will
quickly apply layout rules to your code. With two clicks of a mouse, it will
sort your code, and break it up into regions, such as Fields, Constructors, and
Properties.

One of the things that I liked about this tool, is that it allows you to
create your own layouts, something I wish the Sort Code macro did (but I’m
sure you can modify the macro yourself to suit your needs
).

One of the things I didn’t like about it however, is that you can’t place
code into customized regions (like the Regions Add-In does). So say I
wanted to create a region called Overloads, or Ajax Methods. If I created these
two regions, it doesn’t give me the option to place code into them, and, if I
run the Regionerate, it will remove the regions, and place the code into its own
regions, such as Methods.

 

Resource
Refactoring Tool

Resource Refactoring Tool provides developers an easy way to extract hard
coded strings from the code to resource files.

I installed it, and tried it out on an .aspx page, but didn’t have any
luck.

 

HyperAddin

This cool util allows you to place hyperlinks into your code, to other parts
of your code, or files

 

Update -
September 28, 2007:

DPack

DPack is a free collection of Visual Studio .NET 2003 and 2005 tools, which
were designed to greatly increase developer’s productivity, automate repetitive
processes and expand upon some of the Visual Studio features.

If I had to choose just one Add-in from this list, so far it would be between
Refactor! and DPack.

DPack’s features include: Code
Browser
, File
Browser
, Solution
Browser
, Framework
Browser
, Numbered
Bookmarks
, Surround
With
, Code Navigation, Solution Statistics, and a
Solution
Backup

 

Well, that’s all for now folks. This list, with all of the images is getting
a bit too long for one page, so I’ve decided to stop adding to this list.

However, with that being said, I have already started compiling Part 2, which
will feature a new list of some of the best FREE Visual Studio 2005
Add-ins
. If this list had some things in it that you never knew about,
but wish that you did, I can almost guarantee that the next one will have even
more.

Thanks to everyone who made some suggestions, they haven’t been ignored.

P.S. If you know of any addins that aren’t on the list, feel free to suggest
them.

Powered by ScribeFire.

出处:.NET Tools:Ten Must-Have Tools Every Developer
Should Download Now

本文讨论的工具如下:
  NUnit:编写单元测试的工具

  NDoc:创建代码文档的工具
  NAnt:生成解决方案的工具
  CodeSmith:代码生成工具

  FxCop:用于监视代码的——代码警察
  Snippet Compiler:小型代码段编译工具

  两个不同的转换器工具,ASP.NET 版本转换器(Version Switcher)和 Visual Studio .NET
项目转换器(Project Converter)
  Regulator:生成正则表达式工具
  .NET Reflector:程序集分析检查工具

  本文使用了以下技术:.NET,C#,Visual Basic .NET,Visual Studio .NET(每个工具的介绍最后都会提供参考下载地址

  除非你使用可获得的最好的工具,否则别指望能生成一流的应用程序。除了
Visual Studio .NET 这种大型工具以外,你从 .NET
社区还可以获得许多小型的、不太为人所知的工具。我在本文中将向你介绍几个目前可以获得的、面向 .NET
开发的最好的免费工具。我将引领你完成一个有关如何使用每种工具的快速教程——
有些工具在许多场合可以节约你的时间,而另一些工具则可能完全改变你编写代码的方式。因为我要在本篇文章中介绍如此之多的不同工具,所以我无法详尽讨论其中每种工具,但你应该充分了解每种工具的信息,以便决定哪些工具对你的项目有用。

Snippet Compiler
  Snippet Compiler 是一个基于 Windows
的小型应用程序,你可以通过它来编写、编译和运行代码。如果你具有较小的代码段,并且你不想创建完整的 Visual Studio .NET
项目(以及该项目附带的所有文件),则该工具会很有用。
  例如,假设我希望向你示范如何从 Microsoft .NET 框架中启动另一个应用程序。在
Snippet Compiler 中,我将通过新建一个能够创建小型控制台应用程序的文件开始。可以在该控制台应用程序的 Main
方法内部创建代码片段,而这正是我要在这里做的事情。下面的代码片段演示了如何从 .NET 框架中创建记事本实例: screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor=’hand’; this.alt=’点击在新窗口浏览图片\nCTRL+Mouse 滚轮可放大/缩小’;}” onclick=”if(!this.resized) {return true;} else {window.open(this.src);}” src=”http://www.mndsoft.com/blog/images/code.gif” onload=”if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt=’点击在新窗口浏览图片\nCTRL+Mouse 滚轮可放大/缩小’;}” border=”0″> 程序代码
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName= “notepad.exe”;
proc.Start();
proc.WaitForExit();

当然该代码片段本身无法编译,而这正是 Snippet Compiler
的用武之地。Figure 1 显示了 Snippet Compiler 中的这一代码示例。
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor=’hand’; this.alt=’点击在新窗口浏览图片\nCTRL+Mouse 滚轮可放大/缩小’;}” onclick=”if(!this.resized) {return true;} else {window.open(this.src);}” src=”http://www.pconline.com.cn/pcedu/empolder/net/vc/0509/pic/05-09-13-net-1.gif” onload=”if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt=’点击在新窗口浏览图片\nCTRL+Mouse 滚轮可放大/缩小’;}” border=”0″>

下载地址:http://www.sliver.com/dotnet/SnippetCompiler.

Regulator  

Regulator
是最近添加到我的头等工具清单中的。它是一种很有特色的工具,能够使生成和测试正则表达式变得很容易。由于正则表达式在 .NET
框架中受到极好的支持,因而人们对正则表达式重新产生了兴趣。正则表达式用来基于字符、频率和字符顺序定义字符串中的模式。它们最常见的用途是作为验证用户输入有效性的手段或者作为在较大字符串中查找字符串的方法——例如,在
Web 页上查找 URL 或电子邮件地址。
  Regulator
允许你输入一个正则表达式以及一些针对其运行该表达式的输入。这样,在应用程序中实现该正则表达式之前,你便可以了解它将产生什么效果以及它将返回哪些种类的匹配项。Figure 2 显示了带有简单正则表达式的
Regulator。
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor=’hand’; this.alt=’点击在新窗口浏览图片\nCTRL+Mouse 滚轮可放大/缩小’;}” onclick=”if(!this.resized) {return true;} else {window.open(this.src);}” src=”http://www.pconline.com.cn/pcedu/empolder/net/vc/0509/pic/05-09-13-net-2.gif” onload=”if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt=’点击在新窗口浏览图片\nCTRL+Mouse 滚轮可放大/缩小’;}” border=”0″>
  文档中包含该正则表达式——在该示例中,它是
[0-9]*,应该匹配一行中任意数量的数字。右下侧的框中含有针对该正则表达式的输入,而左下侧的框显示了该正则表达式在输入内容中找到的匹配项。象这样在单独应用程序中编写和测试正则表达式,要比尝试在你自己应用程序中处理它们容易得多。
  Regulator
中的最佳功能之一——是能够在 regexlib.com 搜索在线正则表达式库。例如,如果你在搜索框中输入字符串“phone”,你将找到 20
种多种不同的能够匹配各种电话号码的正则表达式,包括用于英国、澳大利亚的表达式以及其它许多电话号码。Regulator 由 Roy Osherove
编写.
下载地址: http://regex.osherove.com

CodeSmith  
CodeSmith
是一种基于模板的代码生成工具,它使用类似于 ASP.NET 的语法来生成任意类型的代码或文本。与其它许多代码生成工具不同,CodeSmith
不要求你订阅特定的应用程序设计或体系结构。借助 CodeSmith 可以生成包括简单的强类型集合和完整应用程序在内的任何东西。

  当生成应用程序时,无论是编写数据访问代码还是生成自定义集合,你会发现经常需要重复完成某些特定的任务。这时 CodeSmith
就显得特别有用,因为你可以编写模板自动完成这些任务,从而不仅提高你的工作效率,而且能够自动完成那些最为乏味的任务。CodeSmith
附带了许多模板,包括对应于所有 .NET
集合类型的模板以及用于生成存储过程的模板,但该工具的真正威力在于能够创建自定义模板。为了使你能够入门,我将快速介绍一下如何生成自定义模板。
生成自定义模板
  CodeSmith
模板只是一些可以在任意文本编辑器中创建的文本文件。它们的唯一要求是用 .cst
文件扩展名来保存它们。我将要生成的示例模板将接受一个字符串,然后基于该字符串生成一个类。创建模板的第一步是添加模板头,它可声明模板的语言、目标语言以及简要模板说明:
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor=’hand’; this.alt=’点击在新窗口浏览图片\nCTRL+Mouse 滚轮可放大/缩小’;}” onclick=”if(!this.resized) {return true;} else {window.open(this.src);}” src=”http://www.mndsoft.com/blog/images/code.gif” onload=”if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt=’点击在新窗口浏览图片\nCTRL+Mouse 滚轮可放大/缩小’;}” border=”0″> 程序代码
<%@ CodeTemplate Language=“C#”
TargetLanguage=“C#”
Description=“Car Template” %>

模板的下一部分是属性声明,在这里可声明将在模板每次运行时指定的属性。就该模板而言,我要使用的唯一属性只是一个字符串,因此属性声明如下所示:
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor=’hand’; this.alt=’点击在新窗口浏览图片\nCTRL+Mouse 滚轮可放大/缩小’;}” onclick=”if(!this.resized) {return true;} else {window.open(this.src);}” src=”http://www.mndsoft.com/blog/images/code.gif” onload=”if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt=’点击在新窗口浏览图片\nCTRL+Mouse 滚轮可放大/缩小’;}” border=”0″> 程序代码
<%@ Property Name=“ClassName” Type=“String” Category=“Context”
Description=“Class Name” %>

  该属性声明将使 ClassName 属性出现在
CodeSmith 属性窗口中,以便可以在模板运行时指定它。下一步是实际生成模板主体,它非常类似于用 ASP.NET 进行编码。你可以在 Figure 3
附件
中查看该模板的主体。
  正如你所见,该模板接受字符串输入并使用该类名生成单独的类。在模板主体中,使用与 ASP.NET
中相同的起始和结束标记。在该模板中,我只是插入属性值,但你还可以在这些标记内部使用任意类型的 .NET 代码。在该模板完成之后,你就可以通过双击它或者从
CodeSmith 应用程序中打开它将其加载到 CodeSmith 中。Figure 4 显示了已经加载到 CodeSmith
中的该模板。
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor=’hand’; this.alt=’点击在新窗口浏览图片\nCTRL+Mouse 滚轮可放大/缩小’;}” onclick=”if(!this.resized) {return true;} else {window.open(this.src);}” src=”http://www.pconline.com.cn/pcedu/empolder/net/vc/0509/pic/05-09-13-net-4.gif” onload=”if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt=’点击在新窗口浏览图片\nCTRL+Mouse 滚轮可放大/缩小’;}” border=”0″>

  你可以看到左侧的属性正是我在该模板中声明的属性。如果我输入“SingletonClass”作为类名,并单击
Generate 按钮,则生成 Figure 3 的底部显示的类。
  CodeSmith
使用起来相当容易,如果能够正确应用,则可以产生一些令人难以置信的结果。面向代码生成的应用程序中最常见的部分之一是数据访问层。CodeSmith 包括一个名为
SchemaExplorer 的特殊的程序集,可用来从表、存储过程或几乎任何其他 SQL Server? 对象生成模板。
CodeSmith 由 Eric
J. Smith 编写.
下载地址:http://www.ericjsmith.net/codesmith.

NUnit
  NUnit
是为 .NET 框架生成的开放源代码单元测试框架。NUnit
使你可以用你喜欢的语言编写测试,从而测试应用程序的特定功能。当你首次编写代码时,单元测试是一种测试代码功能的很好方法,它还提供了一种对应用程序进行回归测试的方法。NUnit
应用程序提供了一个用于编写单元测试的框架,以及一个运行这些测试和查看结果的图形界面。
编写 NUnit 测试
  作为示例,我将测试 .NET
框架中 Hashtable 类的功能,以确定是否可以添加两个对象并且随后检索这些对象。我的第一步是添加对 NUnit.Framework
程序集的引用,该程序集将赋予我对 NUnit 框架的属性和方法的访问权。接下来,我将创建一个类并用 TestFixture 属性标记它。该属性使 NUnit
可以知道该类包含 NUnit 测试: screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor=’hand’; this.alt=’点击在新窗口浏览图片\nCTRL+Mouse 滚轮可放大/缩小’;}” onclick=”if(!this.resized) {return true;} else {window.open(this.src);}” src=”http://www.mndsoft.com/blog/images/code.gif” onload=”if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt=’点击在新窗口浏览图片\nCTRL+Mouse 滚轮可放大/缩小’;}” border=”0″> 程序代码
using System;
using System.Collections;
using
NUnit.Framework;
namespace NUnitExample
{
[TestFixture]
public
class HashtableTest
{
public HashtableTest()
{
}
}
}

  下一步,我将创建一个方法并用 [Test] 属性标记它,以便
NUnit 知道该方法是一个测试。然后,我将建立一个 Hashtable 并向其添加两个值,再使用 Assert.AreEqual
方法查看我是否可以检索到与我添加到 Hashtable 的值相同的值,如下面的代码所示: [Test] screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor=’hand’; this.alt=’点击在新窗口浏览图片\nCTRL+Mouse 滚轮可放大/缩小’;}” onclick=”if(!this.resized) {return true;} else {window.open(this.src);}” src=”http://www.mndsoft.com/blog/images/code.gif” onload=”if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt=’点击在新窗口浏览图片\nCTRL+Mouse 滚轮可放大/缩小’;}” border=”0″> 程序代码
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!”
);
}

  这将确认我可以首先向 Hashtable 中添加值并随后检索相应的值 —
这是一个很简单的测试,但能够表现 NUnit 的功能。存在许多测试类型以及各种 Assert
方法,可使用它们来测试代码的每个部分。
  要运行该测试,我需要生成项目,在 NUnit 应用程序中打开生成的程序集,然后单击 Run 按钮。Figure
5 显示了结果。当我看到那个大的绿色条纹时,我有一种兴奋和头晕的感觉,因为它让我知道测试已经通过了。这个简单的示例表明 NUnit
和单元测试是多么方便和强大。由于能够编写可以保存的单元测试,并且每当你更改代码时都可以重新运行该单元测试,你不仅可以更容易地检测到代码中的缺陷,而且最终能够交付更好的应用程序。
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor=’hand’; this.alt=’点击在新窗口浏览图片\nCTRL+Mouse 滚轮可放大/缩小’;}” onclick=”if(!this.resized) {return true;} else {window.open(this.src);}” src=”http://www.pconline.com.cn/pcedu/empolder/net/vc/0509/pic/05-09-13-net-5.gif” onload=”if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt=’点击在新窗口浏览图片\nCTRL+Mouse 滚轮可放大/缩小’;}” border=”0″>

   NUnit是一个开放源代码项目,下载: http://www.nunit.org
     
还有一个优秀的 NUnit Visual Studio .NET 外挂程序,它使你可以直接从 Visual Studio 中运行单元测试。你可以在http://sourceforge.net/projects/nunitaddin 找到它。有关 NUnit
及其在测试驱动开发中的地位的详细信息,请参阅文章:“Test-Driven C#: Improve the Design and Flexibility of
Your Project with Extreme Programming Techniques

FxCop

  .NET
框架非常强大,这意味极有可能创建优秀的应用程序,但也同样存在创建劣质程序的可能。FxCop
是有助于创建更好的应用程序的工具之一,通过分析程序集,并使用许多不同的规则来检查它是否符合这些规则。FxCop 随附了由 Microsoft
创建的一组规则,你也可以创建并包括你自己的规则。例如,如果你决定所有的类都应该具有一个不带任何参数的默认构造函数,则可以编写一条规则,以确保程序集的每个类上都具有一个构造函数。这样,无论是谁编写该代码,
你都将获得一定程度的一致性。如果你需要有关创建自定义规则的详细信息,参见 John Robbins 有关这方面的 Bugslayer 专栏文章。

  那么,让我们看看实际运行的 FxCop,并且留心一下它在我正在开发的 NUnitExample 程序集中找到什么错误。当你打开 FxCop
时,你首先需要创建一个 FxCop 项目,然后向其添加你要测试的程序集。在将该程序集添加到项目以后,就可以按 Analyze,FxCop
将分析该程序集。Figure 6 显示了 FxCop 在该程序集中找到的错误和警告。
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor=’hand’; this.alt=’点击在新窗口浏览图片\nCTRL+Mouse 滚轮可放大/缩小’;}” onclick=”if(!this.resized) {return true;} else {window.open(this.src);}” src=”http://www.pconline.com.cn/pcedu/empolder/net/vc/0509/pic/05-09-13-net-6.gif” onload=”if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt=’点击在新窗口浏览图片\nCTRL+Mouse 滚轮可放大/缩小’;}” border=”0″>
  FxCop
在我的程序集中找到了几个问题。你可以双击某个错误以查看详细信息,包括规则说明以及在哪里可以找到更多信息。(你可以做的一件有趣的事情是在框架程序集上运行
FxCop 并查看发生了什么事情。)
  FxCop 可以帮助你创建更好的、更一致的代码,但它无法补偿低劣的应用程序设计或非常简单拙劣的编程。FxCop
也不能替代对等代码检查,但是因为它可以在进行代码检查之前捕获大量错误,所以你可以花费更多时间来解决严重的问题,而不必担心命名约定。FxCop 由
Microsoft 开发.
下载地址:http://www.gotdotnet.com/team/fxcop.

Lutz Roeder
的 .NET Reflector

  下一个必不可少的工具称为 .NET
Reflector,它是一个类浏览器和反编译器,可以分析程序集并向你展示它的所有秘密。.NET 框架向全世界引入了可用来分析任何基于 .NET
的代码(无论它是单个类还是完整的程序集)的反射概念。反射还可以用来检索有关特定程序集中包含的各种类、方法和属性的信息。使用 .NET
Reflector,你可以浏览程序集的类和方法,可以分析由这些类和方法生成的 Microsoft 中间语言 (MSIL),并且可以反编译这些类和方法并查看 C#
或 Visual Basic ?.NET 中的等价类和方法。
  为了演示 .NET Reflector 的工作方式,我将加载和分析前面已经显示的
NUnitExample 程序集。Figure 7 显示了 .NET Reflector 中加载的该程序集。
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor=’hand’; this.alt=’点击在新窗口浏览图片\nCTRL+Mouse 滚轮可放大/缩小’;}” onclick=”if(!this.resized) {return true;} else {window.open(this.src);}” src=”http://www.pconline.com.cn/pcedu/empolder/net/vc/0509/pic/05-09-13-net-7.gif” onload=”if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt=’点击在新窗口浏览图片\nCTRL+Mouse 滚轮可放大/缩小’;}” border=”0″>
Figure 7 NUnitExample 程序集
  在 .NET Reflector
内部,有各种可用来进一步分析该程序集的工具。要查看构成某个方法的 MSIL,请单击该方法并从菜单中选择 Disassembler。
  除了能够查看
MSIL 以外,你还可以通过选择 Tools 菜单下的 Decompiler 来查看该方法的 C# 形式。通过在 Languages
菜单下更改你的选择,你还可以查看该方法被反编译到 Visual Basic .NET 或 Delphi 以后的形式。以下为 .NET Reflector
生成的代码: screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor=’hand’; this.alt=’点击在新窗口浏览图片\nCTRL+Mouse 滚轮可放大/缩小’;}” onclick=”if(!this.resized) {return true;} else {window.open(this.src);}” src=”http://www.mndsoft.com/blog/images/code.gif” onload=”if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt=’点击在新窗口浏览图片\nCTRL+Mouse 滚轮可放大/缩小’;}” border=”0″> 程序代码
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!”
);
}

前面的代码看起来非常像我为该方法实际编写的代码。以下为该程序集中的实际代码:
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor=’hand’; this.alt=’点击在新窗口浏览图片\nCTRL+Mouse 滚轮可放大/缩小’;}” onclick=”if(!this.resized) {return true;} else {window.open(this.src);}” src=”http://www.mndsoft.com/blog/images/code.gif” onload=”if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt=’点击在新窗口浏览图片\nCTRL+Mouse 滚轮可放大/缩小’;}” border=”0″> 程序代码
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!”
);
}

  尽管上述代码中存在一些小的差异,但它们在功能上是完全相同的。
  虽然该示例是一种显示实际代码与反编译代码之间对比的好方法,但在我看来,它并不代表
.NET Reflector 所具有的最佳用途 — 分析 .NET 框架程序集和方法。.NET 框架提供了许多执行类似操作的不同方法。例如,如果你需要从 XML
中读取一组数据,则存在多种使用 XmlDocument、XPathNavigator 或 XmlReader 完成该工作的不同方法。通过使用 .NET
Reflector, 你可以查看 Microsoft 在编写数据集的 ReadXml 方法时使用了什么,或者查看他们在从配置文件读取数据时做了哪些工作。.NET
Reflector 还是一个了解以下最佳实施策略的优秀方法:创建诸如 HttpHandlers 或配置处理程序之类的对象,因为你可以了解到 Microsoft
工作组实际上是如何在框架中生成这些对象的。
  .NET Reflector 由 Lutz Roeder 编写。
下载地址:http://www.aisto.com/roeder/dotnet.

NDoc

  编写代码文档资料几乎总是一项令人畏惧的任务。我所说的不是早期设计文档,甚至也不是更为详细的设计文档;我说的是记录类上的各个方法和属性。NDoc
工具能够使用反射来分析程序集,并使用从 C# XML 注释生成的 XML 自动为代码生成文档资料。XML 注释仅适用于 C#,但有一个名为
VBCommenter 的 Visual Studio .NET Power Toy,它能够为 Visual Basic .NET
完成类似的工作。此外,下一版本的 Visual Studio 将为更多语言支持 XML 注释。
  使用 NDoc
时,你仍然在编写代码的技术文档,但你是在编写代码的过程中完成了文档编写工作(在 XML 注释中),而这更容易忍受。使用 NDoc 时,第一步是为你的程序集打开
XML 注释生成功能。右键单击该项目并选择 Properties | Configuration Properties | Build,然后在 XML
Documentation File 选项中输入用于保存 XML 文件的路径。当该项目生成时,将创建一个 XML 文件,其中包含所有 XML 注释。下面是
NUnit 示例中的一个用 XML 编写了文档的方法:
///
/// This test adds a
number of values to the Hashtable collection
/// and then retrieves those
values and checks if they match.
///
[Test]
public void
HashtableAddTest()
{
//Method Body Here
}

  有关该方法的 XML
文档资料将被提取并保存在 XML 文件中,如下所示:

name=”M:NUnitExample.HashtableTest.HashtableAddTest”>
This
test adds a number of values to the Hashtable collection
and then retrieves
those values and checks if they match.

  NDoc 使用反射来考察你的程序集,然后读取该文档中的
XML,并且将它们进行匹配。NDoc 使用该数据来创建任意数量的不同文档格式,包括 HTML 帮助文件 (CHM)。在生成 XML 文件以后,下一步是将程序集和
XML 文件加载到 NDoc 中,以便可以对它们进行处理。通过打开 NDoc 并单击 Add 按钮,可以容易地完成该工作。
  在将程序集和 XML
文件加载到 NDoc 中并且使用可用的属性范围自定义输出以后,单击 Generate 按钮将启动生成文档资料的过程。使用默认的属性,NDoc
可以生成一些非常吸引人并且实用的 .html 和 .chm 文件,从而以快速有效的方式自动完成原来非常乏味的任务。
  NDoc
是一个开放源代码项目。
下载地址:http://ndoc.sourceforge.net.

NAnt

  NAnt
是一个基于 .NET 的生成工具,与当前版本的 Visual Studio .NET
不同,它使得为你的项目创建生成过程变得非常容易。当你拥有大量从事单个项目的开发人员时,你不能依赖于从单个用户的座位进行生成。你也不希望必须定期手动生成该项目。你更愿意创建每天晚上运行的自动生成过程。NAnt
使你可以生成解决方案、复制文件、运行 NUnit 测试、发送电子邮件,等等。遗憾的是,NAnt
缺少漂亮的图形界面,但它的确具有可以指定应该在生成过程中完成哪些任务的控制台应用程序和 XML 文件。注意,MSBuild(属于 Visual Studio
2005 的新的生成平台)为每种健壮的生成方案进行了准备,并且由基于 XML 的项目文件以类似的方式驱动。

实际运行的 NAnt
  在该示例中,我将为前面创建的 NUnitExample 解决方案创建一个 NAnt
版本文件。首先,我需要创建一个具有 .build 扩展名的 XML 文件,将其放在我的项目的根目录中,然后向该文件的顶部添加一个 XML
声明。我需要添加到该文件的第一个标记是 project 标记:
version=”1.0″?>

basedir=”.”>
The NUnit Example
Project

  项目标记还用于设置项目名称、默认目标以及基目录。Description
标记用于设置该项目的简短说明。
  接着,我将添加 property
标记,该标记可用于将设置存储到单个位置(随后可以从文件中的任意位置访问该位置)。在该例中,我将创建一个名为 debug 的属性,我可以随后将其设置为 true

false,以反映我是否要在调试配置下编译该项目。(最后,这一特定属性并未真正影响如何生成该项目;它只是你设置的一个变量,当你真正确定了如何生成该项目时将读取该变量。)

  接下来,我需要创建一个 target 标记。一个项目可以包含多个可在 NAnt 运行时指定的 target。如果未指定 target,则使用默认
target(我在 project 元素中设置的 target)。在该示例中,默认 target 是 build。让我们观察一下 target
元素,它将包含大多数生成信息:

description=”compiles the source code”>
  在 target
元素内,我将把 target 的名称设置为 build,并且创建有关该 target 将做哪些工作的说明。我还将创建一个 csc 元素,该元素用于指定应该传递给
csc C# 编译器的数据。让我们看一下该 csc 元素:

output=”.\bin\debug\NUnitExample.dll”

debug=”${debug}”>

name=”HashtableTest.cs”/>

  首先,我必须设置该 csc 元素的 target。在该例中,我将创建一个 .dll 文件,因此我将 target 设置为
library。接下来,我必须设置 csc 元素的 output,它是将要创建 .dll 文件的位置。最后,我需要设置 debug
属性,它确定了是否在调试中编译该项目。因为我在前面创建了一个用于存储该值的属性,所以我可以使用下面的字符串来访问该属性的值:${debug}。Csc
元素还包含一些子元素。我需要创建两个元素:references 元素将告诉 NAnt 需要为该项目引用哪些程序集,sources 元素告诉 NAnt
要在生成过程中包含哪些文件。在该示例中,我引用了 NUnit.Framework.dll 程序集并包含了 HashtableTest.cs 文件。Figure
8附件中显示了完整的生成文件。(你通常还要创建一个干净的 target,用于删除生成的文件,但为了简洁起见,我已经将其省略。)

  要生成该文件,我需要转到我的项目的根目录(生成文件位于此处),然后从该位置执行 nant.exe。如果生成成功,你可以在该应用程序的 bin
目录中找到 .dll 和 .pdb 文件。尽管使用 NAnt 肯定不像在 Visual Studio 中单击 Build
那样简单,但它仍然是一种非常强大的工具,可用于开发按自动计划运行的生成过程。NAnt
还包括一些有用的功能,例如能够运行单元测试或者复制附加文件(这些功能没有受到当前 Visual Studio 生成过程的支持)。 NAnt
是一个开放源代码项目。
下载地址:http://nant.sourceforge.net/

ASPNET转换工具
  我已经将两个独立的工具合在一起放在标题“转换工具”下面。这两个工具都非常简单,但又可能极为有用。第一个工具是
ASP.NET 版本转换器,它可用于转换 ASP.NET(虚拟目录在它下面运行)的版本。第二个工具是 Visual Studio
Converter,它可用于将项目文件从 Visual Studio .NET 2002 转换到 Visual Studio .NET 2003。
  当
IIS 处理请求时,它会查看正在请求的文件的扩展名,然后基于该 Web 站点或虚拟目录的扩展名映射,将请求委派给 ISAPI 扩展或者自己处理该请求。这正是
ASP.NET 的工作方式;将为所有 ASP.NET 扩展名注册扩展名映射,并将这些扩展名映射导向
aspnet_isapi.dll。这种工作方式是完美无缺的,除非你安装了 ASP.NET 1.1 — 它会将扩展名映射升级到新版本的
aspnet_isapi.dll。当在 ASP.NET 1.0 上生成的应用程序试图用 1.1
版运行时,这会导致错误。要解决该问题,可以将所有扩展名映射重新转换到 1.0 版的 aspnet_isapi.dll,但是由于有 18
种扩展名映射,所以手动完成这一工作将很枯燥。这正是 ASP.NET 版本转换器可以发挥作用的时候。使用这一小型实用工具,可以转换任何单个 ASP.NET
应用程序所使用的 .NET 框架的版本。
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor=’hand’; this.alt=’点击在新窗口浏览图片\nCTRL+Mouse 滚轮可放大/缩小’;}” onclick=”if(!this.resized) {return true;} else {window.open(this.src);}” src=”http://www.pconline.com.cn/pcedu/empolder/net/vc/0509/pic/05-09-13-net-9.gif” onload=”if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt=’点击在新窗口浏览图片\nCTRL+Mouse 滚轮可放大/缩小’;}” border=”0″>
Figure 9 ASP.NET 版本转换器
  Figure 9 显示了实际运行的 ASP.NET
版本转换器。它的使用方法非常简单,只须选择相应的应用程序,然后选择你希望该应用程序使用的 .NET 框架版本。该工具随后将使用
aspnet_regiis.exe 命令行工具将该应用程序转换到所选版本的框架。随着将来版本的 ASP.NET 和 .NET
框架的发布,该工具将变得更为有用。
  ASP.NET 版本转换器由 Denis Bauer 编写.
下载地址:http://www.denisbauer.com/NETTools/ASPNETVersionSwitcher.aspx.

Visual
Studio .NET 项目转换器

(参见Figure 10)非常类似于 ASP.NET 版本转换器,区别在于它用于转换 Visual
Studio 项目文件的版本。尽管在 .NET 框架的 1.0 版和 1.1 版之间只有很小的差异,但一旦将项目文件从 Visual Studio .NET
2002 转换到 Visual Studio .NET 2003,将无法再把它转换回去。虽然这在大多数时候可能不会成为问题(因为在 .NET 框架 1.0 版和
1.1 版之间几乎没有什么破坏性的更改),但在某些时刻你可能需要将项目转换回去。该转换器可以将任何解决方案或项目文件从 Visual Studio 7.1
(Visual Studio .NET 2003) 转换到 Visual Studio 7.0 (Visual Studio .NET
2002),并在必要时进行反向转换。
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor=’hand’; this.alt=’点击在新窗口浏览图片\nCTRL+Mouse 滚轮可放大/缩小’;}” onclick=”if(!this.resized) {return true;} else {window.open(this.src);}” src=”http://www.pconline.com.cn/pcedu/empolder/net/vc/0509/pic/05-09-13-net-10.gif” onload=”if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt=’点击在新窗口浏览图片\nCTRL+Mouse 滚轮可放大/缩小’;}” border=”0″>
Figure 10 Visual Studio .NET 项目转换器
Visual Studio .NET 项目转换器由
Dacris Software 编写。
下载地址:http://www.codeproject.com/macro/vsconvert.asp.

总结
  本文采用走马观花的方式介绍了上述工具,但我已经试图起码向你提供足够的信息以激起你的好奇心。我相信本文已经让你在某种程度上领悟了几个免费工具,你可以立即开始使用这些工具来编写更好的项目。同时,我还要敦促
你确保自己拥有所有其他可以获得的合适工具,无论是最新版本的 Visual
Studio、功能强大的计算机还是免费的实用工具。拥有合适的工具将使一切变得大不相同。

作者简介
  James Avery
是一位使用 .NET 和其它微软技术的顾问。他撰写了许多书籍和文章,其最新著作是《ASP.NET Setup and Configuration Pocket
Reference》(Microsoft Press, 2003)。你可以通过 javery@infozerk.com 向他发送电子邮件,并且在 http://www.dotavery.com/blog 阅读他的网络日记。

Powered by ScribeFire.

有些是Open
Source
,有些则是商业付费软件,总之如果您公司有在使用VS 2005 Team
System
,则此软件列表具有不错的参考价值

Team Foundation Server
(Utilities)

Team Foundation Server (Clients)

Team Edition for Software
Architects

Team Edition for Software
Developers

Team Edition for Software
Testers

Requirements Authoring, Validation, and
Management

Process Templates

Process Guidance

Alternate Merge/Compare Tools

MS Build Custom Tasks

Miscellaneous/Non-Widgets

Powered by ScribeFire.

NAnt 是一个基于 .NET 的生成工具,与当前版本的 Visual Studio .NET 不同,它使得为您的项目创建生成过程变得非常容易。当您拥有大量从事单个项目的开发人员时,您不能依赖于从单个用户的座位进行生成。您也不希望必须定期手动生成该项目。您更愿意创建每天晚上运行的自动生成过程。NAnt 使您可以生成解决方案、复制文件、运行 NUnit 测试、发送电子邮件,等等。遗憾的是,NAnt 缺少漂亮的图形界面,但它的确具有可以指定应该在生成过程中完成哪些任务的控制台应用程序和 XML 文件。注意,MSBuild(属于 Visual Studio 2005 的新的生成平台)为每种健壮的生成方案进行了准备,并且由基于 XML 的项目文件以类似的方式驱动。

1、系统需求

使用Nant,需要具备以下一种CRL:

·  Microsoft .NET Framework 1.0

·  Microsoft .NET Framework 1.1

·  Microsoft .NET Framework 2.0 Beta 1

·  Mono 1.0.x

依赖的库文件

Nant使用了许多开源的第三方组件库,nant的最近版本中包含了这些组件,在安装nant时不需要做额外的工作。有关这些组件的更多信息请参考以下链接:

 NUnit - Required for unit testing

 NDoc - Required for documentation generation

 SharpZipLib - Required for the zip and unzip tasks

2、安装

无论是源代码还是编译好的二进制文件,Nant都是可以使用的。编译好的二进制文件是建立工程所需要的,包括构建tasks, types 和 functions。

从二进制文件安装:

1)       下载nant-bin.zip 或nant-bin.tar.gz

2)       从机器上删除以前的旧版本

3)        解压缩下载的压缩文件到你期望安装NAnt的地方

4)         根据你的机器环境,构建一个脚本文件来运行Nant:

Ø         在.NET下运行Nant:

新建一个批处理文件,如nant.bat,在里面加入如下内容,注意把红色部分换成自己机器上的Nant的安装路径,然后运行这个批处理文件即可:

          @echo off

          “C:\Program Files\NAnt\bin\NAnt.exe” %*

Ø         在Mono下运行Nant:

Windows环境:同.NET

Linux / Cygwin:在你的文件系统的适当位置创建一个名称为nant的文件(例如/usr/local/bin),在文件中加入以下内容:

          #!/bin/sh

          exec mono /usr/local/nant/bin/NAnt.exe “$@”

          确保nant有运行的权限,如:chmod a+x /usr/local/bin/nant

5)         打开命令行窗口,把目录切换到装有nant的文件夹,执行nant –help,如果安装正确,你就会看到以命令行选项显示的使用信息。

6)         (选做)下载、安装NAnt-contrib或其他第三方扩展程序。

从源代码进行安装:

1)        下载nant-src.zip或nant-src.tar.gz

2)       从机器上删除以前的旧版本

3)        解压缩下载的压缩文件到你期望安装NAnt的地方

4)        打开命令行提示符窗口,把目录切换到你把文件解压缩到的地方

5)         根据你的机器环境,编译Nant:

Ø         在.NET下:

n          GNU Make

make install MONO= MCS=csc prefix=installation-path

例如: make install MONO= MCS=csc prefix=”C:\Program Files”

n          NMake

nmake -f Makefile.nmake install prefix=installation-path

例如: nmake -f Makefile.nmake install prefix=”C:\Program Files”

Ø         在Mono下:

§         GNU Make

make install prefix=installation-path

eg. make install prefix=”C:\Program Files”

§         NMake

nmake -f Makefile.nmake install MONO=mono CSC=mcs prefix=installation-path

eg. nmake -f Makefile.nmake install MONO=mono CSC=mcs prefix=/usr/local/

这会生成一个bootstrap版本的nant,然后使用它生成、安装full版本的nant:installation-path/NAnt

6)         打开命令行窗口,把目录切换到装有nant的文件夹,执行nant –help,如果安装正确,你就会看到以命令行选项显示的使用信息。

7)         (选做)下载、安装NAnt-contrib或其他第三方扩展程序。 

3、运行例子程序

打开命令行窗口,把目录切换到装有nant的文件夹,输入以下命令行并执行,注意红色部分换成自己的例子程序的路径:

Nant –buildfile:..\examples\examples.build

成功会显示编译信息。

<二>运行Nant

1、运行nant

一旦安装了Nant,运行是很简单的,仅仅是输入nant。输入nant –help能够得到用法信息。

1)         指定build文件

没有指定文件时,NAnt会在当前文件夹中寻找.build文件,例如NAnt.build。如果找到了,NAnt就把它作为build文件使用。如果找到了一个以上的文件,你需要使用-buildfile选项(用法见下面)指定一个文件。

如果你使用-find选项,NAnt会到父目录中寻找build文件,如此继续,直至到达根目录。使用命令行选项-buildfile:file可以使NAnt使用其它的build文件。

2)         指定对象

你可以指定一个或多个将要被执行的对象。当忽略时,标签的default属性指定的对象将被使用。

如果工程有描述存在,Projecthelp将把它打印出来,紧跟在描述后面的是工程的对象列表。

3)         设置属性

使用-D:property=value撤销build文件中指定的属性,其中property是属性的名称,value是属性的值。

4)         例子

Nant:在当前目录中使用*.build结尾的xml文件运行nant,使用缺省对象。

NAnt -buildfile:..\ProjectName.build:使用父目录下的ProjectName.build文件,,使用缺省对象。

Nant clean:在当前目录使用build文件运行nant,使用名称为clean对象。

                NAnt -D:debug=false clean dist:使用当前目录中缺省的build文件运行nant,当设置debug为false的时候,先使用clean对象,然后使用dist对象。

<三>Build文件

Nant的Build文件是用xml写的。每一个文件包含一个工程(project)和几个对象(target),每一个对象包含几个任务(taek)。下面是一个编译HelloWorld(c#)工程的简单build文件。


   

       
The Hello World of build
files.
       
overwrite=”false” />
       
           
failonerror=”false” />
           
failonerror=”false” />
       
       
name=”build” description=”compiles the source code”>
           
target=”exe” output=”HelloWorld.exe” debug=”${debug}”>
               

                   
/>
               
           
       

   

在这个例子里面,有”clean” and “build”.  两个对象。缺省时,“build”任务会被调用。

例子

examples 文件夹内,你能找到运行这些粒子所需的文件。

nant:在debug模式(缺省)下运行nant并构建工程。

nant clean:运行nant并删除已编译好的文件。

nant -D:debug=false:在non-debug 模式下运行nant并生成工程。尽管build文件的debug
属性为真(true),命令行中设置的值不会受影响,就像
任务中的“overwrite”属性被设置为假(false)。

重要提示:如果产生的文件的日期比源文件的日期早,将仅仅执行像编译器任务这样的任务。如果你在debug模式下编译HelloWorld工程,然后什么东西也不清除,在no-debug模式下重新编译,这种情况就会发生,因为nant工程不需要被重新生成。


Powered by ScribeFire.

 NAnt 是一个Visual Studio
.Net
应用程序的连编工具,对大而负责的工程而言,使用NAnt很方便。

1.      安装

http://nant.sourceforge.net上可以下载源代码或者编译好的二进制文件,一般下载nant-bin.zip,解压,注册系统环境变量后,就可以使用nant命令了。

2.     
Build文件

XML格式,每个build文件包含一个projectproject有若干target,每个target包含若干taskTask不被包含在target中,即直接包含在project中。

Ø      
Projects(工程)

三个特性,name、设置工程名称,default、设置默认target,和basedir、如果不设置,默认为build文件的父目录。

执行nant时,可以知道targets,如果未指定,执行默认得target,如果build文件中未指定默认得target,仅仅执行全局的task(全局的task总是执行),如果没有全局的task,那就什么都不执行了。

Ø      
Targets(目标、对象)

有五个特性,name、指定名称,depends、此target所依赖的targets(多个用“,”隔开),if、如果条件为true执行此targetunless、如果条件为true跳过此targetdescription、功能的简短描述。其中name必须指定。

执行某个target时,首先执行此target所依赖的targets。一个target可能被depends多次,仅执行一次,但是通过任务(task)执行一个target时,此target及其所依赖的targets要重新执行一次。另外,当target的名称被设置为“*”时,称为wild
target
,一个build文件仅仅有一个wild
target
,在目前的build文件中,当且仅当被调用的target不存在时,才执行wild
target
,主要用来处理无效的请求。

Ø      
Task(任务)

一块可执行的代码,一个task可有多个特性和参数。特性的值可包含对属性的引用,在task执行前,这些引用会被解释出来。

Ø      
Properties(属性)

一个project可有很多属性,这些属性可以通过任务设置在build文件中,也可以设置在Nant外。一个属性有一个name和一个value,可用于task的特性中,也可用于表达式,用在task的特性中时,使用${property
name}
格式。Nant有很多内建属性(与nant有关的,与框架有关的,与平台有关的等)。

任务有readonlyoverwrite等特性,readonly指定属性是否是只读到,默认falseoverwirte指定如果属性已经存在,属性值是否可以重写,默认true,对于只读属性,是不能被重写的。注意:在Nant命令行指定的属性,先于在build文件中指定的属性,并且这些属性往往是只读的。

另外可以在NAnt.exe.config文件中定义全局属性。

Ø       Loggers &
Listeners

Nant
通过LoggersListeners来监控连编过程。Listeners记录了build
started
build
finished
target
started
target finishedtask
started
task
finished
message
logged
事件,Loggers扩展了Listeners,可以按-quiet(静态的,平稳的),
-verbose
(详细的),
-debug
三个层次记录连编信息,可输出到控制台或者文件中。Nant内建了三个类:NAnt.Core.DefaultLoggerNAnt.Core.MailLoggerNAnt.Core.XmlLogger。使用时:-logger:类
-logfile:文件名。可以是普通道文本文件或者XML文件。

Ø      
Expressions(表达式)

表达式是一种简单而强大的机制,允许写高级的公式,用于task的参数和条件式中,这样就可以控制连编过程了。表达式能够访问project的属性、调用内建的或者用户定义的functions

表达式通过${…}符号,可用于task的参数中。也可以使用标准的算术、逻辑和关系运算符。通过prefix::function-name(argument1,
…,
argumentN)
语法调用函数。访问属性,仅需指定其name${…}

例子:

访问属性


/>

调用函数


/>

表达式结果存储


value=”${file::exists(’c:\autoexec.bat’)}” />

Real-life expression use


value=”${path::combine(myprj.basedir,’version.txt’)}” />

 

   

有条件执行task


unless=”property::exists(’myprj.basedir’)” />

     if=”${datetime::now() -
file::get-last-write-time(’out.dll’)) >
timespan::from-hours(1)}”>

Functions

Nant提供了操作字符串、日期时间和路径名字的函数,还提供读取文件或者目录属性,访问目前的连编信息等函数。

调用函数语法prefix::function-name(argument1, …,
argumentN)
,需要的情况下会进行参数类型转换,如果转换有误,会报告错误。

自定义函数可以使用任何.Net语言实现,此外还可以通过任务实现。

3.     
运行Nant

命令Nant,此外还可以指定build文件名、targetsproperties等。

Nant

NAnt -buildfile:..\fileName.build

NAnt clean

NAnt -D:debug=false clean dist

Powered by ScribeFire.