C#3.0中如何使用部分方法? [英] How are partial methods used in C# 3.0?

查看:126
本文介绍了C#3.0中如何使用部分方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在最新的 C#语言规范中阅读了部分方法所以我明白原则,但我想知道人们是如何实际使用它们的。有一些特殊的设计模式可以从部分方法中受益吗?

解决方案

部分方法是为了类似的原因引入的,在.Net 2中。



部分类是可跨多个文件分割的类 - 编译器在运行时将它们全部构建到一个文件中。



这个优点是,Visual Studio可以为编程人员在另一个工作时为部分课程提供图形设计师。



最常见的例子是Form设计器。大多数时候开发人员不想手工定位按钮,输入框等。




  • 在.Net 1中,它是自动的在$ code> #region 块中的代码代码

  • 在.Net 2中,这些代码成为单独的设计师类 - 表单仍然是一个类,它是只需拆分为一个由开发人员编辑的文件,一个由表单设计器分解。


这使得维护更容易。合并更简单,VS表单设计师的风险就意味着意外撤消了编程人员的手动更改。



In .Net 3.5 Linq已经推出。 Linq有一个用于构建数据结构的DBML设计器,并生成自动代码。



此处的额外位置是提供开发人员可能想要填充的方法所需的代码在



由于开发人员将扩展这些类(具有额外的部分文件),因此无法在此使用抽象方法。



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



空方法未优化



所以Linq生成空的部分方法。如果你不创建自己的部分来完成它们,C#编译器将只是优化它们。



所以它可以做这个部分方法总是返回void。 p>

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

  [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);

...

然后在您自己的部分文件中,您可以扩展这个:

  public partial class MyDataContext 
{
partial void OnCreated(){
//在数据上下文创作上做某事
}
}

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



部分方法不能公开 - 因为他们必须在其他类调用。如果你编写自己的代码生成器,我可以看到它们是有用的,但是否则它们对于VS设计师来说真的很有用。



我之前提到的例子是一种可能性:

  //如果没有实体,这段代码将被优化
partial void DoSomethingIfCompFlag();

#if COMPILER_FLAG
//如果标志为off,则此代码将不存在
partial void DoSomethingIfCompFlag(){
//您的代码
}
#endif

另一个潜在的用处是如果你有一个庞大和复杂的类溢出多个文件可能需要调用文件中的部分引用。但是我认为在这种情况下,您应该考虑简化课程。


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?

解决方案

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.

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.

  • In .Net 1 it was auto-generated code in a #region block
  • In .Net 2 these became separate designer classes - the form is still one class, it's just split into one file edited by the developers and one by the form designer

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

In .Net 3.5 Linq has been introduced. Linq has a DBML designer for building your data structures, and that generates auto-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.

Empty methods are not optimised out.

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.

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

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.

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.

这篇关于C#3.0中如何使用部分方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆