我想知道是否有人能给我一个概述,为什么我要使用它们,在这个过程中我能获得什么好处。
当前回答
通过部分类,只需添加源文件,就可以向适当设计的程序添加功能。例如,可以设计一个文件导入程序,这样就可以通过添加处理文件的模块来添加不同类型的已知文件。例如,主文件类型转换器可以包含一个小类:
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
注意,主文件转换器类并没有“公开”——它只是公开了一个小存根类,外接程序模块可以钩子到这个存根类。存在命名冲突的轻微风险,但如果每个外接程序模块的“寄存器”例程是根据它处理的文件类型命名的,那么它们可能不会造成问题。如果担心这样的事情,可以在注册子例程的名称中插入GUID。
Edit/Addendum To be clear, the purpose of this is to provide a means by which a variety of separate classes can let a main program or class know about them. The only thing the main file converter will do with zzFileConverterRegistrar is create one instance of it and call the registerAll method which will fire the Register event. Any module that wants to hook that event can execute arbitrary code in response to it (that's the whole idea) but there isn't anything a module could do by improperly extending the zzFileConverterRegistrar class other than define a method whose name matches that of something else. It would certainly be possible for one improperly-written extension to break another improperly-written extension, but the solution for that is for anyone who doesn't want his extension broken to simply write it properly.
在不使用分部类的情况下,可以在主文件转换器类的某个地方写一些代码,如下所示:
RegisterConverter("GIF", GifConvertor.NewFactory) RegisterConverter("BMP", BmpConvertor.NewFactory) RegisterConverter("JPEG", JpegConvertor.NewFactory)
但是添加另一个转换器模块需要进入转换器代码的那一部分,并将新的转换器添加到列表中。使用partial方法,这就不再需要了——所有转换器都会自动包含。
其他回答
部分类的最大用途是使代码生成器/设计人员的工作更轻松。部分类允许生成器简单地发出它们需要发出的代码,而不需要处理用户对文件的编辑。用户同样可以通过创建第二个分部类来自由地用新成员注释类。这为关注点分离提供了一个非常清晰的框架。
更好的方法是看看设计器在部分类之前是如何工作的。WinForms设计器会吐出区域内的所有代码,并给出不修改代码的强烈注释。它必须插入各种启发式方法来找到生成的代码,以便稍后进行处理。现在它可以简单地打开designer.cs文件,并有高度的信心它只包含与设计器相关的代码。
Multiple Developer Using Partial Classes multiple developer can work on the same class easily. Code Generator Partial classes are mainly used by code generator to keep different concerns separate Partial Methods Using Partial Classes you can also define Partial methods as well where a developer can simply define the method and the other developer can implement that. Partial Method Declaration only Even the code get compiled with method declaration only and if the implementation of the method isn't present compiler can safely remove that piece of code and no compile time error will occur. To verify point 4. Just create a winform project and include this line after the Form1 Constructor and try to compile the code partial void Ontest(string s);
以下是在实现分部类时需要考虑的几点
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.
分部类跨越多个文件。
如何在c#类声明中使用分部修饰符?
使用部分类,您可以将一个类物理地分离到多个文件中。这通常由代码生成器完成。
例子
对于普通的c#类,您不能在同一个项目中的两个独立文件中声明一个类。但是对于部分修饰语,你可以。
如果一个文件经常被编辑,而另一个文件是机器生成的或很少被编辑,这是很有用的。
这里有一个例子来说明:
class Program
{
static void Main()
{
A.A1();
A.A2();
}
}
A1.cs文件内容:c#
using System;
partial class A
{
public static void A1()
{
Console.WriteLine("A1");
}
}
A2.cs文件内容:c#
using System;
partial class A
{
public static void A2()
{
Console.WriteLine("A2");
}
}
输出:
A1
A2
这里需要Partial。
如果你删除了部分修饰符,你将得到一个包含以下文本的错误:
[命名空间'<全局命名空间>'已经包含' a '的定义]。
Tip:
要解决这个问题,可以使用partial关键字,或者更改其中一个类名。
c#编译器如何处理部分类?
如果你分解上面的程序(使用IL Disassembler),你会看到文件A1.cs和A2.cs被删除了。你会发现A类出现了。
类A将在同一个代码块中包含方法A1和A2。这两个班合并为一个班。
A1.cs和A2.cs的编译结果:c#
internal class A
{
// Methods
public static void A1()
{
Console.WriteLine("A1");
}
public static void A2()
{
Console.WriteLine("A2");
}
}
总结
部分类可以简化某些c#编程情况。 当创建Windows窗体/WPF程序时,通常在Visual Studio中使用它们。 机器生成的c#代码是独立的。 或者你可以在这里找到完整的描述。
分部类的主要用途是生成代码。如果你看看WPF (Windows Presentation Foundation)网络,你用标记(XML)定义你的UI。该标记被编译成部分类。您可以用自己的部分类填充代码。
在处理大型类时,或在团队中工作时,尽可能保持所有内容的干净,您可以在不重写的情况下编辑(或始终提交更改)
推荐文章
- 如何在c#中获得正确的时间戳
- Linq选择列表中存在的对象(A,B,C)
- c# .NET中的App.config是什么?如何使用它?
- c#:如何获得一个字符串的第一个字符?
- String类中的什么方法只返回前N个字符?
- 更好的方法将对象转换为int类型
- 我可以将c#字符串值转换为转义字符串文字吗?
- 在c#中转换char到int
- c#中朋友的对等物是什么?
- 关键字使用virtual+override vs. new
- 在ASP中选择Tag Helper。NET Core MVC
- 如何在没有任何错误或警告的情况下找到构建失败的原因
- 跨线程操作无效:控件“textBox1”从创建它的线程以外的线程访问
- 否ConcurrentList<T>在。net 4.0?
- 在c#中解析字符串为日期时间