|
I need to crate a new class which replicate a class in a given assembly but adding also a new constructor and adding new methods. Is it possible?
|
|
|
|
|
Yes, it's called inheritance and is a cornerstone of OOP.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
If the class is a class (and not a struct) and isn't sealed then you can inherit from it:
public class YourClass : OriginalClass
{
public YourClass(...) : base(...)
{ }
}
If it's a struct or a sealed class then you will need to create a wrapper and replicate all the constructors/properties/methods etc you require:
public WrapperClass
{
private WrappedType value;
public WrapperClass(...)
{
value = new WrappedType(...);
}
public void DoSomething()
{
value.DoSomething();
}
}
|
|
|
|
|
in may case the base class have not a parameterless constructor, while in the derived I need this one.
Is it possible to istantiate the base class in later mathod?
|
|
|
|
|
Not by using inheritance - you would need to use a wrapper as in my second snippet, the base would stay null until instanciated.
|
|
|
|
|
You cannot do it later in a method, but if you know the parameters, you can do it like this:
class BaseClass
{
public BaseClass(int theParam)
{
}
}
class DerivedClass : BaseClass
{
public DerivedClass() : base(4711)
{
}
}
|
|
|
|