how can i get method content by reflection or other way
like ILdasm doing
Mattias Sj?gren - 10 Jun 2004 01:32 GMT
>how can i get method content by reflection or other way?
Reflection doesn't support that in v1.x (but will in v2.0 I believe).
So you either have to write your own managed executable reader (the
file format is well documented) or use some existing library (there
are a few available, for example from
http://www.aisto.com/roeder/dotnet/).
Mattias

Signature
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Chad Z. Hower aka Kudzu - 10 Jun 2004 15:10 GMT
=?Utf-8?B?cyB6ZW1w?= <shayhandelman@hotmail.com> wrote in news:F4750901-478F-
421C-A5B2-83B35002EF54@microsoft.com:
> how can i get method content by reflection or other way?
> like ILdasm doing
Try Lutz's reflector. It decompiles methods into C#, VB, or Delphi.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"
Make your ASP.NET applications run faster
http://www.atozed.com/IntraWeb/
Alicia Li - 11 Jun 2004 16:41 GMT
There is not public refection APIs do it in V1.1, but in V2.0, you will be
able to do so.
Below is simple code:
//TargetMethod is methodinfo of the method that you want to inspect
MethodBody methodBody = TargetMethod.GetMethodBody();
// get local variables in to a collection. Then you can inspect them very
easily
IList<LocalVariableInfo> Locals = methodBody.LocalVariables;
Bool bInitLocals = methodBody.InitLocals;
//if you want to parse IL, you need to work on the byte[].
Byte[] IL = methodBody.GetILAsByteArray();
//get exception handling clases into a collection, then you can inspect
them.
IList<ExceptionHandlingClause> ehClauses =
methodBody.ExceptionHandlingClauses;
Below are the new APIs in V2.0:
MethodBase.GetMethodBody()
namespace System.Reflection
{
public sealed class MethodBody
{
// This class can only be created from inside the EE.
private MethodBody() { }
public int LocalSignatureMetadataToken { get; }
public IList<LocalVariableInfo> LocalVariables { get; }
public int MaxStackSize { get; }
public bool InitLocals { get; }
public byte[] GetILAsByteArray();
public IList<ExceptionHandlingClause> ExceptionHandlingClauses
{ get; }
}
}
1.1. (System.Reflection.LocalVariableInfo)
namespace System.Reflection
{
public class LocalVariableInfo
{
internal LocalVariableInfo() { }
public override string ToString();
public virtual Type LocalType { get; }
public virtual bool IsPinned { get; }
public virtual int LocalIndex { get; }
}
}
1.2. (System.Reflection.ExceptionHandlingClause
namespace System.Reflection
{
public sealed class ExceptionHandlingClause
{
// This class can only be created from inside the EE.
private ExceptionHandlingClause() { }
public ExceptionHandlingClauseFlags Flags { get; }
public int TryOffset { get; }
public int TryLength { get; }
public int HandlerOffset { get; }
public int HandlerLength { get; }
public int FilterOffset { get; }
public Type CatchType { get; }
}
public enum ExceptionHandlingClauseFlags : int
{
Clause = 0x0,
Filter = 0x1,
Finally = 0x2,
Fault = 0x4,
}
}
Thanks
Alicia