[译]Visual Basic 2005在语言上的增强(九)部分类型

类别:.NET开发 点击:0 评论:0 推荐:
部分类型允许在不同的源文件中定义同一个类型。在其中一个文件中可以使用普通方式进行的类型定义(作者说的“普通”的意思是不需要使用Partial关键字,涕淌注):
Public Class TestPartial
    '代码实现……
End Class

而在其他的源文件中,你必须使用Partial关键字来告诉编译器,这其中的代码是对原始类定义的一个扩展:
Partial Public Class TestPartial
    '代码实现……
End Class

类中由IDE生成的代码被标以Partial关键字,并且和用户填写的代码分离开来。你在工程中添加一个名为TestForm的窗体,来看看生成的源文件。你可能很惊讶是吧,因为除了窗体类声明的空框架之外就空空如也了。从前那些“Windows窗体自动生成的代码”区域里那些设计器生成的代码到哪儿去了呢?

现在,设计器生成的代码都被存放在TestForm.Designer.vb文件中,你可以点击解决方案浏览器工具条上的Show All Files按键来阅读这个文件。你不应该修改这个文件中的代码(实际上你是可以修改的,只是不推荐你这么做,涕淌注),但是,如果你想在设计器文件被再次生成时保留下一些东西,你可以把需要的代码复制粘贴到TestForm.vb文件中。

工程小组可以利用部分类的优点,为每一个开发人员分配编写同一个类的某一个部分类文件的任务。多人开发已经不再需要对同一个源文件进行共享访问了,因为你们现在可以把同一个类的定义分散到多个源文件中。


@以下是原文供网友们参考@

Partial Types

Partial types allow definition of a single type across multiple source files. The type definition has a normal declaration in one file:
Public Class TestPartial
    'implemention...
End Class

In other source files, you use the Partial keyword to tell the compiler that this code is a continuation of the original class definition:
Partial Public Class TestPartial
    'implemention...
End Class

Code in classes generated by designers in the IDE is marked with the Partial keyword, and is separated from user code. Add a form named TestForm to a project and look at the resulting source file. You may be surprised to see that it's empty except for the form's class declaration. Where's all the designer-generated code that used to go inside the Windows Form Designer Generated Code region?

The designer-generated code is now placed in file TestForm.designer.vb, which you can see by clicking the Show All Files button in the Solution Explorer toolbar. You shouldn't change the code in this file, but you can copy and paste what you need into the TestForm.vb file if you want to preserve something when the designer file is later regenerated.

Project teams can take advantage of partial classes by giving each developer a file containing their portion of the class. Multiple developers no longer need to share access to the same source files since you can now separate a class definition into multiple source files.

本文地址:http://com.8s8s.com/it/it42088.htm