I am refactoring an old library with some class like the following one:
public class Template //Old one
{
public double Value { get; set; }
}
Now it's better to provide more flexibility to allow user defined Value
type. So I changed Template
to a generic class:
public class Template<T>
{
public T Value { get; set; }
}
This change breaks the usages of old Template
class from others, so I tried to add backward compatibility (allows others to use my library in the old way) with:
public class Template: Template<double> { }
But it also requires lots of changes in the library. Especially where the old Template
is used, such as:
public class AnotherClassInLibrary<T>
{
public Template<T> API() {...}
}
public class AnotherClassInLibrary : AnotherClassInLibrary<double>
{
// This class is also defined for the old way of using the library
public Template API()
{
return base.API();
// Error here: Template<double> cannot be implicitly convert to Template.
}
}
So here comes the problem: the new Template
is inherited from Template<double>
, so casting is not a good/working practice here. Is there a work around to keep AnotherClassInLibrary
backward compatible while not rewrite the API()
code twice?
P.S. I really hope C# has something like typedef
in C++. But the answer is NO.