在部分类中重写一个虚方法 [英] Override a virtual method in a partial class
问题描述
我目前与 nopCommerce 源代码工作,并尽我所能来避免编辑源在所有的,而是使用部分类和从源代码单独的插件,我们应该永远需要升级的版本。
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)
{ //....
我的部分类:
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:
键入Nop.Services.Orders.OrderProcessingService'已经定义了一个名为placeOrder在用相同的参数类型
会员
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?
推荐答案
您不能覆盖在同一类的虚拟方法。部分类只是同一类分裂的不同位置的定义,所以这是不可能的它没有定义一个层次
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) { //....
}
这篇关于在部分类中重写一个虚方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!