C# 部分类构造函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1580509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Partial Class Constructors
提问by Omar
Is there a way to have a partial class' constructor call another method that my or may not be defined?
有没有办法让部分类的构造函数调用另一个我定义或可能未定义的方法?
Basically my partial class constructor is defined:
基本上我的部分类构造函数是定义的:
public partial class Test
{
public Test()
{
//do stuff
}
}
I would like to be able to somehow insert extra code to be run after the class constructor is called.
我希望能够以某种方式插入要在调用类构造函数后运行的额外代码。
In addition, is there a way to have more than one file to inject extra code after the constructor is called?
另外,有没有办法让多个文件在调用构造函数后注入额外的代码?
采纳答案by LBushkin
C# does support the feature of partial methods. These allow a partial class definition to forward declare a method that another part of the partial class can then optionally define.
C# 确实支持部分方法的特性。这些允许分部类定义转发声明一个方法,然后分部类的另一部分可以选择定义。
Partial methods have some restrictions:
部分方法有一些限制:
- they MUST be of void type (no return)
- they CANNOT accept out parameters, they can however accept ref parameters
- they CANNOT be virtual or extern and CANNOT override or overwrite another method (via "new" keyword)
- 它们必须是 void 类型(无返回)
- 他们不能接受 out 参数,但是他们可以接受 ref 参数
- 它们不能是 virtual 或 extern 并且不能覆盖或覆盖另一个方法(通过“new”关键字)
Partial methods are implicitly sealed and private.
部分方法是隐式密封和私有的。
It is not, however, possible, to have two different portions of a partial class implement the same partial method. Generally partial methods are used in code-generated partial classes as a way of allowing the non-generated part of extend or customize the behavior of the portion that is generated (or sometimes vice versa). If a partial method is declared but not implemented in any class part, the compiler will automatically eliminate any calls to it.
然而,让分部类的两个不同部分实现相同的分部方法是不可能的。通常在代码生成的部分类中使用部分方法作为允许非生成部分扩展或自定义生成部分的行为的方式(有时反之亦然)。如果声明了分部方法但未在任何类部分中实现,编译器将自动消除对它的任何调用。
Here's a code sample:
这是一个代码示例:
public partial class PartialTestClass
{
partial void DoSomething();
public PartialTestClass() { DoSomething(); }
}
public partial class PartialTestClass
{
partial void DoSomething() { /* code here */ }
}
回答by Neil
Search for "partial methods". They will do exactly what you want.
搜索“部分方法”。他们会做你想做的。
For example:
例如:
public partial class Test
{
public Test()
{
//do stuff
DoExtraStuff();
}
partial void DoExtraStuff();
}
public partial class Test // in some other file
{
partial void DoExtraStuff()
{
// do more stuff
}
}
回答by marc_s
Well, in C# 3.0 you can have what are called partial methods- method that can be called, even if they're not really there.
好吧,在 C# 3.0 中,您可以拥有所谓的部分方法- 可以调用的方法,即使它们实际上并不存在。
If they're not defined in any of the partial class files, the call to them will be removed by the .NET compiler/linker.
如果它们未在任何分部类文件中定义,则对它们的调用将被 .NET 编译器/链接器删除。
So you could define e.g. a Customer class:
所以你可以定义一个 Customer 类:
partial class Customer
{
string name;
public string Name
{
get
{
return name;
}
set
{
OnBeforeUpdateName();
OnUpdateName();
name = value;
OnAfterUpdateName();
}
}
partial void OnBeforeUpdateName();
partial void OnAfterUpdateName();
partial void OnUpdateName();
}
Those partial methods OnBeforeUpdateName() etc. will be called but if your none of the partial class files actually does implement anything for them, that call will be without any effect. Linq-to-SQL uses this big time for these kind of notification methods.
那些分部方法 OnBeforeUpdateName() 等将被调用,但如果您的分部类文件实际上没有为它们实现任何内容,则该调用将没有任何效果。Linq-to-SQL 将大量时间用于这些通知方法。
See those blog posts on partial methods:
请参阅有关部分方法的博客文章:
- http://geekswithblogs.net/sdorman/archive/2007/12/24/c-3.0---partial-methods.aspx
- http://blog.benhall.me.uk/2007/07/partial-methods-in-c-30-and-vbnet-90.html
- http://blogs.msdn.com/wesdyer/archive/2007/05/23/in-case-you-haven-t-heard.aspx
- http://www.danielmoth.com/Blog/2007/08/partial-methods.html
- http://geekswithblogs.net/sdorman/archive/2007/12/24/c-3.0---partial-methods.aspx
- http://blog.benhall.me.uk/2007/07/partial-methods-in-c-30-and-vbnet-90.html
- http://blogs.msdn.com/wesdyer/archive/2007/05/23/in-case-you-haven-t-heard.aspx
- http://www.danielmoth.com/Blog/2007/08/partial-methods.html
Marc
马克