什么时候适合使用 C# 分部类?

发布于 2024-09-16 09:37:29 字数 46 浏览 5 评论 0原文

我想知道是否有人可以概述一下我为什么要使用它们以及我在此过程中会获得什么优势。

I was wondering if someone could give me an overview of why I would use them and what advantage I would gain in the process.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(23

乱了心跳 2024-09-23 09:37:29

分部类的最大用途是让代码生成器/设计者的工作更轻松。部分类允许生成器简单地发出它们需要发出的代码,而不必处理用户对文件的编辑。用户同样可以通过第二个部分类自由地用新成员注释该类。这为关注点分离提供了一个非常干净的框架。

更好的看待它的方法是看看设计者在部分类之前是如何运作的。 WinForms 设计者会吐出一个区域内的所有代码,并带有关于不修改代码的措辞强烈的注释。它必须插入各种启发式方法来查找生成的代码以供以后处理。现在,它可以简单地打开 Designer.cs 文件,并高度确信它只包含与设计器相关的代码。

The biggest use of partial classes is to make life easier for code generators / designers. Partial classes allow the generator to simply emit the code they need to emit and they do not have to deal with user edits to the file. Users are likewise free to annotate the class with new members by having a second partial class. This provides a very clean framework for separation of concerns.

A better way to look at it is to see how designers functioned before partial classes. The WinForms designer would spit out all of the code inside of a region with strongly worded comments about not modifying the code. It had to insert all sorts of heuristics to find the generated code for later processing. Now it can simply open the designer.cs file and have a high degree of confidence that it contains only code relevant to the designer.

玩世 2024-09-23 09:37:29

另一个用途是拆分不同接口的实现,例如:

partial class MyClass : IF3
{
    // main implementation of MyClass
}


partial class MyClass : IF1
{
    // implementation of IF1
}

partial class MyClass : IF2
{
    // implementation of IF2
}

Another use is to split the implementation of different interfaces, e.g:

partial class MyClass : IF3
{
    // main implementation of MyClass
}


partial class MyClass : IF1
{
    // implementation of IF1
}

partial class MyClass : IF2
{
    // implementation of IF2
}
恏ㄋ傷疤忘ㄋ疼 2024-09-23 09:37:29

除了其他答案......

我发现它们作为重构上帝类的踏脚石很有帮助。如果一个类具有多个职责(特别是如果它是一个非常大的代码文件),那么我发现为每个职责添加 1x 部分类作为组织和重构代码的第一步是有益的。

这很有帮助,因为它可以帮助使代码更具可读性,而不会实际影响执行行为。它还可以帮助识别职责何时容易重构或与其他方面紧密纠缠在一起。

然而,需要明确的是,这仍然是糟糕的代码,在开发结束时,您仍然希望每个类承担一个责任(不是每个部分类)。这只是一个踏脚石:)

Aside from the other answers...

I've found them helpful as a stepping-stone in refactoring god-classes. If a class has multiple responsibilities (especially if it's a very large code-file) then I find it beneficial to add 1x partial class per-responsibility as a first-pass for organizing and then refactoring the code.

This helps greatly because it can help with making the code much more readable without actually effecting the executing behavior. It also can help identify when a responsibility is easy to refactor out or is tightly tangled with other aspects.

However--to be clear--this is still bad code, at the end of development you still want one responsibility per-class (NOT per partial class). It's just a stepping-stone :)

暖阳 2024-09-23 09:37:29
  1. 多个开发人员 使用部分类多个开发人员可以处理同一个类
    容易地。
  2. 代码生成器 部分类主要由代码生成器用来保持
    不同的关注点分开
  3. 部分方法 使用部分类,您还可以定义部分方法,开发人员可以简单地定义该方法,而其他开发人员可以实现该方法。
  4. 仅部分方法声明即使代码仅使用方法声明进行编译,并且如果实现
    该方法的不存在,编译器可以安全地删除该部分
    代码,不会发生编译时错误。

    验证第 4 点。只需创建一个 winform 项目并在 Form1 构造函数后面包含此行,然后尝试编译代码

    partial void Ontest(string s);
    

这里是实现部分类时需要考虑的一些要点:-

  1. 在每个部分中使用partial关键字部分类。
  2. 分部类各部分的名称应该相同,但分部类各部分的源文件名可以不同。
  3. 分部类的所有部分都应位于同一名称空间中。
  4. 分部类的每个部分都应该位于同一个程序集或 DLL 中,换句话说,您不能在来自不同类库项目的源文件中创建分部类。
  5. 分部类的每个部分都必须具有相同的可访问性。 (即:私有、公共或受保护)
  6. 如果您继承分部类上的类或接口,那么该分部类的所有部分都会继承它。
  7. 如果部分类的一部分被密封,那么整个类将被密封。
  8. 如果分部类的一部分是抽象的,那么整个类将被视为抽象类。
  1. Multiple Developer Using Partial Classes multiple developer can work on the same class
    easily.
  2. Code Generator Partial classes are mainly used by code generator to keep
    different concerns separate
  3. 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.
  4. 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);
    

Here are some points to consider while implementing partial classes:-

  1. Use partial keyword in each part of partial class.
  2. 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.
  3. All parts of a partial class should be in the same namespace.
  4. 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.
  5. Each part of a partial class must have the same accessibility. (i.e: private, public or protected)
  6. If you inherit a class or interface on a partial class then it is inherited by all parts of that partial class.
  7. If a part of a partial class is sealed then the entire class will be sealed.
  8. If a part of partial class is abstract then the entire class will be considered an abstract class.
诗化ㄋ丶相逢 2024-09-23 09:37:29

一个很大的用途是将生成的代码与属于同一类的手写代码分开。

例如,由于 LINQ to SQL 使用部分类,您可以编写自己的某些功能(如多对多关系)的实现,并且当您重新生成代码时,这些自定义代码不会被覆盖。

WinForms 代码也是如此。 Designer 生成的所有代码都放在一个您通常不会触及的文件中。您手写的代码位于另一个文件中。这样,当您在 Designer 中更改某些内容时,您的更改就不会被取消。

One great use is separating generated code from hand-written code that belong in the same class.

For example since LINQ to SQL uses partial classes you can write your own implementation of certain pieces of functionality (like Many-to-Many relationships) and those pieces of custom code won't get overwritten when you re-generate the code.

The same goes for WinForms code. All the Designer generated code goes in one file that you generally don't touch. Your hand-written code goes in another file. That way, when you change something in Designer, your changes don't get blown away.

策马西风 2024-09-23 09:37:29

分部类跨越多个文件。

如何在 C# 类声明上使用分部修饰符?

使用部分类,您可以将一个类物理地分成多个文件。这通常由代码生成器完成。

示例

对于普通的 C# 类,您无法在同一项目的两个单独文件中声明一个类。但使用 partial 修饰符就可以了。

如果一个文件是经常编辑的,而另一个文件是机器生成的或很少编辑的,则此功能非常有用。

下面是一个需要说明的示例:

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。

如果删除 partial 修饰符,您将收到包含以下文本的错误:

[命名空间“<全局命名空间>”已包含“A”的定义]。

提示

要解决此问题,您可以使用 partial 关键字,或更改其中一个类名称。

C# 编译器如何处理分部类?

如果反汇编上述程序(使用 IL 反汇编器),您将看到文件 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 classes span multiple files.

How can you use the partial modifier on a C# class declaration?

With partial classes, you can physically separate a class into multiple files. This is often done by code generators.

Example

With normal C# classes, you cannot declare a class in two separate files in the same project. But with the partial modifier, you can.

This is useful if one file is commonly edited and the other is machine-generated or rarely edited.

Here's an example to clarify:

class Program
{
    static void Main()
    {
        A.A1();
        A.A2();
    }
}

Contents of file A1.cs: C#

using System;

partial class A
{
    public static void A1()
    {
        Console.WriteLine("A1");
    }
}

Contents of file A2.cs: C#

using System;

partial class A
{
    public static void A2()
    {
        Console.WriteLine("A2");
    }
}

Output:

A1
A2

Partial is required here.

If you remove the partial modifier, you will get an error containing this text:

[The namespace '<global namespace>' already contains a definition for 'A'].

Tip:

To fix this, you can either use the partial keyword, or change one of the class names.

How does the C# compiler deal with partial classes?

If you disassemble the above program (using IL Disassembler), you will see that the files A1.cs and A2.cs are eliminated. You will find that the class A is present.

Class A will contain the methods A1 and A2 in the same code block. The two classes were merged into one.

Compiled result of A1.cs and A2.cs: C#

internal class A
{
    // Methods
    public static void A1()
    {
        Console.WriteLine("A1");
    }

    public static void A2()
    {
        Console.WriteLine("A2");
    }
}

Summary

  • Partial classes can simplify certain C# programming situations.
  • They are often used in Visual Studio when creating Windows Forms/WPF programs.
  • The machine-generated C# code is separate.
  • Or You could find the whole description here.
不甘平庸 2024-09-23 09:37:29

确实,部分类用于自动代码生成,一种用途是维护一个可能有数千行代码的大型类文件。您永远不知道您的类最终可能有 10,000 行,并且您不想创建具有不同名称的新类。

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...
}

希望它有意义!

It is true that Partial Class is used in auto code generation, one use can be maintaining a large class file which might have thousand lines of code. You never know your class might end up with 10 thousand lines and you don't want to create a new class with different name.

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.

Another possible use could be that more than one developer can work on the same class as they are stored at different places. People might laugh but you never know it can be handful sometimes.

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...
}

Hope it makes sense!

假扮的天使 2024-09-23 09:37:29

在处理大型类或在团队中工作时,尽可能保持所有内容干净,您可以在不覆盖的情况下进行编辑(或始终提交更改)

keep everything as clean as possible when working with huge classes, or when working on a team, you can edit without overriding (or always commiting changes)

千纸鹤 2024-09-23 09:37:29

分部类的主要用途是生成代码。如果您查看 WPF (Windows Presentation Foundation) 网络,您可以使用标记 (XML) 定义您的 UI。该标记被编译成部分类。您用自己的部分类填充代码。

The main use for partial classes is with generated code. If you look at the WPF (Windows Presentation Foundation) network, you define your UI with markup (XML). That markup is compiled into partial classes. You fill in code with partial classes of your own.

许你一世情深 2024-09-23 09:37:29

作为预编译器指令的替代方案。

如果您使用预编译器指令(即#IF DEBUG),那么您最终会得到一些看起来粗糙的代码与实际的发布代码混合在一起。

您可以创建一个单独的分部类来包含此代码,并将整个分部类包装在指令中,或者忽略将该代码文件发送到编译器(实际上执行相同的操作)。

As an alternative to pre-compiler directives.

If you use pre-compiler directives (namely #IF DEBUG) then you end up with some gnarly looking code intermingled with your actual Release code.

You can create a seperate partial-class to contain this code, and either wrap the entire partial class in a directive, or omit that code-file from being sent to the compiler (effectively doing the same).

暮倦 2024-09-23 09:37:29

大多数人认为 partial 只能用于具有生成代码文件的类或接口。我不同意,这就是原因。

举个例子,让我们看一下 C# System.Math 类...它是。我不会尝试将 70 多个方法全部填充到同一个代码文件中。维护起来将是一场噩梦。

将每个数学方法放入单独的部分类文件中,并将所有代码文件放入项目中的 Math 文件夹中,这样的组织会更加清晰。

对于具有大量不同功能的许多其他类来说,同样的情况也可能/将会成立。例如,用于管理 PrivateProfile API 的类可以通过在单个项目文件夹中拆分为一组干净的部分类文件来受益。

就我个人而言,我还将大多数人所说的“帮助程序”或“实用程序”类拆分为每个方法或方法功能组的单独部分文件。例如,在一个项目中,字符串辅助类有近 50 个方法。即使使用区域,这也将是一个又长又笨重的代码文件。对于每个方法使用单独的部分类文件,维护起来要容易得多。

执行此操作时,我会小心使用部分类并在整个项目中保持所有代码文件布局一致。例如,将任何类公共枚举和类私有成员放入文件夹中的 Common.cs 或类似命名的文件中,而不是将它们分布在文件中,除非它们仅特定于它们所包含的部分文件。

请记住当您将一个类拆分为单独的文件时,您也无法使用文本编辑器拆分栏,该拆分栏可让您同时查看当前文件的两个不同部分。

Most people remark that partial should only be used for a class that has a generated code file or for interfaces. I disagree, and here is why.

For one example, let's look at the C# System.Math class... that's class. I would not attempt to stuff 70+ methods all into the same single code file. It would be a nightmare to maintain.

Placing each math method into individual partial class files, and all code files into a Math folder in the project, would be significantly cleaner organization.

The same could/would hold true for many other classes that have a large amount of diverse functionality. For example a class for managing the PrivateProfile API might benefit by being split into a clean set of partial class files in a single project folder.

Personally, I also split what most people call "helper" or "utility" classes into individual partial files for each method or method functional group. For example on one project the string helper class has almost 50 methods. That would be a long unwieldy code file even using regions. It is significantly easier to maintain using individual partial class files for each method.

I would just be careful using partial classes and keep all code file layout consistent throughout the project when doing this. Such as placing any class public enums and class private members into a Common.cs or similarly named file in the folder, instead of spreading them out across the files unless they are specific to only the partial file they are contained in.

Keep in mind that when you split a class into separate files you also lose the ability to use the text editor splitter bar that lets you view two different sections of a current file simultaneously.

π浅易 2024-09-23 09:37:29

如果您有一个足够大的类,不适合进行有效的重构,那么将其分成多个文件有助于保持事物的组织性。

例如,如果您有一个包含讨论论坛和产品系统的站点数据库,并且您不想创建两个不同的提供程序类(与代理类不同,只是为了清楚起见),您可以在不同的文件中创建单个分部类,例如

MyProvider.cs - 核心逻辑

MyProvider.Forum.cs - 专门与论坛相关的方法

MyProvider.Product.cs - 产品的方法

这只是保持事物井然有序的另一种方式。

此外,正如其他人所说,这是向生成的类添加方法的唯一方法,而不会冒下次重新生成类时添加的内容被破坏的风险。这对于模板生成的 (T4) 代码、ORM 等非常有用。

If you have a sufficiently large class that doesn't lend itself to effective refactoring, separating it into multiple files helps keep things organized.

For instance, if you have a database for a site containing a discussion forum and a products system, and you don't want to create two different providers classes (NOT the same thing as a proxy class, just to be clear), you can create a single partial class in different files, like

MyProvider.cs - core logic

MyProvider.Forum.cs - methods pertaining specifically to the forum

MyProvider.Product.cs - methods for products

It's just another way to keep things organized.

Also, as others have said, it's about the only way to add methods to a generated class without running the risk of having your additions destroyed the next time the class is regenerated. This comes in handy with template-generated (T4) code, ORMs, etc.

峩卟喜欢 2024-09-23 09:37:29

我看到的另一个用途是,

扩展一个关于数据访问逻辑的大抽象类,

我有各种名称为 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..
}

Another use i saw is,

Extending a big abstract class regarding data access logic ,

i have various files with names 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..
}
古镇旧梦 2024-09-23 09:37:29

服务引用是另一个示例,其中部分类可用于将生成的代码与用户创建的代码分开。

您可以“扩展”服务类,而无需在更新服务引用时覆盖它们。

Service references are another example where partial classes are useful to separate generated code from user-created code.

You can "extend" the service classes without having them overwritten when you update the service reference.

蔚蓝源自深海 2024-09-23 09:37:29

部分类使得仅通过添加源文件就可以向适当设计的程序添加功能。例如,可以设计一个文件导入程序,以便可以通过添加处理不同类型的已知文件的模块来添加它们。例如,主文件类型转换器可以包含一个小类:

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。

编辑/附录
需要明确的是,这样做的目的是提供一种方法,使各种单独的类可以让主程序或类了解它们。主文件转换器对 zzFileConverterRegistrar 所做的唯一一件事是创建它的一个实例并调用 registerAll 方法,该方法将触发 Register 事件。任何想要挂钩该事件的模块都可以执行任意代码来响应它(这就是整个想法),但是除了定义一个名称与其他名称匹配的方法之外,模块无法通过不正确地扩展 zzFileConverterRegistrar 类来执行任何操作。一个编写不正确的扩展当然有可能破坏另一个编写不正确的扩展,但解决方案是任何不希望其扩展被破坏的人都可以简单地正确编写它。

在不使用部分类的情况下,可以在主文件转换器类中的某个位置添加一些代码,如下所示:

  RegisterConverter("GIF", GifConvertor.NewFactory)
  RegisterConverter("BMP", BmpConvertor.NewFactory)
  RegisterConverter("JPEG", JpegConvertor.NewFactory)

但是添加另一个转换器模块将需要进入转换器代码的该部分并将新转换器添加到列表中。使用部分方法,这不再是必要的——所有转换器都将自动包含在内。

Partial classes make it possible to add functionality to a suitably-designed program merely by adding source files. For example, a file-import program could be designed so that one could add different types of known files by adding modules that handle them. For example, the main file type converter could include a small class:

Partial Public Class zzFileConverterRegistrar
    Event Register(ByVal mainConverter as zzFileConverter)
    Sub registerAll(ByVal mainConverter as zzFileConverter)
        RaiseEvent Register(mainConverter)
    End Sub
End Class

Each module that wishes to register one or more types of file converter could include something like:

Partial Public Class zzFileConverterRegistrar
    Private Sub RegisterGif(ByVal mainConverter as zzFileConverter) Handles Me.Register
        mainConverter.RegisterConverter("GIF", GifConverter.NewFactory))
    End Sub
End Class

Note that the main file converter class isn't "exposed"--it just exposes a little stub class that add-in modules can hook to. There is a slight risk of naming conflicts, but if each add-in module's "register" routine is named according to the type of file it deals with, they probably shouldn't pose a problem. One could stick a GUID in the name of the registration subroutine if one were worried about such things.

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.

One could, without using partial classes, have a bit of code somewhere within the main file converter class, which looked like:

  RegisterConverter("GIF", GifConvertor.NewFactory)
  RegisterConverter("BMP", BmpConvertor.NewFactory)
  RegisterConverter("JPEG", JpegConvertor.NewFactory)

but adding another converter module would require going into that part of the converter code and adding the new converter to the list. Using partial methods, that is no longer necessary--all converters will get included automatically.

轮廓§ 2024-09-23 09:37:29

部分类最近有助于源代码控制,其中多个开发人员添加到一个文件,其中新方法被添加到文件的同一部分(由 Resharper 自动执行)。

这些对 git 的推送导致了合并冲突。我找不到方法告诉合并工具将新方法作为完整的代码块。

在这方面,部分类允许开发人员坚持使用其文件的某个版本,并且我们可以稍后手动将它们合并回来。

示例 -

  • MainClass.cs - 保存字段、构造函数等
  • MainClass1.cs - 开发人员在实现
  • MainClass2.cs 时的新代码 - 是用于新代码的另一个开发人员类。

Partial classes recently helped with source control where multiple developers were adding to one file where new methods were added into the same part of the file (automated by Resharper).

These pushes to git caused merge conflicts. I found no way to tell the merge tool to take the new methods as a complete code block.

Partial classes in this respect allows for developers to stick to a version of their file, and we can merge them back in later by hand.

example -

  • MainClass.cs - holds fields, constructor, etc
  • MainClass1.cs - a developers new code as they implement
  • MainClass2.cs - is another developers class for their new code.
相对绾红妆 2024-09-23 09:37:29

来自 MSDN

1.在编译时,部分类型定义的属性被合并。例如,考虑以下声明:

[SerializableAttribute]
partial class Moon { }

[ObsoleteAttribute]
partial class Moon { }

它们等效于以下声明:

[SerializableAttribute]
[ObsoleteAttribute]
class Moon { }

以下是从所有部分类型定义合并而来的:

  • XML 注释

  • 接口

  • 泛型类型参数属性

  • 类属性

  • 成员

2.另一件事,嵌套的部分类也可以是部分的:

partial class ClassWithNestedClass
{
    partial class NestedClass { }
}

partial class ClassWithNestedClass
{
    partial class NestedClass { }
}

From MSDN:

1.At compile time, attributes of partial-type definitions are merged. For example, consider the following declarations:

[SerializableAttribute]
partial class Moon { }

[ObsoleteAttribute]
partial class Moon { }

They are equivalent to the following declarations:

[SerializableAttribute]
[ObsoleteAttribute]
class Moon { }

The following are merged from all the partial-type definitions:

  • XML comments

  • interfaces

  • generic-type parameter attributes

  • class attributes

  • members

2.Another thing, nested partial classes can be also partial:

partial class ClassWithNestedClass
{
    partial class NestedClass { }
}

partial class ClassWithNestedClass
{
    partial class NestedClass { }
}
落花浅忆 2024-09-23 09:37:29

以下列出了部分类的一些优点。

您可以将UI设计代码和业务逻辑代码分开,以便于阅读和理解。例如,您正在使用 Visual Studio 开发一个 Web 应用程序并添加一个新的 Web 表单,然后有两个源文件“aspx.cs”和“aspx.designer.cs”。这两个文件具有相同的类,并带有partial关键字。 “.aspx.cs”类具有业务逻辑代码,而“aspx.designer.cs”具有用户界面控件定义。

使用自动生成的源时,可以将代码添加到类中,而无需重新创建源文件。例如,您正在使用 LINQ to SQL 并创建一个 DBML 文件。现在,当您拖放表格时,它会在 Designer.cs 中创建一个分部类,并且所有表格列都在该类中具有属性。您需要此表中的更多列来绑定到 UI 网格,但您不想向数据库表添加新列,因此您可以为此类创建一个单独的源文件,该文件具有该列的新属性,并且它将成为一个部分类。因此,这确实会影响数据库表和 DBML 实体之间的映射,但您可以轻松获得额外的字段。这意味着您可以自己编写代码,而不会弄乱系统生成的代码。

多个开发人员可以同时为该类编写代码。

您可以通过压缩大型类来更好地维护您的应用程序。假设您有一个具有多个接口的类,因此您可以根据接口实现创建多个源文件。源文件具有分部类的接口很容易理解和维护。

Here is a list of some of the advantages of partial classes.

You can separate UI design code and business logic code so that it is easy to read and understand. For example you are developing an web application using Visual Studio and add a new web form then there are two source files, "aspx.cs" and "aspx.designer.cs" . These two files have the same class with the partial keyword. The ".aspx.cs" class has the business logic code while "aspx.designer.cs" has user interface control definition.

When working with automatically generated source, the code can be added to the class without having to recreate the source file. For example you are working with LINQ to SQL and create a DBML file. Now when you drag and drop a table it creates a partial class in designer.cs and all table columns have properties in the class. You need more columns in this table to bind on the UI grid but you don't want to add a new column to the database table so you can create a separate source file for this class that has a new property for that column and it will be a partial class. So that does affect the mapping between database table and DBML entity but you can easily get an extra field. It means you can write the code on your own without messing with the system generated code.

More than one developer can simultaneously write the code for the class.

You can maintain your application better by compacting large classes. Suppose you have a class that has multiple interfaces so you can create multiple source files depending on interface implements. It is easy to understand and maintain an interface implemented on which the source file has a partial class.

苍景流年 2024-09-23 09:37:29

我觉得令人不安的是,“凝聚力”这个词没有出现在这些帖子中的任何地方(直到现在)。
我还感到不安的是,有人认为启用或鼓励庞大的课程和方法在某种程度上是一件好事。
如果您试图理解和维护代码库,那么“部分”很糟糕。

I find it disturbing that the word 'cohesion' does not appear anywhere in these posts (until now).
And I'm also disturbed that anyone thinks enabling or encouraging huge classes and methods is somehow a good thing.
If you're trying to understand and maintain a code-base 'partial' sucks.

宁愿没拥抱 2024-09-23 09:37:29

每当我有一个包含具有任何显着大小/复杂性的嵌套类的类时,我都会将该类标记为 partial 并将嵌套类放入单独的文件中。我使用以下规则命名包含嵌套类的文件:[类名].[嵌套类名].cs。

以下 MSDN 博客解释了使用部分类和嵌套类来实现可维护性: http://blogs.msdn.com/b/marcelolr/archive/2009/04/13/using-partial-classes-with-nested-classes-for -可维护性.aspx

Whenever I have a class that contains a nested class that is of any significant size/complexity, I mark the class as partial and put the nested class in a separate file. I name the file containing the nested class using the rule: [class name].[nested class name].cs.

The following MSDN blog explains using partial classes with nested classes for maintainability: http://blogs.msdn.com/b/marcelolr/archive/2009/04/13/using-partial-classes-with-nested-classes-for-maintainability.aspx

七婞 2024-09-23 09:37:29

我知道这个问题确实很老,但我只想添加我对部分课程的看法。

我个人使用分部类的原因之一是当我为程序(尤其是状态机)创建绑定时。

例如,OpenGL是一个状态机,有的方法都可以全局更改,但是,根据我的经验,绑定类似于OpenGL的东西,其中有很多方法,该类很容易超过10k 位置。

部分类将为我分解这个帮助我快速找到方法。

I know this question is really old but I would just like to add my take on partial classes.

One reason that I personally use partial classes is when I'm creating bindings for a program, especially state machines.

For example, OpenGL is a state machine, there are heaps of methods that can all be changed globally, however, in my experience binding something similar to OpenGL where there are so many methods, the class can easily exceed 10k LOC.

Partial classes will break this down for me and help me with finding methods quickly.

强者自强 2024-09-23 09:37:29

我注意到我在答案中找不到明确的两种用法。

对类项进行分组

一些开发人员使用注释来分隔其类的不同“部分”。例如,团队可能会使用以下约定:

public class MyClass{  
  //Member variables
  //Constructors
  //Properties
  //Methods
}

对于部分类,我们可以更进一步,将各个部分拆分为单独的文件。作为惯例,团队可能会在每个文件后面添加与其对应的部分的后缀。所以在上面我们会有这样的东西:MyClassMembers.cs、MyClassConstructors.cs、MyClassProperties.cs、MyClassMethods.cs。

正如其他答案所提到的,是否值得将班级分开可能取决于本例中班级的规模。如果规模很小,在一堂大师课上包含所有内容可能会更容易。但是,如果这些部分中的任何一个变得太大,则可以将其内容移动到单独的部分类中,以保持主类的整洁。在这种情况下,惯例可能是在部分标题后留下注释,例如“查看部分类”,例如:

//Methods - See partial class

管理使用语句/命名空间的范围

这可能很少发生,但两个函数之间可能会发生命名空间冲突来自您要使用的库。在单个类中,您最多可以对其中之一使用 using 子句。对于另一个,您需要一个完全限定的名称或别名。对于部分类,因为每个命名空间 & using 语句列表不同,可以将两组函数分成两个单独的文件。

I note two usages which I couldn't find explicitly in the answers.

Grouping Class Items

Some developers use comments to separate different "parts" of their class. For example, a team might use the following convention:

public class MyClass{  
  //Member variables
  //Constructors
  //Properties
  //Methods
}

With partial classes, we can go a step further, and split the sections into separate files. As a convention, a team might suffix each file with the section corresponding to it. So in the above we would have something like: MyClassMembers.cs, MyClassConstructors.cs, MyClassProperties.cs, MyClassMethods.cs.

As other answers alluded to, whether or not it's worth splitting the class up probably depends on how big the class is in this case. If it's small, it's probably easier to have everything in one master class. But if any of those sections get too big, its content can be moved to a separate partial class, in order to keep the master class neat. A convention in that case might be to leave a comment in saying something like "See partial class" after the section heading e.g.:

//Methods - See partial class

Managing Scope of Using statements / Namespace

This is probably a rare occurrence, but there might be a namespace collision between two functions from libraries that you want to use. In a single class, you could at most use a using clause for one of these. For the other you'd need a fully qualified name or an alias. With partial classes, since each namespace & using statements list is different, one could separate the two sets of functions into two separate files.

以为你会在 2024-09-23 09:37:29

引入部分类主要是为了帮助代码生成器,因此我们(用户)最终不会在每次重新生成时丢失对生成的类(如 ASP.NET 的 .designer.cs 类)所做的所有工作/更改,几乎所有生成的新工具代码 LINQ、EntityFrameworks、ASP.NET 使用分部类来生成代码,因此我们可以利用分部类和方法安全地添加或更改这些生成代码的逻辑,但在使用分部类向生成的代码添加内容之前要非常小心如果我们破坏构建,情况会更容易,但如果引入运行时错误,情况会更糟。有关更多详细信息,请查看此 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文