Partial Class Constructors
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?
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.
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)
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 */ }
}
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
}
}
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.
If they're not defined in any of the partial class files, the call to them will be removed by the .NET compiler/linker.
So you could define e.g. a Customer class:
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.
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
Marc
精彩评论