In C#, if I have
public class BaseClass
{
//BaseClass implementation
}
public class Derived : BaseClass
{
//Derived implementation
}
public class AnotherClass
{
AnotherClass(BaseClass baseClass)
{
//Some code
}
AnotherClass(Derived derived) : this(derived as BaseClass)
{
//Some more code
}
}
and then do:
BaseClass baseVariable = new Derived();
AnotherClass anotherVariable = new AnotherClass(baseVariable);
This will lead to early binding, calling the AnotherClass(BaseClass)
method.
Instead, if I cast it using dynamic
keyword - or instantiate a variable with dynamic and then pass it as constructor argument, the AnotherClass(Derived)
will be invoked:
BaseClass baseVariable = new Derived();
//This will instantiate it using the AnotherClass(Derived)
AnotherClass anotherVariable = new AnotherClass(baseVariable as dynamic);
Is method overloading early bound (Evaluated at compile time) in C#? That meaning, is there any other way or trick to determine the mostly-derived call to the other class constructor to apply the invocation of the constructor that takes the mostly derived class type as argument in my case without the use of dynamic
or reflection?