Using a derived type in Action
I'm deriving from an abstract class whose constructor accepts an object of type
Action<LocalMessageBase>
. LocalMessageBase
is an abstract class, of which there are a few derived classes, of which one is LocalInteractionMessageResponse
. Is there a way to use this derived type for the action instead of the base type, either directly within the action, or via some other casting magic?
This is what I tried to no avail:
though casting messageTemplate
to Action<LocalMessageBase>
doesn't cause any warning/hint lints in VS or via R#?3 Replies
You can cast
Action<LocalInteractionMessageResponse>
to Action<LocalMessageBase>
at runtime it'll throw InvalidCastException
You can however do something like this
how would this solution apply to my example exactly? if it removes any additional confusion, this ViewBase class I am deriving is third-party and I can't modify it, as well as any of the LocalMessageBase + derived classes
If you can't modify them, I'd do
Or if I'm going to be deriving from ViewBase a lot, I'd just do
@Kiel