I have the requirement to retrieve the method body from a .dll file in C#. I'm using System.Reflection
to retrieve the method names, parameters and properties etc. but didn't find a way the to retrieve the method body.
Please suggest me s开发者_如何转开发ome an idea... :-)
Well, you can retrieve the method body using MethodBase.GetMethodBody()
, which gets you the raw IL, the local variables etc.
Obviously you'd need to load the assembly first (e.g. with Assembly.Load
), then get the relevant type (Assembly.GetType
), then find the method (Type.GetMethod
), then get the method body.
You could use a reverse engineering tool like Reflector. (Mind you, this might be illegal, depending on the situation.)
Also I'm not sure if this is a correct answer to the "in C#.Net" part of your question, as this is an external tool, not something you can easily do yourself from your own C# code. For that you'd need to interpret the IL you can get through reflection (see Jon Skeets answer) yourself
Well if you mean to retrieve the code of the method, unless it comes attached to the .dll, I don't think you can retrieve it.
By the other way, maybe you mean this: http://msdn.microsoft.com/en-us/library/system.reflection.methodbody%28v=VS.80%29.aspx
Hope it'll help you regards
精彩评论