May 5, 201510 yr If I were you I would use MethodBody.GetILAsByteArray (System.Reflection) Edited May 5, 201510 yr by SpoonStudio
May 5, 201510 yr Hey, How can I cast a byte[] into a methodbody by using dnlib? You can't cast a byte[] to any other incompatible type. You must parse the bytes. There's a class for that already: https://github.com/0xd4d/dnlib/blob/master/src/DotNet/Emit/MethodBodyReader.cs#L42 If I were you I would use MethodBody.GetILAsByteArray (System.Reflection) He already has a byte[].
May 5, 201510 yr Author @0xd4d : I tried understanding that part of dnlib but I have a bit of trouble.Could you probably provide an example snippet?Not that I want to c&p, - but its quiet hard to understand how to use the MethodBodyReader to convert a byte[] to a MethodBody.. :/
May 6, 201510 yr It assumes you have some basic .NET metadata knowledge. Check de4dot source code if you want to see code calling it. Where? Find All References in VS and you'll find it. Edited May 6, 201510 yr by 0xd4d
March 9, 20178 yr https://github.com/0xd4d/dnlib/blob/master/src/DotNet/MethodDef.cs Dnlib has in MethodDef a property called MethodBody. public MDToken MDToken { get { return new MDToken(Table.Method, rid); } } This is how methods are identified, so you resolve the method using Module.ResolveMethod(int int metadataToken):https://msdn.microsoft.com/en-us/library/k16h33dz(v=vs.110).aspx
March 9, 20178 yr On 5/6/2015 at 0:33 AM, yq8 said: @0xd4d : I tried understanding that part of dnlib but I have a bit of trouble. Could you probably provide an example snippet? Not that I want to c&p, - but its quiet hard to understand how to use the MethodBodyReader to convert a byte[] to a MethodBody.. :/ https://github.com/0xd4d/dnlib/blob/master/src/DotNet/Emit/MethodBody.cs namespace dnlib.DotNet.Emit { /// <summary> /// Method body base class /// </summary> public abstract class MethodBody { } Wired abstact classs MethodBody: I mean it has no methods! /// <summary> /// CIL (managed code) body /// </summary> public sealed class CilBody : MethodBody This static method is important: /// <summary> /// Creates a CIL method body or returns an empty one if <paramref name="code"/> is not /// a valid CIL method body. /// </summary> /// <param name="opResolver">The operand resolver</param> /// <param name="code">All code</param> /// <param name="exceptions">Exceptions or <c>null</c> if all exception handlers are in /// <paramref name="code"/></param> /// <param name="parameters">Method parameters</param> /// <param name="flags">Method header flags, eg. 2 if tiny method</param> /// <param name="maxStack">Max stack</param> /// <param name="codeSize">Code size</param> /// <param name="localVarSigTok">Local variable signature token or 0 if none</param> public static CilBody CreateCilBody(IInstructionOperandResolver opResolver, byte[] code, byte[] exceptions, IList<Parameter> parameters, ushort flags, ushort maxStack, uint codeSize, uint localVarSigTok) { return CreateCilBody(opResolver, code, exceptions, parameters, flags, maxStack, codeSize, localVarSigTok, new GenericParamContext()); } MethodBodyReader also contain some statics methods "public static CilBody CreateCilBody" ModuleDef contains this for resolving tokens: /// <summary> /// Resolves a token /// </summary> /// <param name="mdToken">The metadata token</param> /// <returns>A <see cref="IMDTokenProvider"/> or <c>null</c> if <paramref name="mdToken"/> is invalid</returns> public IMDTokenProvider ResolveToken(MDToken mdToken) { return ResolveToken(mdToken.Raw, new GenericParamContext()); }
Create an account or sign in to comment