DLL override method

I'm not even sure if this is possible, so apologize if not. I searched Google quite widely and did not find what I was looking for.

Basically, we have an application created by a third party, which, being completely dumb, is garbage. We have a particular problem, and we were able to trace the problem using ILSpy to a method in the DLL. Obviously, we do not have (and cannot get) the source code, and the company in question does not want to fix the problem in a reasonable amount of time.

So, we explored various areas of the investigation and came up with nothing. I am studying whether this can be done through reflection, and this is pretty much the last hope we have for getting this to work. In short, I would like to do the following:

  • Create a simple class library with the same name as the existing DLL
  • Use reflection to import methods from an existing DLL
  • Somehow override the method in question with my own correct code
  • Rebuild the code, so I have a new DLL containing 99% of the functionality of the existing DLL, but with my redefinition code providing the correct functionality.

During my research, I found TypeBuilder.DefineMethodOverride, as well as a page from StackOverflow, which seems similar, but not quite what I am looking for.

http://msdn.microsoft.com/en-us/library/system.reflection.emit.typebuilder.definemethodoverride.aspx

Is there a way to override the reflection method?

Any advice appreciated!

Andrew

Edit

Another possible idea I had was to create a partial class containing an override function, but that also seemed impossible.

+8
reflection c # dll
source share
4 answers

You can override a method only if it is virtual, and it doesn’t matter if you do it through reflection or statically. I would suggest using a decompiler (there are many free ones) and a code fix in MSIL. Then you can create a new assembly from MSIL.

+6
source share

I think your first idea is good. If the third-party class is not sealed , you can extract from it and add your own method with a different name to correct the incorrect behavior. If you need it to be in 1 dll, you can use IlMerge .

If your third-party class is sealed, you can simply have an instance of this third class in your new class and call methods if necessary.

But you will need to verify that the method you want to "override" is not called inside this library, because if this solution does not work ...

This is not very clean, but it may be a workaround for the time when the company editing the library fixes the problem.

And when it is fixed, you just need to rename the method you use, so it will not take much time.

0
source share

From what you described, I would recommend changing the original assembly. Process essentially

  • Decompile the assembly in MSIL, C #, or any other language you choose
  • Modify the decompiled assembly to include your changes.
  • Recompile the assembly using the modified source

From what I see Reflexil , you can do this, although you may need to buy Resharper (I haven’t tried it myself)

Alternatively, you can use ILDasm to decompile the entire assembly into a single file, modify the file, and then recompile it with ILAsm

0
source share

I know I'm a little late for this, but I agree with Charle; if you have a class that does not behave well and does not facilitate substitution, but at least declares its methods as virtual , then you are in luck. The following links link to Castle.Core and Patterns :

 var interceptor = new DelegateInterceptor(proceed: call => { if(ShouldCallProceed(call)) call.Proceed(); else AlternativeLogic(); }); var generator = new ProxyGenerator(); var proxy = generator.CreateClassProxy<UncooperativeType>(interceptor); proxy.RubbishMethod(); 

I also allowed to run a sample of this in LinqPad. It shows the difference between methods that allow interception ( virtual ) and those that do not. It also shows a useful way to eliminate Try.Do capture using Try.Do from Patterns .

0
source share

All Articles