我想知道是否有人能给我一个概述,为什么我要使用它们,在这个过程中我能获得什么好处。
当前回答
Partial classes are primarily introduced to help Code generators, so we (users) don't end up loosing all our work / changes to the generated classes like ASP.NET's .designer.cs class each time we regenerate, almost all new tools that generate code LINQ, EntityFrameworks, ASP.NET use partial classes for generated code, so we can safely add or alter logic of these generated codes taking advantage of Partial classes and methods, but be very carefully before you add stuff to the generated code using Partial classes its easier if we break the build but worst if we introduce runtime errors. For more details check this https://web.archive.org/web/20211020111732/https://www.4guysfromrolla.com/articles/071509-1.aspx
其他回答
Partial classes are primarily introduced to help Code generators, so we (users) don't end up loosing all our work / changes to the generated classes like ASP.NET's .designer.cs class each time we regenerate, almost all new tools that generate code LINQ, EntityFrameworks, ASP.NET use partial classes for generated code, so we can safely add or alter logic of these generated codes taking advantage of Partial classes and methods, but be very carefully before you add stuff to the generated code using Partial classes its easier if we break the build but worst if we introduce runtime errors. For more details check this https://web.archive.org/web/20211020111732/https://www.4guysfromrolla.com/articles/071509-1.aspx
服务引用是另一个例子,其中部分类用于将生成的代码与用户创建的代码分开。
您可以在更新服务引用时“扩展”服务类,而不必覆盖它们。
分部类跨越多个文件。
如何在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#代码是独立的。 或者你可以在这里找到完整的描述。
另一个用途是分割不同接口的实现,例如:
partial class MyClass : IF3
{
// main implementation of MyClass
}
partial class MyClass : IF1
{
// implementation of IF1
}
partial class MyClass : IF2
{
// implementation of IF2
}
我知道这个问题很老了,但我想补充一下我对部分课程的看法。
我个人使用部分类的一个原因是当我为程序创建绑定时,尤其是状态机。
例如,OpenGL是一个状态机,有很多方法都可以全局更改,然而,根据我的经验,绑定类似于OpenGL的东西,其中有很多方法,类很容易超过10k LOC。
部分类将为我分解这个问题,并帮助我快速找到方法。
推荐文章
- 实体框架核心:在上一个操作完成之前,在此上下文中开始的第二个操作
- 如何为构造函数定制Visual Studio的私有字段生成快捷方式?
- 如何使用JSON确保字符串是有效的JSON。网
- AppSettings从.config文件中获取值
- 通过HttpClient向REST API发布一个空体
- 如何检查IEnumerable是否为空或空?
- 自动化invokerrequired代码模式
- 在c#代码中设置WPF文本框的背景颜色
- 在c#中,什么是单子?
- c#和Java中的泛型有什么不同?和模板在c++ ?
- c#线程安全快速(est)计数器
- 如何将此foreach代码转换为Parallel.ForEach?
- 如何分裂()一个分隔字符串到一个列表<字符串>
- 如何转换列表<字符串>列表<int>?
- c#对象列表,我如何得到一个属性的和