Jump to content
View in the app

A better way to browse. Learn more.

Tuts 4 You

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Cast byte[] to methodbody

Featured Replies

Posted

Hey,


How can I cast a byte[] into a methodbody by using dnlib?


If I were you I would use MethodBody.GetILAsByteArray (System.Reflection)


Edited by SpoonStudio

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[].

My bad.


  • 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.. :/

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 by 0xd4d

  • 1 year later...

how can i get MethodBody by MethodDef?

Edited by Avenger

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

 

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.