There are two ways to do this: Inheritance and Extension Methods.
Inheritance is simple: add a new class and derive it from the original:
namespace MyProject
{
internal class MyNewControl : OriginalControl
{
... Add your new stuff here ...
}
}
All the appropriate methods, fields, properties, events, and so on are available as a part of your new class, and your class code can access all non-
private
objects and methods in the base class.
Extension methods are harder, and are mostly used when the original class is
sealed
(and all
struct
s are
sealed
so cannot be inherited from). They must be declared in a
static
class, and all methods must be declared as static (thought they can be accessed as instance methods if
this
is used in teh declaration:
namespace ExtensionMethods
{
public static class MyExtensions
{
public static int MyNewMethod(this OriginalClass cls)
{
return cls.PropertyOfSometype;
}
}
}
There is more info here:
Extension Methods - C# | Microsoft Learn[
^]