0

Is it possible to compare 2 methods using reflection in a deeper way?

For instance, I have file1.dll and file2.dll. They both have the Class X which has a method Y that is named the same and do the same thing.
So my question is if its possible to see the body of the method Y is different between those 2 files using reflection?

Neffarion
  • 59
  • 1
  • 7
  • Related: https://stackoverflow.com/q/2693881/9287029 – Chris Yungmann Oct 09 '19 at 18:09
  • Please use search before creating new question: https://stackoverflow.com/questions/2693881/can-i-use-reflection-to-inspect-the-code-in-a-method – Vitalii Ilchenko Oct 09 '19 at 18:10
  • 1
    Just for context, why are you doing this? – DevEstacion Oct 09 '19 at 18:25
  • 1
    @VitaliiIlchenko I don't think OP is asking for detailed information "body of the method Y is different between those 2 files" - simple diff as suggested by Chris Yungmann answer does satisfy that requirement... While indeed OP may be actually looking for something different this feels clear and complete enough to stay as separate question for me... (Lack of research can be indicated by downvote...) – Alexei Levenkov Oct 09 '19 at 18:33
  • @DevEstacion Just analyzing code and comparing differences. Thought if done this way it could make it easier – Neffarion Oct 09 '19 at 18:34

1 Answers1

2

You can use MethodBase.GetMethodBody followed by MethodBody.GetILAsByteArray in order to get the MSIL for the method body as an array of bytes. As the second link notes, however,

Parsing method bodies requires a thorough understanding of metadata and MSIL instruction formats. Information can be found in the Common Language Infrastructure (CLI) documentation, especially "Partition II: Metadata Definition and Semantics" and "Partition III: CIL Instruction Set". The documentation is available online; see ECMA C# and Common Language Infrastructure Standards on MSDN and Standard ECMA-335 - Common Language Infrastructure (CLI) on the Ecma International Web site.

Even if the two methods have the same MSIL, they did not necessarily originate with the same C# source code. You could attempt to decompile the MSIL into C# source code using something like Mono.Cecil, DelegateDecompiler, or ICSharpCode.Decompiler, but I would not recommend going down that road and instead suggest reconsidering your approach.

Chris Yungmann
  • 1,079
  • 6
  • 14