Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 690751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:28:07+00:00 2026-05-14T02:28:07+00:00

I have some MSIL in byte format (result of reflection’s GetMethodBody()) that I’d like

  • 0

I have some MSIL in byte format (result of reflection’s GetMethodBody()) that I’d like to analyze a bit. I’d like to find all classes created with the new operator in the MSIL. Any ideas on how to do that programmatically?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:28:08+00:00Added an answer on May 14, 2026 at 2:28 am

    I ended up using the MSIL parser here: http://blogs.msdn.com/zelmalki/archive/2008/12/11/msil-parser.aspx, with the source slightly modified to work on ConstructorInfo as well as MethodInfo (results returned from reflector).

    It will give a list of operations, with the opcode and parameters. The opcode is an enum, based on that value the parameters can be interpreted. The parameters are in binary form, need to used MethodInfo.Module.Resolve*() to get the actual parameter values.

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Reflection;
    using System.Reflection.Emit;
    using System.Text;
    
    namespace AspnetReflection
    {
        public class MsilReader
        {
            static readonly Dictionary<short, OpCode> _instructionLookup;
            static readonly object _syncObject = new object();
            readonly BinaryReader _methodReader;
            MsilInstruction _current;
            Module _module; // Need to resolve method, type tokens etc
    
    
            static MsilReader()
            {
                if (_instructionLookup == null)
                {
                    lock (_syncObject)
                    {
                        if (_instructionLookup == null)
                        {
                            _instructionLookup = GetLookupTable();
                        }
                    }
                }
            }
    
            public MsilReader(MethodInfo method)
            {
                if (method == null)
                {
                    throw new ArgumentException("method");
                }
    
                _module = method.Module;
                _methodReader = new BinaryReader(new MemoryStream(method.GetMethodBody().GetILAsByteArray()));
            }
    
            public MsilReader(ConstructorInfo contructor)
            {
                if (contructor == null)
                {
                    throw new ArgumentException("contructor");
                }
    
                _module = contructor.Module;
                _methodReader = new BinaryReader(new MemoryStream(contructor.GetMethodBody().GetILAsByteArray()));
            }
    
            public MsilInstruction Current
            {
                get { return _current; }
            }
    
    
            public bool Read()
            {
                if (_methodReader.BaseStream.Length == _methodReader.BaseStream.Position)
                {
                    return false;
                }
    
                int instructionValue;
    
                if (_methodReader.BaseStream.Length - 1 == _methodReader.BaseStream.Position)
                {
                    instructionValue = _methodReader.ReadByte();
                }
                else
                {
                    instructionValue = _methodReader.ReadUInt16();
    
                    if ((instructionValue & OpCodes.Prefix1.Value) != OpCodes.Prefix1.Value)
                    {
                        instructionValue &= 0xff;
                        _methodReader.BaseStream.Position--;
                    }
                    else
                    {
                        instructionValue = ((0xFF00 & instructionValue) >> 8) |
                                           ((0xFF & instructionValue) << 8);
                    }
                }
    
                OpCode code;
    
                if (!_instructionLookup.TryGetValue((short) instructionValue, out code))
                {
                    throw new InvalidProgramException();
                }
    
                int dataSize = GetSize(code.OperandType);
    
                var data = new byte[dataSize];
    
                _methodReader.Read(data, 0, dataSize);
    
                _current = new MsilInstruction(code, data);
    
                return true;
            }
    
            static int GetSize(OperandType opType)
            {
                int size = 0;
    
                switch (opType)
                {
                    case OperandType.InlineNone:
                        return 0;
                    case OperandType.ShortInlineBrTarget:
                    case OperandType.ShortInlineI:
                    case OperandType.ShortInlineVar:
                        return 1;
    
                    case OperandType.InlineVar:
                        return 2;
    
                    case OperandType.InlineBrTarget:
                    case OperandType.InlineField:
                    case OperandType.InlineI:
                    case OperandType.InlineMethod:
                    case OperandType.InlineSig:
                    case OperandType.InlineString:
                    case OperandType.InlineSwitch:
                    case OperandType.InlineTok:
                    case OperandType.InlineType:
                    case OperandType.ShortInlineR:
                        return 4;
                    case OperandType.InlineI8:
    
                    case OperandType.InlineR:
    
    
                        return 8;
    
                    default:
    
                        return 0;
                }
            }
    
    
            static Dictionary<short, OpCode> GetLookupTable()
            {
                var lookupTable = new Dictionary<short, OpCode>();
    
                FieldInfo[] fields = typeof (OpCodes).GetFields(BindingFlags.Static | BindingFlags.Public);
    
                foreach (FieldInfo field in fields)
                {
                    var code = (OpCode) field.GetValue(null);
    
                    lookupTable.Add(code.Value, code);
                }
    
                return lookupTable;
            }
        }
    
    
        public struct MsilInstruction
        {
            public readonly byte[] Data;
            public readonly OpCode Instruction;
    
            public MsilInstruction(OpCode code, byte[] data)
            {
                Instruction = code;
    
                Data = data;
            }
    
    
            public override string ToString()
            {
                var builder = new StringBuilder();
    
                builder.Append(Instruction.Name + " ");
    
                if (Data != null && Data.Length > 0)
                {
                    builder.Append("0x");
    
                    foreach (byte b in Data)
                    {
                        builder.Append(b.ToString("x2"));
                    }
                }
    
                return builder.ToString();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have some users in Liferay 4.4.2 that are currently active, all having valid passwords.
Have some dates in my local Oracle 11g database that are in this format:
Have some text that needs to be replaced, searched around this website for all
I have some JSON that is sent to my webservice that looks something like
Hej All I have some code that builds a new TYPE runtime, it sets
I have some perl code that looks something like this: my @array = map
I have some .net dll's and Exe's. I need to pass MSIL files to
I have some files that are uuencoded, and I need to decode them, using
I have some files stored at amazon. all in private mode, and since I
I have some script in my default page that redirects users to language specific

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.