Suppose I have a C# Project that references a dll that contains Class1. From Project1, I want to extend the functionality of Class1 by using a class called Class2 that inherits from Class1, and then I want to use Class2 whenever Class1 is expected (without modifying the calls to Class1 to point to Class2, this is key).
Moreoever, I have to add Class1 is an initialization module (in the framework I am using) which is not called explicitly (it's part of the inner mechanism). This means I can't simply go and replace calls to Class1 by Class2 after making Class2 extend the behaviour of Class1 in my code.
(How) can this be achieved?
Another idea that just crosses my mind is to use partial classes; however, I doubt that it's possible for a full class to be redefined 'later' as a partial class, outside of that dll.
===== Later edit: I came across an article that seems to suggest that is possible and I'm trying to figure out how. See, in this article it has RestrictFileTypes (Class2) inherit from IInitializableModule (Class1) which is in a dll, but it doesn't say how Class2 replaces all calls of Class1 from then on, going forward (http://world.episerver.com/blogs/al-higgs/dates/2012/11/Restricting-the-file-types/). What do you make of it?
I do not understand the second requirement ("Moreoever..."). As for the first, I see 2 ways.
You could write class2 and then swap the names of class1 and class2 by manual editing. The occurences should be few, just the cs file names and the constructors. It may be save to use an intermediate name first like you would generally do in a swap operation. This would probably be the easiest and cleanest way but you may not have any control over the current class1 and be unable to rename it. So here's the other way.
You write your class2 naming it class1 again from the start but in a different namespace. You will have a class1 descending from another class1 (the old one) in a different namespace. When you are done, put a using directive for your new namespace in every file that references your old class1 and make sure it is the last using line so it will take precedence over the ones above it. This should work if the current class1 is not a member of the same namespace it is referenced from. If the latter were the case, you would have to insert declarations of your new namespace into all the declarations of the existing one:
namespace ns.of.old.class1
{
namespace ns.of.new.class1
{
[...]
Some reference to / use of class1
}
}
The using directive trick is fragile, someone may notice the directives are not in alphabetic order and right-click them, choosing "Remove and Sort". Your code may then be broken or worse: still compile but using the old class1 again.