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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:50:44+00:00 2026-05-27T20:50:44+00:00

I’m creating a system that turns a small script into a dll. I’m encountering

  • 0

I’m creating a system that turns a small script into a dll. I’m encountering a problem when I try to take a nullable value class and make it the default value of a parameter.
The problem is that I need to create an instance of user selected nullable within the compiler and set it as the constant.

Unfortunately, whenever I use Activator.CreateInstance(NullableType) (where NullableType is a created type from user input) I get null as the return value. For example, simply executing:

object Test = Activator.CreateInstance(typeof(Nullable<int>));
Console.WriteLine(Test == null);

returns true. This ONLY happens with Nullables. A struct created in C#, even a generic one, is created fine. Heck, if I copy & paste nullable from DotNetReflector (and remove TypeDependencyAttribute, TargetPatchingOptOutAttribute, and ThrowHelper calls since I don’t have access to those), it shows False above.

What is going on? Is there any other possible way to create a nullable when I don’t know the generic parameters until run-time?

  • 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-27T20:50:44+00:00Added an answer on May 27, 2026 at 8:50 pm

    The problem is in ParameterBuilder.SetConstant, not Activator.CreateInstance. SetConstant is a function for defining the default value of an optional parameter, and requires a concrete value be provided as the constant. For ref classes, null is a valid concrete value, but for value classes prior to the creation of Nullable<>, null was not a valid value. How do you unbox null into an int, for example? So SetConstant checked value types to see if the concrete value passed in as the constant was null and throws an ArgumentException as a more descriptive error than the NullReferenceException you’d get for unboxing a null into a value class.

    In the case of a Nullable<>, null is now a valid concrete value for a value class. Nullable<> itself is a value class, as seen with Activator.CreateInstance, and unboxing null into a Nullable<int> has meaning. This is where SetConstant has a bug: it fails to take this into account and throws a ‘descriptive’ error for what isn’t actually an error. In lieu of a bugfix from Microsoft, any call to SetConstant with a null Nullable<> will have to implement the behavior that’s guarded by the incorrect conditional. This means digging into ParameterBuilder‘s private methods and fields using reflection. Here’s the code I made to handle just this case. The standard SetConstant function should be used in situations that don’t manifest the bug.

    //ModuleBuilder module : a parameter passed into the containing function, the module which is being built (which contains the method that has the optional parameter we are setting the constant for)
    //ParameterBuilder optParam : A parameter passed into the containing function, the parameter that is being built which is to have the null default value.
    MethodInfo method = typeof(TypeBuilder).GetMethods(BindingFlags.Static | BindingFlags.NonPublic)
        .Where(m => m.Name == "SetConstantValue" && m.GetParameters().Length > 0 && m.GetParameters()[0].ParameterType.Name == "RuntimeModule")
        .Single();
    var RuntimeHandle = typeof(ModuleBuilder).GetMethod("GetNativeHandle", BindingFlags.NonPublic | BindingFlags.Instance);
    method.Invoke(null, new object[]
    {
        RuntimeHandle.Invoke(module, new object[]{}),
        optParam.GetToken().Token,
        0x12,
        null
    });
    

    I have reported the bug to Microsoft. They responded that it wouldn’t be fixed in .NET 3.5, but it was added to the internal bug database.

    UPDATE:

    The bug has been fixed in .NET 4.0. ParameterBuilder.SetConstant now has a branch to the constant == null conditional that checks to see if a value type is a generic derived from Nullable<> and only throws the exception if it is not.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

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.