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

  • Home
  • SEARCH
  • 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 6006585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:33:38+00:00 2026-05-23T01:33:38+00:00

I want to generate IL for the 2D array construction, using System.Reflection.Emit namespace. My

  • 0

I want to generate IL for the 2D array construction, using System.Reflection.Emit namespace.

My C# code is

Array 2dArr  = Array.CreateInstance(typeof(int),100,100); 

Using ildasm, I realized that following IL Code is generated for the above
C# code.

IL_0006:  call       class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
IL_000b:  ldc.i4.s   100
IL_000d:  ldc.i4.s   100
IL_000f:  call       class [mscorlib]System.Array [mscorlib]System.Array::CreateInstance(class [mscorlib]System.Type, 
                                                                                           int32,
                                                                                           int32)

I was able to generate last three IL statements as given below.

MethodInfo createArray = typeof(Array).GetMethod("CreateInstance",
                new Type[] { typeof(Type),typeof(int),typeof(int) });
gen.Emit(OpCodes.Ldc_I4_1);
           gen.Emit(OpCodes.Ldc_I4_1);
           gen.Emit(OpCodes.Call, createArray);

But I don’t have clear idea about how to generate fist IL statement (i.e. IL_0006: call class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
)

Do you have any idea?

Furthermore, Can somebody point put some good tutorials/documents about how to
Use System.Reflection.Emit namespace in order to generate IL codes?

  • 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-23T01:33:39+00:00Added an answer on May 23, 2026 at 1:33 am

    Ah, good ol’ typeof; yes, that is:

     il.Emit(OpCodes.Ldtoken, typeof(int));
     il.EmitCall(OpCodes.Call, typeof(Type).GetMethod("GetTypeFromHandle"), null);
    

    Re guidance… my trick if I get stuck is always “compile something similar, and look at it in reflector”.

    If you wanted some examples, dapper-dot-net and protobuf-net both do a decent amount of IL – the first is more contained, limited and understandable; the second is all-out, no-holds-barred crazy IL.

    Hints for IL:

    • track the stack in comments at EVERY step on the right hand side of the screen
    • use the short-forms of branches etc, but only use them when you know you have a very local branch
    • write yourself a little set of utility methods even for simple things like loading an integer (which is actually quite complex since there are 12 different ways of loading an int-32, depending on the value)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using T4 I want to generate some code based on examining what files are
I have a task and I want to generate some code using the CodeDom.
Hi I want to be able to generate a list using find so that
I want to generate a thumbnail preview of videos in Java. I'm mostly JMF
I want to generate some XML in a stored procedure based on data in
I want to generate some formatted output of data retrieved from an MS-Access database
I want to generate a list in C#. I am missing python's list comprehensions.
I want to generate a dict with the letters of the alphabet as the
I want to generate random numbers manually. I know that every language have the
Say you want to generate a matched list of identifiers and strings enum {

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.