partial 部分类

partial关键字允许把类,结构或接口放在多个文件中,一般情况下,一个类存储在单个文件中,但有时,多个开发人员需要访问同一个类,或者某种类型的代码生成器生成了一个类的某部分,所以把类放在多个文件中是有益的。

 

Code
//BigClassPart1.cs
partial class TheBigClass
{
  
public void MethodOne(){}
}

//BigClassPart2.cs
partial class TheBigClass 
{
  
public void MethodTwo(){}

 

编译包含这两个源文件的项目时,会创建一个TheBigClass类,它有两个方法MethodOne和MethodTwo,如果声明类时使用了下面的关键字,这些关键字将应用于一个类的所有部分:public private protected,internal,abstract sealed new 一般约束

 

Code
//BigClassPart1.cs
[CustomAttribute]
partial class TheBigClass:TheBigBaseClass,IBigClass
{
  
public void MethodOne(){}
}

//BigClassPart2.cs
[AnotherAttribute]
partial class TheBigClass:IOtherBigClass
{
  
public void MethodTow(){}
}

//编译后

[CustomAttribute]
[AntherAttribute]
partial class TheBigClass:TheBigBaseClass:IBigClass:IOtherBigClass
{
  
public void MethodOne(){}
  
public void MethodTwo(){}
}
posted @ 2009-03-16 14:20  君未鸣  阅读(187)  评论(0编辑  收藏  举报