How to replace a class in a dll?

The subject is pretty vague since I'm not sure what's the correct terminology for what I'm trying to do.

I've downloaded a dll (I don't have the source code), and using a reflection tool, I found a bug in the dll implementation. The bug is easy to fix. So let's say the bug is here:

class A 
{
    void f() { // BUG!!! }
}

Is there any way to implement my own A which would fix the bug and inject it in runtime to replace other A instances?


Solution 1:

If you are using .NET 4.0. or higher, take a look at the: MethodRental.SwapMethodBody Method

Other way: CLR Injection: Runtime Method Replacer

Solution 2:

Easiest way would be to inherit from that class and write your own implementation.

  class ParentClass
            {
                public void SomeMethod() { 
                   //bug here 
                }
            }

            class Child:ParentClass
            {
                new public void SomeMethod() { 
                   // I fixed it 
                }
            }

Here after, use your class.

Child child = new Child();
child.SomeMethod();