Partial Public Class zzFileConverterRegistrar
Event Register(ByVal mainConverter as zzFileConverter)
Sub registerAll(ByVal mainConverter as zzFileConverter)
RaiseEvent Register(mainConverter)
End Sub
End Class
每个希望注册一个或多个类型的文件转换器的模块可以包括如下内容:
Partial Public Class zzFileConverterRegistrar
Private Sub RegisterGif(ByVal mainConverter as zzFileConverter) Handles Me.Register
mainConverter.RegisterConverter("GIF", GifConverter.NewFactory))
End Sub
End Class
< b >编辑/补遗
需要明确的是,这样做的目的是提供一种方法,通过这种方法,各种单独的类可以让主程序或类知道它们。主文件转换器使用zzFileConverterRegistrar要做的唯一一件事是创建它的一个实例并调用registerAll方法,该方法将触发Register事件。任何想要钩住该事件的模块都可以执行任意代码来响应它(这就是整个思想),但是模块不能通过不恰当地扩展zzFileConverterRegistrar类来做任何事情,只能定义一个名称与其他方法名称匹配的方法。一个写得不正确的扩展破坏另一个写得不正确的扩展当然是可能的,但解决这个问题的解决方案是,对于任何不想让他的扩展破坏的人来说,只要正确地编写它
partial class MyClass : IF3
{
// main implementation of MyClass
}
partial class MyClass : IF1
{
// implementation of IF1
}
partial class MyClass : IF2
{
// implementation of IF2
}
in Post.cs
public partial class XMLDAO :BigAbstractClass
{
// CRUD methods of post..
}
in Comment.cs
public partial class XMLDAO :BigAbstractClass
{
// CRUD methods of comment..
}
in Pages.cs
public partial class XMLDAO :BigAbstractClass
{
// CRUD methods of Pages..
}
public partial class Product
{
// 50 business logic embedded in methods and properties..
}
public partial class Product
{
// another 50 business logic embedded in methods and properties..
}
//finally compile with product.class file.
Here are some points to consider while implementing partial classes:-
Use partial keyword in each part of partial class.
The name of each part of partial class should be the same but the source file name for each part of partial class can be different.
All parts of a partial class should be in the same namespace.
Each part of a partial class should be in the same assembly or DLL, in other words you can't create a partial class in source files from a different class library project.
Each part of a partial class must have the same accessibility. (i.e: private, public or protected)
If you inherit a class or interface on a partial class then it is inherited by all parts of that partial class.
If a part of a partial class is sealed then the entire class will be sealed.
If a part of partial class is abstract then the entire class will be considered an abstract class.
当使用自动生成的源文件时,可以将代码添加到类中,而不必重新创建源文件。例如,您正在使用LINQ to SQL并创建一个DBML文件。现在,当你拖拽一个表时,它会在designer.cs中创建一个分部类,并且所有表列在类中都有属性。您需要在这个表中绑定更多的列到UI网格上,但您不想向数据库表中添加新列,因此您可以为这个类创建一个单独的源文件,该文件具有该列的新属性,它将是一个partial类。因此,这确实会影响数据库表和DBML实体之间的映射,但您可以轻松地获得一个额外的字段。这意味着您可以自己编写代码,而不会破坏系统生成的代码。