Do not be surprised - user statements do not override anything, they are overloaded - therefore, they are selected at compile time, rather than at run time.
When we remove implicit text input from code, this makes it more understandable:
object bInstanceReflection = Activator.CreateInstance(typeof (B)); Console.WriteLine(bInstanceReflection.GetType());
Now itβs clear enough that the last line (A) is just different from the object , which performs a normal reference conversion. No custom conversions will be applied at all.
If you are using .NET 4, you can use dynamic typing to make it work:
// Note the change of type dynamic bInstanceReflection = Activator.CreateInstance(typeof (B)); Console.WriteLine(bInstanceReflection.GetType()); // <assemblyname>.B A aInstanceReflection = (A) bInstanceReflection;
Now the transformation is applied to the dynamic value, which means that the choice of which transformation in use is delayed until runtime is at that moment it will use your custom operator.
source share