As the title states, is it possible to upcast/clone a base class into an extending class without knowing the internal structure of the base class?
As an example, given a match object with with named groups, I would like to subclass the match class into a new class NamedGroupMatch with properties that point to the named groups. So given a match instance, I am curious whether it is possible to upcast/clone this instance into an instance of NamedGroupMatch.
I am fully aware that I can accomplish this with composition, and that is beside the point.
Non functioning code example illustrating intent:
class NamedGroupMatch : System.Text.RegularExpressions.Match
{
public Group NamedGroup1 {get; set;}
public Group NamedGroup2 {get; set;}
public NamedGroupMatch(Match match)
{
//initialize this instance to be a clone of the match instance parameter
}
}
var regex = new Regex("pattern");
var match = regex.Match("some string with named groups");
//instead of working with a match instance,
//we would like to work with a NamedGroupMatch instance
//as it is logically an extension of a match object and should
// have all of the same properties.
var ngMatch = new NamedGroupMatch(match);