To some extent, this is more a thought exercise than a real problem, since I don’t have enough CustomFS classes to be particularly bothered by just using copy paste. But I wonder if there’s a better way.
Suppose I have several classes CustomFS, CustomFS2, etc., all of which inherit from FS, FS2, etc. FS/FS2/etc. all inherit from FSG, which has a function GetStuff. Assuming I do not have the ability to modify FS and FSG how can I override a particular function in many of of the FS/FS2 with only one CustomFS class, and without constructing CustomFS with FS and adding a wrapper function for all of FS’s methods to customFS.
Current strategy: Copy/paste:
class CustomFS : FS
{
protected override int GetStuff(int x)
{
int retval = base.GetStuff(x);
return retval + 1;
}
}
class CustomFS2 : FS2
{
protected override int GetStuff(int x)
{
int retval = base.GetStuff(x);
return retval + 1;
}
}
You can’t, except by means of a proxy or by emitting your own derived classes via Reflection.Emit.
However, if you have more complex functionality which you need to perform on each class, you might want to create a helper method (probably generic and static) which does the actual work.