Merge pull request #495 from nicolas-raoul/fix-for-issue2907-with-no-formatting-changes
[mono.git] / mcs / class / corlib / System.Reflection.Emit / FieldBuilder.cs
old mode 100755 (executable)
new mode 100644 (file)
index 200f1cb..053d8c8
@@ -31,6 +31,7 @@
 // (C) 2001-2002 Ximian, Inc.  http://www.ximian.com
 //
 
+#if !FULL_AOT_RUNTIME
 using System;
 using System.Reflection;
 using System.Reflection.Emit;
@@ -39,7 +40,13 @@ using System.Runtime.CompilerServices;
 using System.Runtime.InteropServices;
 
 namespace System.Reflection.Emit {
-       public sealed class FieldBuilder : FieldInfo {
+       [ComVisible (true)]
+       [ComDefaultInterface (typeof (_FieldBuilder))]
+       [ClassInterface (ClassInterfaceType.None)]
+       [StructLayout (LayoutKind.Sequential)]
+       public sealed class FieldBuilder : FieldInfo, _FieldBuilder {
+       
+#pragma warning disable 169, 414
                private FieldAttributes attrs;
                private Type type;
                private String name;
@@ -53,8 +60,13 @@ namespace System.Reflection.Emit {
                private RuntimeFieldHandle handle;
                private Type[] modReq;
                private Type[] modOpt;
+#pragma warning restore 169, 414
+
+               internal FieldBuilder (TypeBuilder tb, string fieldName, Type type, FieldAttributes attributes, Type[] modReq, Type[] modOpt)
+               {
+                       if (type == null)
+                               throw new ArgumentNullException ("type");
 
-               internal FieldBuilder (TypeBuilder tb, string fieldName, Type type, FieldAttributes attributes, Type[] modReq, Type[] modOpt) {
                        attrs = attributes;
                        name = fieldName;
                        this.type = type;
@@ -63,6 +75,8 @@ namespace System.Reflection.Emit {
                        offset = -1;
                        typeb = tb;
                        table_idx = tb.get_next_table_index (this, 0x04, true);
+
+                       ((ModuleBuilder) tb.Module).RegisterToken (this, GetToken ().Token);
                }
 
                public override FieldAttributes Attributes {
@@ -92,15 +106,26 @@ namespace System.Reflection.Emit {
                }
 
                public override object[] GetCustomAttributes(bool inherit) {
-                       throw CreateNotSupportedException ();
+                       /*
+                        * On MS.NET, this always returns not_supported, but we can't do this
+                        * since there would be no way to obtain custom attributes of 
+                        * dynamically created ctors.
+                        */
+                       if (typeb.is_created)
+                               return MonoCustomAttrs.GetCustomAttributes (this, inherit);
+                       else
+                               throw CreateNotSupportedException ();
                }
 
                public override object[] GetCustomAttributes(Type attributeType, bool inherit) {
-                       throw CreateNotSupportedException ();
+                       if (typeb.is_created)
+                               return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
+                       else
+                               throw CreateNotSupportedException ();
                }
 
                public FieldToken GetToken() {
-                       return new FieldToken (0x04000000 | table_idx);
+                       return new FieldToken (MetadataToken);
                }
 
                public override object GetValue(object obj) {
@@ -142,6 +167,9 @@ namespace System.Reflection.Emit {
                        } else if (attrname == "System.NonSerializedAttribute") {
                                attrs |= FieldAttributes.NotSerialized;
                                return;
+                       } else if (attrname == "System.Runtime.CompilerServices.SpecialNameAttribute") {
+                               attrs |= FieldAttributes.SpecialName;
+                               return;
                        } else if (attrname == "System.Runtime.InteropServices.MarshalAsAttribute") {
                                attrs |= FieldAttributes.HasFieldMarshal;
                                marshal_info = CustomAttributeBuilder.get_umarshal (customBuilder, true);
@@ -159,11 +187,13 @@ namespace System.Reflection.Emit {
                        }
                }
 
+               [ComVisible (true)]
                public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
                        RejectIfCreated ();
                        SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
                }
 
+               [Obsolete ("An alternate API is available: Emit the MarshalAs custom attribute instead.")]
                public void SetMarshal( UnmanagedMarshal unmanagedMarshal) {
                        RejectIfCreated ();
                        marshal_info = unmanagedMarshal;
@@ -190,12 +220,32 @@ namespace System.Reflection.Emit {
                                throw new InvalidOperationException ("Unable to change after type has been created.");
                }
 
-#if NET_2_0 || BOOTSTRAP_NET_2_0
-               public override FieldInfo Mono_GetGenericFieldDefinition ()
+               public override Module Module {
+                       get {
+                               return base.Module;
+                       }
+               }
+
+               void _FieldBuilder.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
                {
-                       return this;
+                       throw new NotImplementedException ();
+               }
+
+               void _FieldBuilder.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               void _FieldBuilder.GetTypeInfoCount (out uint pcTInfo)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               void _FieldBuilder.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
+               {
+                       throw new NotImplementedException ();
                }
-#endif
        }
 }
 
+#endif