Using Reflection to acquire IL Code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nate

    Using Reflection to acquire IL Code

    All,

    I've been searching all over Google for a nice code sample explaining how to
    use reflection to get the instance of a method or property and then retrieve
    just the IL code for that method or property. The overall goal is to be able
    to reverse engineer a given assembly and create a diagram of dependencies.
    For instance, let's say that method foo() of class bob makes a call to method
    bar() of class sally, I would like to be able to map that "dependency " into a
    diagram, but the only way I can think of getting to that point is to be able
    to retrieve the IL code for method foo() and examine it in order to determine
    that it is calling a method bar() of class sally.

    Does anyone have any links to good code samples specific to the following or
    code snippets of their own that they are willing to post? Any suggestions
    are welcome.


    Thanks
  • Greg Young

    #2
    Re: Using Reflection to acquire IL Code

    The MethodBody class in 2.0 gives you access to the IL
    Provides access to the metadata and MSIL for the body of a method.

    http://blogs.msdn.com/haibo_luo/arch...02/476242.aspx includes a
    full example of parsing/formatting that IL

    Cheers,

    Greg Young
    MVP - C#




    "Nate" <Nate@discussio ns.microsoft.co mwrote in message
    news:AC48A9FD-2C2B-439B-A76C-5CAC293E99B2@mi crosoft.com...
    All,
    >
    I've been searching all over Google for a nice code sample explaining how
    to
    use reflection to get the instance of a method or property and then
    retrieve
    just the IL code for that method or property. The overall goal is to be
    able
    to reverse engineer a given assembly and create a diagram of dependencies.
    For instance, let's say that method foo() of class bob makes a call to
    method
    bar() of class sally, I would like to be able to map that "dependency "
    into a
    diagram, but the only way I can think of getting to that point is to be
    able
    to retrieve the IL code for method foo() and examine it in order to
    determine
    that it is calling a method bar() of class sally.
    >
    Does anyone have any links to good code samples specific to the following
    or
    code snippets of their own that they are willing to post? Any suggestions
    are welcome.
    >
    >
    Thanks

    Comment

    • Nicholas Paldino [.NET/C# MVP]

      #3
      Re: Using Reflection to acquire IL Code

      Nate,

      You can use reflection to get the MethodInfo instance for the method,
      which you can then use to get the method body (through the GetMethodBody
      method). Then, you can call the GetILAsByteArra y method on the MethodBody
      instance to get the corresponding IL code.

      From there, you will have to parse the IL from the byte array on your
      own.

      Hope this helps.


      --
      - Nicholas Paldino [.NET/C# MVP]
      - mvp@spam.guard. caspershouse.co m

      "Nate" <Nate@discussio ns.microsoft.co mwrote in message
      news:AC48A9FD-2C2B-439B-A76C-5CAC293E99B2@mi crosoft.com...
      All,
      >
      I've been searching all over Google for a nice code sample explaining how
      to
      use reflection to get the instance of a method or property and then
      retrieve
      just the IL code for that method or property. The overall goal is to be
      able
      to reverse engineer a given assembly and create a diagram of dependencies.
      For instance, let's say that method foo() of class bob makes a call to
      method
      bar() of class sally, I would like to be able to map that "dependency "
      into a
      diagram, but the only way I can think of getting to that point is to be
      able
      to retrieve the IL code for method foo() and examine it in order to
      determine
      that it is calling a method bar() of class sally.
      >
      Does anyone have any links to good code samples specific to the following
      or
      code snippets of their own that they are willing to post? Any suggestions
      are welcome.
      >
      >
      Thanks

      Comment

      Working...