Merge pull request #495 from nicolas-raoul/fix-for-issue2907-with-no-formatting-changes
[mono.git] / mcs / class / corlib / System.Reflection.Emit / ParameterBuilder.cs
index 2832768238a5763903d68b2bda0bdc027e471c08..97f998051304136cc366fea080def4168c65dd44 100644 (file)
@@ -32,6 +32,7 @@
 // (C) 2001 Ximian, Inc.  http://www.ximian.com
 //
 
+#if !FULL_AOT_RUNTIME
 using System;
 using System.Reflection;
 using System.Reflection.Emit;
@@ -40,12 +41,13 @@ using System.Runtime.CompilerServices;
 using System.Runtime.InteropServices;
 
 namespace System.Reflection.Emit {
-#if NET_2_0
        [ComVisible (true)]
        [ComDefaultInterface (typeof (_ParameterBuilder))]
-#endif
        [ClassInterface (ClassInterfaceType.None)]
+       [StructLayout (LayoutKind.Sequential)]
        public class ParameterBuilder : _ParameterBuilder {
+
+#pragma warning disable 169, 414
                private MethodBase methodb; /* MethodBuilder, ConstructorBuilder or DynamicMethod */
                private string name;
                private CustomAttributeBuilder[] cattrs;
@@ -54,17 +56,16 @@ namespace System.Reflection.Emit {
                private int position;
                private int table_idx;
                object def_value;
+#pragma warning restore 169, 414
                
                internal ParameterBuilder (MethodBase mb, int pos, ParameterAttributes attributes, string strParamName) {
                        name = strParamName;
                        position = pos;
                        attrs = attributes;
                        methodb = mb;
-#if NET_2_0
                        if (mb is DynamicMethod)
                                table_idx = 0;
                        else
-#endif
                                table_idx = mb.get_next_table_index (this, 0x08, true);
                }
 
@@ -93,6 +94,16 @@ namespace System.Reflection.Emit {
 
                public virtual void SetConstant (object defaultValue)
                {
+                       if (position > 0) {
+                               Type t = methodb.GetParameterType (position - 1);
+                               if (defaultValue != null && t != defaultValue.GetType ()) {
+                                       if(!t.IsEnum || t.UnderlyingSystemType != defaultValue.GetType ())
+                                               throw new ArgumentException ("Constant does not match the defined type.");
+                               }
+                               if (t.IsValueType && !t.IsPrimitive && !t.IsEnum && t != typeof (DateTime))
+                                       throw new ArgumentException ("" + t + " is not a supported constant type.");
+                       }
+
                        def_value = defaultValue;
                        attrs |= ParameterAttributes.HasDefault;
                }
@@ -110,7 +121,7 @@ namespace System.Reflection.Emit {
                                return;
                        } else if (attrname == "System.Runtime.InteropServices.MarshalAsAttribute") {
                                attrs |= ParameterAttributes.HasFieldMarshal;
-                               marshal_info = CustomAttributeBuilder.get_umarshal (customBuilder, true);
+                               marshal_info = CustomAttributeBuilder.get_umarshal (customBuilder, false);
                                /* FIXME: check for errors */
                                return;
                        } else if (attrname == "System.Runtime.InteropServices.DefaultParameterValueAttribute") {
@@ -132,16 +143,12 @@ namespace System.Reflection.Emit {
                        }
                }
 
-#if NET_2_0
                [ComVisible (true)]
-#endif
                public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
                        SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
                }
 
-#if NET_2_0
                [Obsolete ("An alternate API is available: Emit the MarshalAs custom attribute instead.")]
-#endif
                public virtual void SetMarshal( UnmanagedMarshal unmanagedMarshal) {
                        marshal_info = unmanagedMarshal;
                        attrs |= ParameterAttributes.HasFieldMarshal;
@@ -169,3 +176,4 @@ namespace System.Reflection.Emit {
        }
 }
 
+#endif