[sre] ConstructorBuilder.DefineParamter(0,...) shouldn't throw
authorAleksey Kliger <aleksey@xamarin.com>
Thu, 3 Aug 2017 22:18:59 +0000 (18:18 -0400)
committerAleksey Kliger <aleksey@xamarin.com>
Fri, 4 Aug 2017 20:38:23 +0000 (16:38 -0400)
.NET Framework says:

> If you specify 0 (zero) for iSequence, this method returns a ParameterBuilder
>  instead of throwing an exception. There is nothing useful that you can do
>  with this ParameterBuilder.

So let's do that.

mcs/class/corlib/System.Reflection.Emit/ConstructorBuilder.cs
mcs/class/corlib/Test/System.Reflection.Emit/ConstructorBuilderTest.cs

index 55a392d38442814419e937fdb4088f2f3bd8b526..7b77a70194e0ba6b535e858fb3bb269108e36d57 100644 (file)
@@ -235,7 +235,10 @@ namespace System.Reflection.Emit {
 
                public ParameterBuilder DefineParameter (int iSequence, ParameterAttributes attributes, string strParamName)
                {
-                       if (iSequence < 1 || iSequence > GetParametersCount ())
+                       // The 0th ParameterBuilder does not correspond to an
+                       // actual parameter, but .NETFramework lets you define
+                       // it anyway. It is not useful.
+                       if (iSequence < 0 || iSequence > GetParametersCount ())
                                throw new ArgumentOutOfRangeException ("iSequence");
                        if (type.is_created)
                                throw not_after_created ();
index faaa55af818011dc11a4366367aa83c9d24ef25a..57e86fa647ce6d719139ba416d80b0295e0e28f8 100644 (file)
@@ -195,23 +195,20 @@ public class ConstructorBuilderTest
        }
 
        [Test]
-       [Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=341439
        public void DefineParameter_Position_Zero ()
        {
+               // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=341439
+               // https://msdn.microsoft.com/en-us/library/system.reflection.emit.constructorbuilder.defineparameter(v=vs.110).aspx
+               // "If you specify 0 (zero) for iSequence, this method returns
+               // a ParameterBuilder instead of throwing an exception. There
+               // is nothing useful that you can do with this
+               // ParameterBuilder."
+
                ConstructorBuilder cb = genClass.DefineConstructor (
                        0, 0, new Type [2] { typeof (int), typeof (int) });
 
-               try {
-                       cb.DefineParameter (0, ParameterAttributes.In, "param1");
-                       Assert.Fail ("#1");
-               } catch (ArgumentOutOfRangeException ex) {
-                       // Specified argument was out of the range of valid values
-                       Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
-                       Assert.IsNull (ex.ActualValue, "#3");
-                       Assert.IsNull (ex.InnerException, "#4");
-                       Assert.IsNotNull (ex.Message, "#5");
-                       Assert.IsNotNull (ex.ParamName, "#6");
-               }
+               var pb = cb.DefineParameter (0, ParameterAttributes.In, "param1");
+               Assert.IsNotNull (pb);
        }
 
        [Test]