2007-11-14 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / corlib / System.Reflection.Emit / GenericTypeParameterBuilder.cs
index daf50bbc95e13a982040599db9d6a8485aa548f6..5483f01560f4ab253835eff732bb57aa8d588576 100644 (file)
@@ -6,16 +6,41 @@
 // (C) 2004 Novell, Inc.
 //
 
+//
+// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
 using System.Reflection;
 using System.Reflection.Emit;
 using System.Collections;
 using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
 using System.Globalization;
 using System.Runtime.Serialization;
 
-#if NET_1_2
+#if NET_2_0 || BOOTSTRAP_NET_2_0
 namespace System.Reflection.Emit
 {
+       [ComVisible (true)]
        public sealed class GenericTypeParameterBuilder : Type
        {
        #region Sync with reflection.h
@@ -25,6 +50,8 @@ namespace System.Reflection.Emit
                private int index;
                private Type base_type;
                private Type[] iface_constraints;
+               private CustomAttributeBuilder[] cattrs;
+               private GenericParameterAttributes attrs;
        #endregion
 
                public void SetBaseTypeConstraint (Type base_type_constraint)
@@ -32,11 +59,17 @@ namespace System.Reflection.Emit
                        this.base_type = base_type_constraint;
                }
 
-               public void SetInterfaceConstraints (Type[] iface_constraints)
+               [ComVisible (true)]
+               public void SetInterfaceConstraints (params Type[] iface_constraints)
                {
                        this.iface_constraints = iface_constraints;
                }
 
+               public void SetGenericParameterAttributes (GenericParameterAttributes attrs)
+               {
+                       this.attrs = attrs;
+               }
+
                internal GenericTypeParameterBuilder (TypeBuilder tbuilder,
                                                      MethodBuilder mbuilder,
                                                      string name, int index)
@@ -62,7 +95,7 @@ namespace System.Reflection.Emit
 
                protected override TypeAttributes GetAttributeFlagsImpl ()
                {
-                       return TypeAttributes.Class;
+                       return TypeAttributes.Public;
                }
 
                protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr,
@@ -182,8 +215,6 @@ namespace System.Reflection.Emit
 
                protected override bool IsValueTypeImpl ()
                {
-#warning "FIXME"
-                       return false;
                        return base_type != null ? base_type.IsValueType : false;
                }
                
@@ -202,7 +233,7 @@ namespace System.Reflection.Emit
 
                public override Type UnderlyingSystemType {
                        get {
-                               return null;
+                               return this;
                        }
                }
 
@@ -258,13 +289,11 @@ namespace System.Reflection.Emit
                }
 
                public override Type DeclaringType {
-                       get { return mbuilder != null ? null : tbuilder; }
+                       get { return mbuilder != null ? mbuilder.DeclaringType : tbuilder; }
                }
 
                public override Type ReflectedType {
-                       get {
-                               return DeclaringType;
-                       }
+                       get { return DeclaringType; }
                }
 
                public override RuntimeTypeHandle TypeHandle {
@@ -276,17 +305,18 @@ namespace System.Reflection.Emit
                        throw not_supported ();
                }
 
-               public override Type GetGenericTypeDefinition ()
+               public override Type[] GetGenericArguments ()
                {
                        throw not_supported ();
                }
 
-               public override bool HasGenericArguments {
-                       get { return false; }
+               public override Type GetGenericTypeDefinition ()
+               {
+                       throw not_supported ();
                }
 
                public override bool ContainsGenericParameters {
-                       get { return false; }
+                       get { return true; }
                }
 
                public override bool IsGenericParameter {
@@ -297,10 +327,50 @@ namespace System.Reflection.Emit
                        get { return index; }
                }
 
-               public override MethodInfo DeclaringMethod {
+               public override GenericParameterAttributes GenericParameterAttributes {
+                       get {
+                               return attrs;
+                       }
+               }
+
+               public override Type[] GetGenericParameterConstraints ()
+               {
+                       if (base_type == null) {
+                               if (iface_constraints != null)
+                                       return iface_constraints;
+
+                               return Type.EmptyTypes;
+                       }
+
+                       if (iface_constraints == null)
+                               return new Type[] { base_type };
+
+                       Type[] ret = new Type [iface_constraints.Length + 1];
+                       ret [0] = base_type;
+                       iface_constraints.CopyTo (ret, 1);
+                       return ret;
+               }
+
+               public override MethodBase DeclaringMethod {
                        get { return mbuilder; }
                }
 
+               public void SetCustomAttribute (CustomAttributeBuilder customBuilder)
+               {
+                       if (customBuilder == null)
+                               throw new ArgumentNullException ("customBuilder");
+
+                       if (cattrs != null) {
+                               CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
+                               cattrs.CopyTo (new_array, 0);
+                               new_array [cattrs.Length] = customBuilder;
+                               cattrs = new_array;
+                       } else {
+                               cattrs = new CustomAttributeBuilder [1];
+                               cattrs [0] = customBuilder;
+                       }
+               }
+
                private Exception not_supported ()
                {
                        return new NotSupportedException ();