Something like MethodInfo.GetILBytes()?

  • Thread starter Thread starter James F. Bellinger
  • Start date Start date
J

James F. Bellinger

Is there any (managed) function which can retrieve a method's IL data? Or
would one have to write a PE file reader (which would still not do if it
were dynamically created...)

If not, are there any plans to add something of this sort? It would be
extremely handy. What I'd like to do is have someone pass a System.Type,
which my code would then check for a certain interface, and if it had that
interface it would read the IL. However as far as I can tell one can only
write IL. :-/

Thanks :)
Jim Bellinger
 
Well, I can't use that, because it appears to be closed source. Also,
there's no License.txt with it. :-)

Nevertheless, its Example.cs uses MethodBase, so that gave me a hint where
to look (didn't see anything in MethodInfo, was frustrating ;-). I'm
guessing MethodHandle (mentions "internal metadata") has something to do
with what I am looking for.

Would be nicer to have it built in, but this indicates it is possible to do
without accessing unmanaged code, and it's in MethodBase, otherwise
Example.cs wouldn't pass that most likely, so this gives me hope. :-) Still,
MethodHandle being a RuntimeMethodHandle, it returning an IntPtr doesn't
look too helpful (how does the GC interact with such a thing? *puzzled*) Oh
well, if it's possible to do I'll keep looking. Can't use that though due to
the reasons mentioned above. :-)

On an unrelated note: After I thought about it, GetILBytes() would be a bad
method. Something like an ILStream would make more sense (and GetILStream(),
since methods might be fairly long, even though the whole thing would
usually be read. Maybe just have the internal implementation be a
MemoryStream, but it would also be good to have a Version property for such
a stream, so that one could see if the IL was compatible with the reader (it
could be the lowest version the reader could support for the method to be
readable). Anyhow, that would be good :)

Thanks :-)
Jim Bellinger
 
Ugh, or maybe it's got nothing to do with those handles. The memory at those
addresses looks nothing like anything in my test file. :-(

James F. Bellinger said:
Well, I can't use that, because it appears to be closed source. Also,
there's no License.txt with it. :-)

Nevertheless, its Example.cs uses MethodBase, so that gave me a hint where
to look (didn't see anything in MethodInfo, was frustrating ;-). I'm
guessing MethodHandle (mentions "internal metadata") has something to do
with what I am looking for.

Would be nicer to have it built in, but this indicates it is possible to do
without accessing unmanaged code, and it's in MethodBase, otherwise
Example.cs wouldn't pass that most likely, so this gives me hope. :-) Still,
MethodHandle being a RuntimeMethodHandle, it returning an IntPtr doesn't
look too helpful (how does the GC interact with such a thing? *puzzled*) Oh
well, if it's possible to do I'll keep looking. Can't use that though due to
the reasons mentioned above. :-)

On an unrelated note: After I thought about it, GetILBytes() would be a bad
method. Something like an ILStream would make more sense (and GetILStream(),
since methods might be fairly long, even though the whole thing would
usually be read. Maybe just have the internal implementation be a
MemoryStream, but it would also be good to have a Version property for such
a stream, so that one could see if the IL was compatible with the reader (it
could be the lowest version the reader could support for the method to be
readable). Anyhow, that would be good :)

Thanks :-)
Jim Bellinger
 
Back
Top