Bug 7912 - Type.Missing can't be used to when invoking a constructor with optional parameters
Summary: Type.Missing can't be used to when invoking a constructor with optional param...
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib ()
Version: unspecified
Hardware: PC Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2012-10-19 16:30 UTC by Jonathan Pryor
Modified: 2012-10-22 10:37 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Jonathan Pryor 2012-10-19 16:30:16 UTC
Context: http://forums.xamarin.com/discussion/155/reflection-calling-constructor-with-optional-parameters-using-type-missing

Type.Missing can't be used to when invoking a constructor with optional parameters

Example code:

  using System;
  using System.Linq;
  using System.Globalization;
  using System.Reflection;
  
  namespace Demo {
    public class StringElement {
      public StringElement(string caption = null, string value = null,
          string layoutName = null)
      {
        Value = value;
      }

      public string Value {get; private set;}
    }

    class Test {
      public static void Main ()
      {
        var constructor = typeof(StringElement).GetConstructors()
          .FirstOrDefault(c => c.GetParameters().All(p => p.IsOptional));
        if (constructor == null)
          throw new ArgumentException("No parameterless Constructor found");
        var parameters = constructor.GetParameters()
          .Select(p => (object)Type.Missing).ToArray();
        var instance = constructor.Invoke(
            BindingFlags.OptionalParamBinding | BindingFlags.InvokeMethod |
              BindingFlags.CreateInstance,
            null,
            parameters,
            CultureInfo.InvariantCulture);
      }
    }
  }

Run on mono:
> $ dmcs se.cs
> $ mono se.exe 
> 
> Unhandled Exception: System.ArgumentException: failed to convert parameters
>   at System.Reflection.MonoCMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0 
>   at System.Reflection.MonoCMethod.Invoke (BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0 
>   at Demo.Test.Main () [0x00000] in <filename unknown>:0 
> [ERROR] FATAL UNHANDLED EXCEPTION: System.ArgumentException: failed to convert parameters
>   at System.Reflection.MonoCMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0 
>   at System.Reflection.MonoCMethod.Invoke (BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0 
>   at Demo.Test.Main () [0x00000] in <filename unknown>:0 

Run on .NET:

  Z:\tmp\csharp>csc se.cs
  Microsoft (R) Visual C# Compiler version 4.0.30319.17626
  for Microsoft (R) .NET Framework 4.5
  Copyright (C) Microsoft Corporation. All rights reserved.

  Z:\tmp\csharp>se.exe

Note the lack of an error on .NET.
Comment 1 Marek Safar 2012-10-22 10:37:31 UTC
Fixed in master