Class library with multiple public aliases for a class
I'm looking for some ideas on how to do this in the most efficient way.
I am replacing an old COM library that is used in a large legacy ASP website with a .NET library written in C#. The old library consisted of just two classes. I've rewritten this functionality in just one class in the new library.
The old code makes multiple references to both classes in several dozen pages. I've been getting around the name issue by exposing some empty classes that derive from my single master class which performs the functionality of both previous classes and all is well:
public class Foo1 : Bar {}
public class Foo2 : Bar {}
Bar being the master class which performs all functionality and the two empty classes being the names used within the ASP pages that reference this functionality. Declaring these empty classes seems inefficient but it works fine and alleviates the need to touch the existing legacy ASP code.
I'd ideally like to do something like this in my library:
public using Foo1 = Bar;
public using Foo2 = Bar;
Obviously this doesn't work but I feel it best describes what I'd like to do. I would like my class to have two public aliases that can be refe开发者_Python百科renced outside it's namespace.
I've been doing quite a bit of digging and I have not been able to find a method that will allow me to do this.
I've got a couple different solutions however.
- Rename all existing class references in the ASP code to be consistent with my new class.
- Use some empty public class declarations that derive from my new all inclusive class.
Are there any other methods which would be more efficient?
I guess inheriting from your master class is the better approach. This will allow you not to go through all the references and risking making some mistakes here and there, and ensures your changes are transparent for your application. I think this is efficient enough! =)
The "using x = int" in C# is effectively used to tell the compiler to do a find&replace on all declarations of something "of x" and replace them with something "of int", as Eric Lippert answered on another question recently (His blog, http://blogs.msdn.com/ericlippert/ is well worth adding to your reading list!).
By far the simplest option would be to just have your inherited Foo1 and Foo2 classes, with a comment on each of them explaining why you've done what you've done (so the person that supports the code when you leave, if it's not changed by then!), and I'd say it's a job done :)
精彩评论