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 41241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:08:58+00:00 2026-05-10T15:08:58+00:00

In Microsoft IL, to call a method on a value type you need an

  • 0

In Microsoft IL, to call a method on a value type you need an indirect reference. Lets say we have an ILGenerator named ‘il’ and that currently we have a Nullable on top of the stack, if we want to check whether it has a value then we could emit the following:

var local = il.DeclareLocal(typeof(Nullable<int>)); il.Emit(OpCodes.Stloc, local); il.Emit(OpCodes.Ldloca, local); var method = typeof(Nullable<int>).GetMethod('get_HasValue'); il.EmitCall(OpCodes.Call, method, null); 

However it would be nice to skip saving it as a local variable, and simply call the method on the address of the variable already on the stack, something like:

il.Emit(/* not sure */); var method = typeof(Nullable<int>).GetMethod('get_HasValue'); il.EmitCall(OpCodes.Call, method, null); 

The ldind family of instructions looks promising (particularly ldind_ref) but I can’t find sufficient documentation to know whether this would cause boxing of the value, which I suspect it might.

I’ve had a look at the C# compiler output, but it uses local variables to achieve this, which makes me believe the first way may be the only way. Anyone have any better ideas?

**** Edit: Additional Notes ****

Attempting to call the method directly, as in the following program with the lines commented out, doesn’t work (the error will be ‘Operation could destabilise the runtime’). Uncomment the lines and you’ll see that it does work as expected, returning ‘True’.

var m = new DynamicMethod('M', typeof(bool), Type.EmptyTypes); var il = m.GetILGenerator(); var ctor = typeof(Nullable<int>).GetConstructor(new[] { typeof(int) }); il.Emit(OpCodes.Ldc_I4_6); il.Emit(OpCodes.Newobj, ctor); //var local = il.DeclareLocal(typeof(Nullable<int>)); //il.Emit(OpCodes.Stloc, local); //il.Emit(OpCodes.Ldloca, local); var getValue = typeof(Nullable<int>).GetMethod('get_HasValue'); il.Emit(OpCodes.Call, getValue); il.Emit(OpCodes.Ret); Console.WriteLine(m.Invoke(null, null)); 

So you can’t simply call the method with the value on the stack because it’s a value type (though you could if it was a reference type).

What I’d like to achieve (or to know whether it is possible) is to replace the three lines that are shown commented out, but keep the program working, without using a temporary local.

  • 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. 2026-05-10T15:08:59+00:00Added an answer on May 10, 2026 at 3:08 pm

    If the variable is already on the stack, you can go ahead and just emit the method call.

    It seems that the constructor doesn’t push the variable on the stack in a typed form. After digging into the IL a bit, it appears there are two ways of using the variable after constructing it.

    You can load the variable that will store the reference onto the evaluation stack before calling the constructor, and then load that variable again after calling the constructor like so:

    DynamicMethod method = new DynamicMethod('M', typeof(bool), Type.EmptyTypes); ILGenerator il = method.GetILGenerator(); Type nullable = typeof(Nullable<int>); ConstructorInfo ctor = nullable.GetConstructor(new Type[] { typeof(int) }); MethodInfo getValue = nullable.GetProperty('HasValue').GetGetMethod(); LocalBuilder value = il.DeclareLocal(nullable);           // load the variable to assign the value from the ctor to il.Emit(OpCodes.Ldloca_S, value); // load constructor args il.Emit(OpCodes.Ldc_I4_6); il.Emit(OpCodes.Call, ctor); il.Emit(OpCodes.Ldloca_S, value);  il.Emit(OpCodes.Call, getValue); il.Emit(OpCodes.Ret); Console.WriteLine(method.Invoke(null, null)); 

    The other option is doing it the way you have shown. The only reason for this that I can see is that the ctor methods return void, so they don’t put their value on the stack like other methods. It does seem strange that you can call Setloc if the new object isn’t on the stack.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 64k
  • Answers 64k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer First of all, it's IMHO incorrect not to send 'Vary:… May 11, 2026 at 10:54 am
  • added an answer Here's my guess (I posted as community wiki so you… May 11, 2026 at 10:54 am
  • added an answer I know for a fact you can use that feature… May 11, 2026 at 10:54 am

Related Questions

In Microsoft IL, to call a method on a value type you need an
I'm the maintainer of http://www.linux.org.il/ and it doesn't look properly in Microsoft Internet Explorer
In Microsoft SQL Server, is there a way to detect whether a database has
In Microsoft Oslo SDK CTP 2008 (using Intellipad) the following code compiles fine: module
in Microsoft Access, is there a way which I can programatically set the Confirm
In Microsoft's UnitTesting namespace ( Microsoft.VisualStudio.TestTools.UnitTesting ) there are AssemblyInitialize and AssemblyCleanup attributes you
In Microsoft Sql it is possible to encrypted stored procedures with CREATE PROCEDURE dbo.foo
How to export pictures in Microsoft Word to TIFF file using Visual Studio Tools
We are utilizing the XML data type in Microsoft SQL Server 2005 for a
How would you create a database in Microsoft Access that is searchable only by

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.