none
Creating a class library from a 3rd party dll RRS feed

  • Question

  • Hi.

    We have a 3rd party dll that's being used by different applications (and by different developers).  We want to simplify the use of this dll by creating a class library which encapsulates only the methods and the objects that we need, and thus avoid rewriting code every time we use the dll.

    My question is... Is it possible to create a class library (a dll), which uses the 3rd party dll, but not having to reference or copy the 3rd party dll in our projects' output folder when we use the new class library?

    Thanks... any insight is appreciated.
    Monday, December 1, 2008 9:36 PM

Answers

  • I have to say, this is not a good approach.  For one, it certainly won't avoid the need to have to reference the 3rd party assembly.  You'll have to reference to allow your shim to compile.  Using Assembly.Load(Byte[]) is fraught with trouble too, you'll end up loading the assembly over and over again.  Okay for exceptional stuff, not for common usage.  Writing a shim is a good idea, trying to avoid referencing or deploying it is not.
    Hans Passant.
    • Marked as answer by Zhi-Xin Ye Monday, December 8, 2008 9:22 AM
    Tuesday, December 2, 2008 12:43 AM
    Moderator

All replies

  • I think you are looking to do something like the following (embedding assemblies):
    http://objecthead.blogspot.com/2006/04/embedded-assemblys.html
    - Parker Hillius
    Monday, December 1, 2008 9:59 PM
  • Thanks Parker...

    It's an interesting article, but I think this will only work with an exe file?  If I'm creating a class library, where do i put

    AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);



    Monday, December 1, 2008 10:19 PM
  • I actually ran into this building an Exception Handling library, because we wanted to hook to the CurrentDomain.UnhandledException. I went about it like so...first define your static "helper" class: (I'm going oto use the exception handling as an example since i'm familiar with it).
    public static class ExceptionHandler {

        // Define a Setup method that user's must call
    // Note: this actually doesn't do anything but it gets the static class initialized in effect calling the static constructor below
        public static void Setup() { }
        
        static ExceptionHandler() {
            AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
        }

        static void CurrentDomain_AssemblyResolve(object sender, EventArgs e) { // Or whatever parameters...
        }
    }

    Then from the app consuming your new Class Library, you would do something like so:
    public static class Program {
         static void Main(String[] args) {
              ExceptionHandler.Setup(); // This gets everything initizlied and setup so you want to call it right away.
              // Now you should be able to use it anywhere you want with whatever methods you define.
              ExceptionHandler.SomeMethod();
         }
    }

    That was kind of off the cuff and quick, but let me know what you find out or if you need more detail/help.


    - Parker Hillius
    Monday, December 1, 2008 10:23 PM
  • Looks good, ill give it a try.  Thanks
    Monday, December 1, 2008 10:31 PM
  • I have to say, this is not a good approach.  For one, it certainly won't avoid the need to have to reference the 3rd party assembly.  You'll have to reference to allow your shim to compile.  Using Assembly.Load(Byte[]) is fraught with trouble too, you'll end up loading the assembly over and over again.  Okay for exceptional stuff, not for common usage.  Writing a shim is a good idea, trying to avoid referencing or deploying it is not.
    Hans Passant.
    • Marked as answer by Zhi-Xin Ye Monday, December 8, 2008 9:22 AM
    Tuesday, December 2, 2008 12:43 AM
    Moderator