Override a virtual method in a partial class

2019-02-22 01:43发布

问题:

I am currently working with the nopCommerce source code and trying my best to avoid editing the source at all, but instead using partial classes and plugins that are separate from the source code, should we ever need to upgrade versions.

I want to make some changes to the code that places an order, by using a partial class in the same assembly:

Orignal Source Code:

namespace Nop.Services.Orders {

  public partial class OrderProcessingService : IOrderProcessingService {

        public virtual PlaceOrderResult PlaceOrder(ProcessPaymentRequest processPaymentRequest)
        { //....

My partial class:

namespace Nop.Services.Orders {

  public partial class OrderProcessingService : IOrderProcessingService {

    public override PlaceOrderResult PlaceOrder(ProcessPaymentRequest processPaymentRequest) { //....

When I try to compile this code I get an error:

Type 'Nop.Services.Orders.OrderProcessingService' already defines a member called 'PlaceOrder' with the same parameter types

But I am using override and the method in the original class is virtual, could someone tell me where I am going wrong here, and how I could override this method?

回答1:

You cannot override a virtual method on the same class. Partial classes are just the same class with definition splitted on different places, it doesn't define a hierarchy so that's just not possible

It is possible to split the definition of a class or a struct, or an interface over two or more source files. Each source file contains a section of the class definition, and all parts are combined when the application is compiled

You should create a inherited class to achieve your goal

public class MyOrderProcessingService : OrderProcessingService
{
    public override PlaceOrderResult PlaceOrder(ProcessPaymentRequest processPaymentRequest) { //....
}


回答2:

Not entirely clear what you want, but maybe you can use a partial method?

So the "party" responsible for the first "part" of the class can give the signature alone in:

"Orignal Source Code":

namespace Nop.Services.Orders {

  public partial class OrderProcessingService : IOrderProcessingService {

    partial void PlaceOrder(ProcessPaymentRequest processPaymentRequest,
      ref PlaceOrderResult result);

Then the "party" responsible for the other "part" may or may not choose to supply an implementation of the method. If they supply one, that will look like:

"My partial class":

namespace Nop.Services.Orders {

  public partial class OrderProcessingService : IOrderProcessingService {

    partial void PlaceOrder(ProcessPaymentRequest processPaymentRequest,
      ref PlaceOrderResult result) {

      // actual code goes here
    }

Things to note:

  • the partial method cannot be public; it must be private and it is illegal to even specify the private keyword. And a private method is not allowed to be virtual.
  • the partial method must return void and cannot have parameters marked with out, so we put the result as a ref parameter
  • the declaration in the first "part" has a semicolon ; instead of a body
  • the implementation in the other "part", if it exists, has a method body { ... }

Now in "Orignal Source Code" the method can be called like this:

// ...
PlaceOrderResult result = null;
PlaceOrder(someRequest, ref result);
// check if 'result' was changed to something non-null, and if so use 'result'

Be aware:

  • If no "part" of the class chooses to implement PlaceOrder, the method (including all calls to it!) is (mentally) removed from all parts of the class before compilation. This also removes the evaluations of the arguments in the calls, which could be important if that evaluation had side effects (e.g. PlaceOrder(FindRequestAndCauseOtherEffects(), ref result);).
  • The restrictions I mentioned earlier that the method must return void and have no out parameters can be understood as a consequence thereof.

End of today's lesson on partial void methods.



回答3:

You can use partial class to split the code of a class in multiple, but you can not split a single method in 2 partial classes. You need to have method in only one place. If you want to extent the functionality then look for another methods like sub-classing or composition etc. which ever suits your scenario.



回答4:

The problem is that you're still technically in the same class. Partial classes are essentially combined when compiling, so what it sees is both methods defined in the same class. If you used a subclass, you could do that, but you can't have the method defined twice in the same class.



回答5:

You can't do that. What partial basically does is tell the C# compiler to join the two bits of code together.

A bit of a hacky solution is to finish off the class, and then inherit from that and override the methods you want, e.g. here's a simple example:

public partial class A
{
    public virtual void X() { }
}
public partial class A
{
    public void Y() { }
}
public class B : A
{
    public override void X() { }
}