2005-06-14 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / corlib / System.Reflection.Emit / CustomAttributeBuilder.cs
index d3df16f9ef45e5fc6634e37dd65f46d35b30bce8..2c9f81df2d162831254d59a162b08fd0eb69e84e 100644 (file)
@@ -38,6 +38,11 @@ using System.Runtime.CompilerServices;
 using System.Runtime.InteropServices;
 
 namespace System.Reflection.Emit {
+#if NET_2_0
+       [ComVisible (true)]
+       [ClassInterfaceAttribute (ClassInterfaceType.None)]
+       [ComDefaultInterfaceAttribute (typeof (_CustomAttributeBuilder))]
+#endif
        public class CustomAttributeBuilder {
                ConstructorInfo ctor;
                byte[] data;
@@ -294,5 +299,91 @@ namespace System.Reflection.Emit {
                        }
                }
 
+               static object decode_cattr_value (Type t, byte[] data, int pos, out int rpos) {
+                       switch (Type.GetTypeCode (t)) {
+                       case TypeCode.String:
+                               int len = decode_len (data, pos, out pos);
+                               rpos = pos + len;
+                               return string_from_bytes (data, pos, len);
+                       case TypeCode.Int32:
+                               rpos = pos + 4;
+                               return data [pos] + (data [pos + 1] << 8) + (data [pos + 2] << 16) + (data [pos + 3] << 24);
+                       case TypeCode.Boolean:
+                               rpos = pos + 1;
+                               return (data [pos] == 0) ? false : true;
+                       default:
+                               throw new Exception ("FIXME: Type " + t + " not yet handled in decode_cattr_value.");
+                       }
+               }
+
+               internal struct CustomAttributeInfo {
+                       public ConstructorInfo ctor;
+                       public object[] ctorArgs;
+                       public string[] namedParamNames;
+                       public object[] namedParamValues;
+               }
+
+               internal static CustomAttributeInfo decode_cattr (CustomAttributeBuilder customBuilder) {
+                       byte[] data = customBuilder.Data;
+                       ConstructorInfo ctor = customBuilder.Ctor;
+                       int pos = 0;
+
+                       CustomAttributeInfo info = new CustomAttributeInfo ();
+
+                       // Prolog
+                       if (data.Length < 2)
+                               throw new Exception ();
+                       if ((data [0] != 0x1) || (data [1] != 0x00))
+                               throw new Exception ();
+                       pos = 2;
+
+                       ParameterInfo [] pi = ctor.GetParameters ();
+                       info.ctor = ctor;
+                       info.ctorArgs = new object [pi.Length];
+                       for (int i = 0; i < pi.Length; ++i)
+                               info.ctorArgs [i] = decode_cattr_value (pi [i].ParameterType, data, pos, out pos);
+
+                       int num_named = data [pos] + (data [pos + 1] * 256);
+                       pos += 2;
+
+                       info.namedParamNames = new string [num_named];
+                       info.namedParamValues = new object [num_named];
+                       for (int i = 0; i < num_named; ++i) {
+                               int named_type = data [pos++];
+                               int data_type = data [pos++];
+                               string enum_type_name = null;
+
+                               if (data_type == 0x55) {
+                                       int len2 = decode_len (data, pos, out pos);
+                                       enum_type_name = string_from_bytes (data, pos, len2);
+                                       pos += len2;
+                               }
+
+                               int len = decode_len (data, pos, out pos);
+                               string name = string_from_bytes (data, pos, len);
+                               info.namedParamNames [i] = name;
+                               pos += len;
+
+                               if (named_type == 0x53) {
+                                       /* Field */
+                                       FieldInfo fi = ctor.DeclaringType.GetField (name, BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Instance);
+                                       if (fi == null)
+                                               throw new Exception ("Custom attribute type '" + ctor.DeclaringType + "' doesn't contain a field named '" + name + "'");
+
+                                       object val = decode_cattr_value (fi.FieldType, data, pos, out pos);
+                                       if (enum_type_name != null) {
+                                               Type enumType = Type.GetType (enum_type_name);
+                                               val = Enum.ToObject (enumType, val);
+                                       }
+
+                                       info.namedParamValues [i] = val;
+                               }
+                               else
+                                       // FIXME:
+                                       throw new Exception ();
+                       }
+
+                       return info;
+               }
        }
 }