Is it possible to split a method across two files in C#? I know partial methods are there, but they seemingly cannot do this.
Here's the scenario. I'm using an open-source library and need to add some customizations in one or more files. For example, I want to add two new fields to the class Employee
and then initialize them in the InitializeFields()
method of that class. Since the open-source project continues to evolve and new versions are released every now and then, I want to keep my customizations separate from the original project, to easily upgrade to newer versions of the library.
I have already split all the required classes into Orig.cs
and Custom.cs
using partial class
syntax, and have added my custom fields in Custom.cs
. Now the problem is that I have no way of splitting the InitializeFields()
method, so that my custom code goes into Custom.cs
file.
Please note that I cannot use inheritance to solve the problem. The open-source library would contain numerous references to Employee class and I cannot afford to change all of them.
When you do this, you're compiling the library yourself, right?
I understand you don't want to subclass Employee
, because then all the library code that does new Employee()
won't work. However, what if you rename Employee
to EmployeeBase
in Orig.cs, and provide the class Employee
in Custom.cs? That way when the library code is compiled, new Employee()
will reference your class, not the library one. Since you're compiling the library and your customizations in the same project, you can make this substitution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With