我想知道是否有人能给我一个概述,为什么我要使用它们,在这个过程中我能获得什么好处。
当前回答
部分类的最大用途是使代码生成器/设计人员的工作更轻松。部分类允许生成器简单地发出它们需要发出的代码,而不需要处理用户对文件的编辑。用户同样可以通过创建第二个分部类来自由地用新成员注释类。这为关注点分离提供了一个非常清晰的框架。
更好的方法是看看设计器在部分类之前是如何工作的。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.
我发现令人不安的是,“凝聚力”这个词在这些帖子中没有出现(直到现在)。 我也很不安,有人认为启用或鼓励大型类和方法在某种程度上是件好事。 如果你试图理解和维护代码基础的“部分”,那就糟透了。
的确,Partial Class用于自动代码生成,其中一个用途是维护一个可能有上千行代码的大型类文件。你永远不知道你的类最终会有1万行,你不想用不同的名字创建一个新类。
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.
另一个可能的用途是多个开发人员可以使用同一个类,因为它们存储在不同的地方。人们可能会笑,但你永远不知道有时候情况会很棘手。
Product1.cs
public partial class Product
{
//you are writing the business logic for fast moving product
}
Product2.cs
public partial class Product
{
// Another developer writing some business logic...
}
希望这是有意义的!
作为预编译器指令的替代方案。
如果您使用预编译器指令(即# If DEBUG),那么您最终会看到一些看起来粗糙的代码与实际的Release代码混合在一起。
你可以创建一个单独的分部类来包含这些代码,或者将整个分部类包装在一个指令中,或者省略该代码文件,使其不被发送给编译器(实际上是做同样的事情)。
分部类跨越多个文件。
如何在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#代码是独立的。 或者你可以在这里找到完整的描述。
推荐文章
- 如何使用JSON确保字符串是有效的JSON。网
- AppSettings从.config文件中获取值
- 通过HttpClient向REST API发布一个空体
- 如何检查IEnumerable是否为空或空?
- 自动化invokerrequired代码模式
- 在c#代码中设置WPF文本框的背景颜色
- 在c#中,什么是单子?
- c#和Java中的泛型有什么不同?和模板在c++ ?
- c#线程安全快速(est)计数器
- 如何将此foreach代码转换为Parallel.ForEach?
- 如何分裂()一个分隔字符串到一个列表<字符串>
- 如何转换列表<字符串>列表<int>?
- c#对象列表,我如何得到一个属性的和
- 我如何使用IValidatableObject?
- 如何指定最小值,但没有使用范围数据注释属性的最大小数?