Create partial method in C #

1

How can I create a method similar to a class that is partial?

For example:

It has method $InitializeComponent() that is of the class of a form.

I want to increment this function, without touching the original (create a proxy ). How can I do this?

What I tried and not its right was:

partial class form1
{
    ...
    private void InitializeComponent()
    {
        ...
    }
    ...
}

partial class form1 : Form
{
     ...
     partial void InitializeComponent()
     {
           ...
     }
}
    
asked by anonymous 17.12.2014 / 15:24

1 answer

1

Yes, this is actually called partial method . But maybe you will not do what you want. You can not write a part of the method code in one place and one part in another. What you can do is declare the method in one part and set its code elsewhere. As it is in C / C ++.

I find it unnecessary to actually do this but if you really want to, if you think you have a real reason to do, what you can do is create one main method and call another helper that is in the other part. Partial method was created to make it easier to use with code generators, to generate the declaration and ensure that the programmer will create the implementation in another file.

Method has algorithm, it is different from a class that is a data structure. In the data structure the position where each element is is not very important. The compiler manages to do well with this.

In an algorithm this part of the code will go where? The compiler has no way of knowing which one goes first, or worse, if it's to get in the way. Only the programmer can tell you how this will be done. So just make a call to another method in the appropriate place.

If you really want to make a proxy, do a proxy .

I still think you're trying to make a play that either is not possible or will not go well.

File1

partial class form1 {
    ...
    private void InitializeComponent() {
        ...
        InitializeComponentAux();
    }
    ...
}

File2

partial class form1 : Form {
     ...
     partial void InitializeComponentAux() {
           ...
     }
}

With the official partial method would only do this:

File1

partial class form1 {
    ...
    private void InitializeComponent();
    ...
}

File2

partial class form1 : Form {
     ...
     partial void InitializeComponent() {
           ...
     }
}
    
17.12.2014 / 15:44