我想知道是否有人能给我一个概述,为什么我要使用它们,在这个过程中我能获得什么好处。
当前回答
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.
其他回答
服务引用是另一个例子,其中部分类用于将生成的代码与用户创建的代码分开。
您可以在更新服务引用时“扩展”服务类,而不必覆盖它们。
我看到的另一个用法是,
扩展一个关于数据访问逻辑的大型抽象类,
我有各种文件的名字Post.cs,Comment.cs,Pages.cs…
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..
}
分部类跨越多个文件。
如何在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#代码是独立的。 或者你可以在这里找到完整的描述。
除了其他答案之外……
我发现它们可以作为重构神类的垫脚石。如果一个类有多个职责(特别是如果它是一个非常大的代码文件),那么我发现为每个职责添加1x个部分类作为组织和重构代码的第一步是有益的。
这非常有帮助,因为它可以帮助使代码更具可读性,而不会实际影响执行行为。它还可以帮助确定何时易于重构某个职责,或者何时与其他方面紧密纠缠在一起。
然而,需要明确的是,这仍然是糟糕的代码,在开发结束时,您仍然希望每个类有一个职责(而不是每个分部类)。这只是一块垫脚石:)
如果您有一个足够大的类,而不适合进行有效的重构,那么将它分离到多个文件中有助于保持内容的组织性。
例如,如果您有一个包含论坛和产品系统的站点数据库,并且您不想创建两个不同的提供者类(与代理类不是一回事,只是为了清楚起见),您可以在不同的文件中创建单个分部类,例如
——核心逻辑
cs -专门与论坛相关的方法
产品的方法
这是另一种让事情井井有条的方法。
另外,正如其他人所说,这是向生成的类添加方法而不会在下次重新生成类时破坏所添加的方法的唯一方法。这对于模板生成的(T4)代码、orm等非常方便。
推荐文章
- 为什么我不能在c#中有抽象静态方法?
- net HttpClient。如何POST字符串值?
- Symfony 2.x中的所有东西都应该捆绑吗?
- 我如何使一个方法的返回类型泛型?
- 何时处理CancellationTokenSource?
- 如何获取正在执行的程序集版本?
- AutoMapper vs valueinjector
- 为什么控制台不。Writeline,控制台。在Visual Studio Express中编写工作?
- 什么是.NET程序集?
- 字符串不能识别为有效的日期时间“格式dd/MM/yyyy”
- 函数应该返回空对象还是空对象?
- 如何转换日期时间?将日期时间
- 如何在c#中连接列表?
- 在c#中引用类型变量的“ref”的用途是什么?
- 防止在ASP中缓存。NET MVC中使用属性的特定操作