Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem is in <code>ParameterBuilder.SetConstant</code>, not <code>Activator.CreateInstance</code>. <code>SetConstant</code> 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, <code>null</code> is a valid concrete value, but for value classes prior to the creation of <code>Nullable&lt;&gt;</code>, <code>null</code> was not a valid value. How do you unbox <code>null</code> into an <code>int</code>, for example? So <code>SetConstant</code> checked value types to see if the concrete value passed in as the constant was <code>null</code> and throws an <code>ArgumentException</code> as a more descriptive error than the <code>NullReferenceException</code> you'd get for unboxing a null into a value class.</p> <p>In the case of a <code>Nullable&lt;&gt;</code>, <code>null</code> is now a valid concrete value for a value class. <code>Nullable&lt;&gt;</code> itself is a value class, as seen with <code>Activator.CreateInstance</code>, and unboxing <code>null</code> into a <code>Nullable&lt;int&gt;</code> has meaning. This is where <code>SetConstant</code> 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 <code>SetConstant</code> with a null <code>Nullable&lt;&gt;</code> will have to implement the behavior that's guarded by the incorrect conditional. This means digging into <code>ParameterBuilder</code>'s private methods and fields using reflection. Here's the code I made to handle <em>just</em> this case. The standard <code>SetConstant</code> function should be used in situations that don't manifest the bug.</p> <pre><code>//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 =&gt; m.Name == "SetConstantValue" &amp;&amp; m.GetParameters().Length &gt; 0 &amp;&amp; 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 }); </code></pre> <p>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.</p> <p><strong>UPDATE:</strong></p> <p>The bug has been fixed in .NET 4.0. <code>ParameterBuilder.SetConstant</code> now has a branch to the <code>constant == null</code> conditional that checks to see if a value type is a generic derived from <code>Nullable&lt;&gt;</code> and only throws the exception if it is not.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload