Create a .net dll wrapper around an existing .net library

I have a dll named ExpensiveAndLargeObfuscatedFoo.dll. Let's say it defines a type named ExpensiveAndLargeObfuscatedFooSubClass. It was compiled for .NET.

Are there any tools (free, paid, whatever) that will generate C # or vb class files that will do nothing but wrap everything that's defined in this expensive dll? That way I can add functionality, fix bugs (which CorpFUBAR won't fix), add a log, etc.?

Literally, I need an output similar to this

namespace easytoread {
    public class SubClass {
        private ExpensiveAndLargeObfuscatedFoo.SubClass _originalSubClass;
        public SubClass() {
            this._originalSubClass = new ExpensiveAndLargeObfuscatedFoo.SubClass ();
        }
        public string StupidBuggyMethod(string param1,int param2) {
            return _originalSubClass.StupidBuggyMethod(param1, param2);
        }
    }
}

      

It would have to handle custom return types as well as primitives

namespace easytoread {
    public class SubFooClass {
        private ExpensiveAndLargeObfuscatedFoo.SubFooClass _originalSubFooClass;
        public SubFooClass() {
            this._originalSubFooClass= new ExpensiveAndLargeObfuscatedFoo.SubFooClass ();
        }
        private SubFooClass(ExpensiveAndLargeObfuscatedFoo.SubFooClass orig) {
            this._originalSubFooClass = orig;
        }
        public SubFooClass StupidBuggyMethod(string param1,int param2) {
            return new SubFooClass(_originalSubFooClass.StupidBuggyMethod(param1, param2));
        }
    }
}

      

And so on, etc. for each specific class.

Basically a bad mans dynamic proxy? (yay, Castle Project awesome!)

We would also like to rename some of our wrapper classes, but the tool shouldn't.

Without renaming, we could replace the old assembly with our new generated one, change with statements and continue as nothing happened (except for the bugs were fixed!)

You just need to examine the dll and generate the code. the generated code could even be VB.NET, or ironpython, or something CLR.

It's a slippery slope and I'm not happy with being here, but it looks like it's the way to go. I looked at the Castle project, but if I'm not mistaken, this won't work for two reasons: 1) I can't rename anything (don't ask), 2) none of the assembly methods are declared virtual or even overridden, Even if they there were hundreds of types that I would have to override manually, which doesn't sound like fun.

+3


source to share


3 answers


The best answer seems to be "There is no such tool." So I will be taking a stab at writing my own later as a project outside of the hours. If I ever get something useful, I will update and update it.



UPDATE Visual Studio 2012 Fakes seems promising. http://msdn.microsoft.com/en-us/library/tfs/hh549175(v=vs.110).aspx - we moved on, but I could try to create a fake and then delete it as a replacement dll in the future

+1


source


ReSharper can do most of the work for you.

You will need to declare the base class:

namespace easytoread {
    public class SubClass {
        private ExpensiveAndLargeObfuscatedFoo.SubClass _originalSubClass;
    }
}

      



Then choose ReSharper> Edit> Generate Code (Alt + Ins), choose Delegate Members, select all and let it generate the code.

It will not nullify return values ​​with custom classes (it will return the original type), so it will still have to be added manually.

+3


source


  • If you have access to the source code, rename and correct the code in the source.
  • If you don't have access (and can legally do so), use some tool like Reflector or dotPeek to get the source code and then skip to the first point.
+1


source







All Articles