Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace a class in a dll?

Tags:

c#

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?

like image 931
Shmoopy Avatar asked Feb 25 '14 12:02

Shmoopy


People also ask

Can we add a new method to an external DLL file's class?

We can create new methods in the dll library class also without changing the old code though extension method. Let's see the simple example of extensionmethod . return (a + " " + b); // This is the extension method named Myextmethod.

How can I tell what class A DLL is?

Search for your class name in the object browser (Menu View -> Object Browser). The assembly information window will contain the dll complete path. Show activity on this post.

When to use class library c#?

Using libraries is useful when you want to share code between multiple programs. If you think you will need your classes to sanitize data in more than one program, this is a good idea to put them in a library.

What is a class library project in c#?

A class library defines types and methods that are called by an application. If the library targets . NET Standard 2.0, it can be called by any . NET implementation (including . NET Framework) that supports .


2 Answers

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

Other way: CLR Injection: Runtime Method Replacer

like image 75
HABJAN Avatar answered Oct 08 '22 02:10

HABJAN


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(); 
like image 31
danish Avatar answered Oct 08 '22 00:10

danish