如何使用分部方法C#3.0?

如何使用分部方法C#3.0?

问题描述:

我看了一下最新 C#语言规范的部分方法,让我明白了的原则,但我不知道人们是如何实际使用它们。是否有利于从局部方法特定的设计模式?

I have read about partial methods in the latest C# language specification, so I understand the principles, but I'm wondering how people are actually using them. Is there a particular design pattern that benefits from partial methods?

部分方法目前已经提出了类似的理由,为什么部分类在.NET 2。

Partial methods have been introduced for similar reasons to why partial classes were in .Net 2.

一个局部类是一个可以跨多个文件进行分割 - 编译器生成它们合并成一个文件,因为它运行

A partial class is one that can be split across multiple files - the compiler builds them all into one file as it runs.

有这样的好处是Visual Studio可以为类,而在其他codeRS工作的一部分提供一个图形设计

The advantage for this is that Visual Studio can provide a graphical designer for part of the class while coders work on the other.

最常见的例子是窗体设计器。开发者不希望被定位按钮,输入框等通过手的大部分时间。

The most common example is the Form designer. Developers don't want to be positioning buttons, input boxes, etc by hand most of the time.

  • 在净1这是自动生成的code在#地区
  • 在净2这些成为独立的设计器类 - 的形式仍然是一个类,它只是分成了表单设计由开发商编辑一个文件和一个

这使得维护双方更加容易。合并更简单,有在VS窗体设计器的风险更小意外撤消codeRS'手动更改。

This makes maintaining both much easier. Merges are simpler and there's less risk of the VS form designer accidentally undoing coders' manual changes.

在.NET 3.5中的LINQ已经出台。 LINQ有一个DBML设计师构建的数据结构,并产生自动code。

In .Net 3.5 Linq has been introduced. Linq has a DBML designer for building your data structures, and that generates auto-code.

这里的额外位是需要提供开发人员可能需要填充的方法是code。

The extra bit here is that code needed to provide methods that developers might want to fill in.

由于开发商将延长这些类(额外部分的文件),他们不能在这里使用的抽象方法。

As developers will extend these classes (with extra partial files) they couldn't use abstract methods here.

另一个问题是,大多数情况下,这些方法不会被调用,调用空方法是在浪费时间。

The other issue is that most of the time these methods wont be called, and calling empty methods is a waste of time.

空方法不优化掉

所以LINQ的生成空部分的方法。如果你没有创建自己的部分来完成他们的C#编译器将只对其进行优化的。

So Linq generates empty partial methods. If you don't create your own partial to complete them the C# compiler will just optimise them out.

所以,它可以做到这一点分部方法始终返回void。

So that it can do this partial methods always return void.

如果您创建一个新的LINQ DBML文件,它会自动生成一个部分类,像

If you create a new Linq DBML file it will auto-generate a partial class, something like

[System.Data.Linq.Mapping.DatabaseAttribute(Name="MyDB")]
public partial class MyDataContext : System.Data.Linq.DataContext
{
    ...

    partial void OnCreated();
    partial void InsertMyTable(MyTable instance);
    partial void UpdateMyTable(MyTable instance);
    partial void DeleteMyTable(MyTable instance);

    ...

然后在你自己的部分文件,你可以扩展这样的:

Then in your own partial file you can extend this:

public partial class MyDataContext
{
    partial void OnCreated() {
        //do something on data context creation
    }
}

如果您不扩展这些方法,他们得到优化右出。

If you don't extend these methods they get optimised right out.

分部方法不能是公共 - 因为那么他们必须在那里为其他类调用。如果你编写自己的code发电机我可以看到他们是有用的,但除此之外,他们只为VS设计师真的很有用。

Partial methods can't be public - as then they'd have to be there for other classes to call. If you write your own code generators I can see them being useful, but otherwise they're only really useful for the VS designer.

我在前面提到的例子是一种可能:

The example I mentioned before is one possibility:

//this code will get optimised out if no body is implemented
partial void DoSomethingIfCompFlag();

#if COMPILER_FLAG
//this code won't exist if the flag is off
partial void DoSomethingIfCompFlag() {
    //your code
}
#endif

另一个潜在用途是,如果你有一个庞大而复杂的类在多个文件洒你可能想在调用文件部分参考。不过,我认为在这种情况下,你应该首先考虑简化的类。

Another potential use is if you had a large and complex class spilt across multiple files you might want partial references in the calling file. However I think in that case you should consider simplifying the class first.