Issues with extended classes and rewriting with methods

I have a .net website written in C # and will create functions that other developers can use. Therefore, I will do some standard implementation, and the developer can overwrite some methods.

Example: I have a class ShoppingCartand the Productproduct class class has a method getProductPrice then to calculate the total basket price the purchase code will call the methodgetProductPrice

ShoppingCartand Productare in the same project, and I will give the .dll developers so that they cannot change the source code so that we can update the assembly later

Therefore, they need to do another project and extend the product class and overwrite the method getProductPriceso that they can implement their own logic. The problem is that the shopping cart will not call the advanced method, but the original

If we make an already extended project for developers, and shoppingcart calls the advanced method, then we will get a circular link because the extended product needs a link to the product and a shopping cart for the extended product

Partial classes also do not work, because we can only use partial elements within the same assembly

Does anyone have a suggestion?

Thanks in advance.

+3
source share
2 answers

Product virtual:

public virtual decimal GetProductPrice() { };

, , :

public class OverriddenProduct : Product
{
   public override decimal GetProductPrice() { }
}

OverridenProduct ( ) Product, Product ShoppingCart.

symantics, , ShoppingCart, , GetTotalPrice() not GetProductPrice(), , .

+2

. , . .

, GetProductPrice , .

0

Source: https://habr.com/ru/post/1749195/


All Articles