inheritance - A way to extend existing class without creating new class in c# -


i have complete class doing awesome things. need allow users use class replacing methods in it, inheritance not allowed, because class used in other application classes.

it have class creating table, need allow users redefine method creating table cell let user print custom in cell. class, however, has default way print cell content (in case user not need customize it).

is there common-used or standartized way achieve this?

updated

having had "the peanut gallery" point out approach (at bottom) wouldn't fit bill, here's way:

use delegation. define public properties type action or func. these behaviors need invoked in code, compare properties null. if null, use default behavior. if not, invoke values.

your calling code may set properties, doesn't have to.

(first try) alternative approaches:

you describing extension method, or use of inheritance if that's available.

extension methods enable "add" methods existing types without creating new derived type, recompiling, or otherwise modifying original type. extension methods special kind of static method, called if instance methods on extended type. client code written in c# , visual basic, there no apparent difference between calling extension method , methods defined in type.

https://msdn.microsoft.com/en-us//library/bb383977.aspx

inheritance, encapsulation , polymorphism, 1 of 3 primary characteristics (or pillars) of object-oriented programming. inheritance enables create new classes reuse, extend, , modify behavior defined in other classes. class members inherited called base class, , class inherits members called derived class. derived class can have 1 direct base class. however, inheritance transitive. if classc derived classb, , classb derived classa, classc inherits members declared in classb , classa.

https://msdn.microsoft.com/en-us/library/ms173149.aspx

you can't derive .net types, can write extension methods them.


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -