Is there a way to override an existing method in a partial class?
Something like this (I know this doesn’t compile)
/// <summary>
/// Generated Code for MyClass
/// </summary>
public partial class MyClass
{
public void MyMethod()
{
//Do work specific to MyClass
}
}
/// <summary>
/// Non-generated extension for MyClass
/// </summary>
public partial class MyClass
{
public override void MyMethod()
{
//Do some other work
}
}
I am running into this need while doing some code generation. I am generating partial classes from my EDMX and methods for these classes where 90% of my classes will have methods specific to their properties.
But I am running into cases where I would like the one-off the methods.
Is there something like this I am overlooking? Or some suggested alternatives?
You can use a partial method for this. I assume you can extrapolate here if you need to make changes to the signature, etc.
It’s also worth noting that if you define a partial method and nobody defines an implementation for it in another partial bit of the class it just turns into a no-op, not an error, so you don’t need to worry about that causing problems. It was literally built for just this use case.