40

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?

AndreyAkinshin
  • 18,603
  • 29
  • 96
  • 155
Omar
  • 39,496
  • 45
  • 145
  • 213

3 Answers3

60

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 */ }
 }
Community
  • 1
  • 1
LBushkin
  • 129,300
  • 32
  • 216
  • 265
  • 4
    Actually they can accept ref parameters. They don't accept out. – Mircea Grelus Oct 16 '09 at 21:35
  • 9
    Unfortunately you cannot assign values to `readonly` fields from within these partial methods, so technically there are no partial constructors even if you decide to only call them from the constructor. – C.Evenhuis Mar 31 '14 at 08:33
  • This answer is not correct. A partial method absolutely CAN have a return type other than void, accept out parameters, or be virtual, override, sealed, new, or extern. What CANNOT be the case is for those things to be true and ALSO not provide an implementation for the partial method. You also missed that you cannot add access modifiers if you do not plan to supply an implementation. – gamesaucer Mar 11 '23 at 13:25
9

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
  • 7,227
  • 5
  • 42
  • 43
9

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

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    "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 Oct 16 '09 at 21:31
  • Is it possible for multiple partial classes to declare this method, or can it only be declared max once? – Omar Oct 16 '09 at 21:31
  • @itowlson: of course, sorry, gotten partial methods and inheritance mixed up :-) – marc_s Oct 16 '09 at 21:33
  • @Unknown: it can be declared and have an implementation just once in all of the partial class files. – marc_s Oct 16 '09 at 21:59