views:

919

answers:

3

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?

+2  A: 

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
     }
}
Neil Whitaker
+2  A: 

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:

Marc

marc_s
"descendant class": the reason to use a partial method is to have it implemented in the *same* class, but a different partial file. If it's not implemented in the same class, it gets removed by the compiler.
itowlson
Is it possible for multiple partial classes to declare this method, or can it only be declared max once?
Baddie
@itowlson: of course, sorry, gotten partial methods and inheritance mixed up :-)
marc_s
@Unknown: it can be declared and have an implementation just once in all of the partial class files.
marc_s
+10  A: 

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, then can however accept ref parameters
  • they CANNOT be virtual or extern

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 */ }
 }
LBushkin
Actually they can accept ref parameters. They don't accept out.
Mircea Grelus
Yes thanks Mircea, just fixed that up.
LBushkin