2 // mono-api-info.cs - Dumps public assembly information to an xml file.
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // Copyright (C) 2003-2008 Novell, Inc (http://www.novell.com)
11 using System.Collections;
12 using System.Collections.Generic;
13 using System.Globalization;
14 using System.Runtime.InteropServices;
15 using System.Security.Permissions;
26 public static int Main (string [] args)
31 AssemblyCollection acoll = new AssemblyCollection ();
33 foreach (string fullName in args) {
37 XmlDocument doc = new XmlDocument ();
41 var writer = new WellFormedXmlWriter (new XmlTextWriter (Console.Out) { Formatting = Formatting.Indented });
42 XmlNode decl = doc.CreateXmlDeclaration ("1.0", "utf-8", null);
43 doc.InsertBefore (decl, doc.DocumentElement);
51 public static string CleanupTypeName (TypeReference type)
53 return CleanupTypeName (type.FullName);
56 static string CleanupTypeName (string t)
58 return t.Replace ('<', '[').Replace ('>', ']').Replace ('/', '+');
62 class AssemblyCollection
65 List<AssemblyDefinition> assemblies = new List<AssemblyDefinition> ();
67 public AssemblyCollection ()
71 public bool Add (string name)
73 AssemblyDefinition ass = LoadAssembly (name);
81 public void DoOutput ()
84 throw new InvalidOperationException ("Document not set");
86 XmlNode nassemblies = document.CreateElement ("assemblies", null);
87 document.AppendChild (nassemblies);
88 foreach (AssemblyDefinition a in assemblies) {
89 AssemblyData data = new AssemblyData (document, nassemblies, a);
94 public XmlDocument Document {
95 set { document = value; }
98 AssemblyDefinition LoadAssembly (string assembly)
101 return TypeHelper.Resolver.Resolve (assembly);
108 abstract class BaseData
110 protected XmlDocument document;
111 protected XmlNode parent;
113 protected BaseData (XmlDocument doc, XmlNode parent)
116 this.parent = parent;
119 public abstract void DoOutput ();
121 protected void AddAttribute (XmlNode node, string name, string value)
123 XmlAttribute attr = document.CreateAttribute (name);
125 node.Attributes.Append (attr);
129 class AssemblyData : BaseData
131 AssemblyDefinition ass;
133 public AssemblyData (XmlDocument document, XmlNode parent, AssemblyDefinition ass)
134 : base (document, parent)
139 public override void DoOutput ()
141 if (document == null)
142 throw new InvalidOperationException ("Document not set");
144 XmlNode nassembly = document.CreateElement ("assembly", null);
145 AssemblyNameDefinition aname = ass.Name;
146 AddAttribute (nassembly, "name", aname.Name);
147 AddAttribute (nassembly, "version", aname.Version.ToString ());
148 parent.AppendChild (nassembly);
149 AttributeData.OutputAttributes (document, nassembly, ass.CustomAttributes);
150 TypeDefinitionCollection typesCollection = ass.MainModule.Types;
151 if (typesCollection == null || typesCollection.Count == 0)
153 object [] typesArray = new object [typesCollection.Count];
154 for (int i = 0; i < typesCollection.Count; i++) {
155 typesArray [i] = typesCollection [i];
157 Array.Sort (typesArray, TypeReferenceComparer.Default);
159 XmlNode nss = document.CreateElement ("namespaces", null);
160 nassembly.AppendChild (nss);
162 string current_namespace = "$%&$&";
164 XmlNode classes = null;
165 foreach (TypeDefinition t in typesArray) {
166 if (string.IsNullOrEmpty (t.Namespace))
169 if ((t.Attributes & TypeAttributes.VisibilityMask) != TypeAttributes.Public)
172 if (t.DeclaringType != null)
173 continue; // enforce !nested
175 if (t.Namespace != current_namespace) {
176 current_namespace = t.Namespace;
177 ns = document.CreateElement ("namespace", null);
178 AddAttribute (ns, "name", current_namespace);
179 nss.AppendChild (ns);
180 classes = document.CreateElement ("classes", null);
181 ns.AppendChild (classes);
184 TypeData bd = new TypeData (document, classes, t);
190 abstract class MemberData : BaseData
192 MemberReference [] members;
194 public MemberData (XmlDocument document, XmlNode parent, MemberReference [] members)
195 : base (document, parent)
197 this.members = members;
200 public override void DoOutput ()
202 XmlNode mclass = document.CreateElement (ParentTag, null);
203 parent.AppendChild (mclass);
205 foreach (MemberReference member in members) {
206 XmlNode mnode = document.CreateElement (Tag, null);
207 mclass.AppendChild (mnode);
208 AddAttribute (mnode, "name", GetName (member));
209 if (!NoMemberAttributes)
210 AddAttribute (mnode, "attrib", GetMemberAttributes (member));
212 AttributeData.OutputAttributes (document, mnode, GetCustomAttributes (member));
214 AddExtraData (mnode, member);
219 protected abstract CustomAttributeCollection GetCustomAttributes (MemberReference member);
221 protected virtual void AddExtraData (XmlNode p, MemberReference memberDefenition)
225 protected virtual string GetName (MemberReference memberDefenition)
230 protected virtual string GetMemberAttributes (MemberReference memberDefenition)
235 public virtual bool NoMemberAttributes {
236 get { return false; }
240 public virtual string ParentTag {
241 get { return "NoPARENTTAG"; }
244 public virtual string Tag {
245 get { return "NoTAG"; }
248 public static void OutputGenericParameters (XmlDocument document, XmlNode nclass, IGenericParameterProvider provider)
250 if (provider.GenericParameters.Count == 0)
253 var gparameters = provider.GenericParameters;
255 XmlElement ngeneric = document.CreateElement (string.Format ("generic-parameters"));
256 nclass.AppendChild (ngeneric);
258 foreach (GenericParameter gp in gparameters) {
259 XmlElement nparam = document.CreateElement (string.Format ("generic-parameter"));
260 nparam.SetAttribute ("name", gp.Name);
261 nparam.SetAttribute ("attributes", ((int) gp.Attributes).ToString ());
263 ngeneric.AppendChild (nparam);
265 var constraints = gp.Constraints;
266 if (constraints.Count == 0)
269 XmlElement nconstraint = document.CreateElement ("generic-parameter-constraints");
271 foreach (TypeReference constraint in constraints) {
272 XmlElement ncons = document.CreateElement ("generic-parameter-constraint");
273 ncons.SetAttribute ("name", Utils.CleanupTypeName (constraint));
274 nconstraint.AppendChild (ncons);
277 nparam.AppendChild (nconstraint);
282 class TypeData : MemberData
286 public TypeData (XmlDocument document, XmlNode parent, TypeDefinition type)
287 : base (document, parent, null)
292 protected override CustomAttributeCollection GetCustomAttributes (MemberReference member) {
293 return ((TypeDefinition) member).CustomAttributes;
296 public override void DoOutput ()
298 if (document == null)
299 throw new InvalidOperationException ("Document not set");
301 XmlNode nclass = document.CreateElement ("class", null);
302 AddAttribute (nclass, "name", type.Name);
303 string classType = GetClassType (type);
304 AddAttribute (nclass, "type", classType);
306 if (type.BaseType != null)
307 AddAttribute (nclass, "base", Utils.CleanupTypeName (type.BaseType));
310 AddAttribute (nclass, "sealed", "true");
313 AddAttribute (nclass, "abstract", "true");
315 if ( (type.Attributes & TypeAttributes.Serializable) != 0 || type.IsEnum)
316 AddAttribute (nclass, "serializable", "true");
318 string charSet = GetCharSet (type);
319 AddAttribute (nclass, "charset", charSet);
321 string layout = GetLayout (type);
323 AddAttribute (nclass, "layout", layout);
325 parent.AppendChild (nclass);
327 AttributeData.OutputAttributes (document, nclass, GetCustomAttributes(type));
329 XmlNode ifaces = null;
331 foreach (TypeReference iface in TypeHelper.GetInterfaces (type)) {
332 if (!TypeHelper.IsPublic (iface))
333 // we're only interested in public interfaces
336 if (ifaces == null) {
337 ifaces = document.CreateElement ("interfaces", null);
338 nclass.AppendChild (ifaces);
341 XmlNode iface_node = document.CreateElement ("interface", null);
342 AddAttribute (iface_node, "name", Utils.CleanupTypeName (iface));
343 ifaces.AppendChild (iface_node);
346 MemberData.OutputGenericParameters (document, nclass, type);
348 ArrayList members = new ArrayList ();
350 FieldDefinition [] fields = GetFields (type);
351 if (fields.Length > 0) {
352 Array.Sort (fields, MemberReferenceComparer.Default);
353 FieldData fd = new FieldData (document, nclass, fields);
354 // Special case for enum fields
355 // TODO:Special case for enum fields
356 //if (classType == "enum") {
357 // string etype = fields [0].GetType ().ToString ();
358 // AddAttribute (nclass, "enumtype", etype);
363 MethodDefinition [] ctors = GetConstructors (type);
364 if (ctors.Length > 0) {
365 Array.Sort (ctors, MemberReferenceComparer.Default);
366 members.Add (new ConstructorData (document, nclass, ctors));
369 PropertyDefinition[] properties = GetProperties (type);
370 if (properties.Length > 0) {
371 Array.Sort (properties, MemberReferenceComparer.Default);
372 members.Add (new PropertyData (document, nclass, properties));
375 EventDefinition [] events = GetEvents (type);
376 if (events.Length > 0) {
377 Array.Sort (events, MemberReferenceComparer.Default);
378 members.Add (new EventData (document, nclass, events));
381 MethodDefinition [] methods = GetMethods (type);
382 if (methods.Length > 0) {
383 Array.Sort (methods, MemberReferenceComparer.Default);
384 members.Add (new MethodData (document, nclass, methods));
387 foreach (MemberData md in members)
390 NestedTypeCollection nested = type.NestedTypes;
391 //remove non public(familiy) and nested in second degree
392 for (int i = nested.Count - 1; i >= 0; i--) {
393 TypeDefinition t = nested [i];
394 if ((t.Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic ||
395 (t.Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamily ||
396 (t.Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamORAssem) {
398 if (t.DeclaringType == type)
399 continue; // not nested of nested
406 if (nested.Count > 0) {
407 XmlNode classes = document.CreateElement ("classes", null);
408 nclass.AppendChild (classes);
409 foreach (TypeDefinition t in nested) {
410 TypeData td = new TypeData (document, classes, t);
416 protected override string GetMemberAttributes (MemberReference member)
419 throw new InvalidOperationException ("odd");
421 return ((int) type.Attributes).ToString (CultureInfo.InvariantCulture);
424 public static bool MustDocumentMethod (MethodDefinition method) {
426 MethodAttributes maskedAccess = method.Attributes & MethodAttributes.MemberAccessMask;
427 return maskedAccess == MethodAttributes.Public
428 || maskedAccess == MethodAttributes.Family
429 || maskedAccess == MethodAttributes.FamORAssem;
432 static string GetClassType (TypeDefinition t)
443 if (TypeHelper.IsDelegate(t))
449 static string GetCharSet (TypeDefinition type)
451 TypeAttributes maskedStringFormat = type.Attributes & TypeAttributes.StringFormatMask;
452 if (maskedStringFormat == TypeAttributes.AnsiClass)
453 return CharSet.Ansi.ToString ();
455 if (maskedStringFormat == TypeAttributes.AutoClass)
456 return CharSet.Auto.ToString ();
458 if (maskedStringFormat == TypeAttributes.UnicodeClass)
459 return CharSet.Unicode.ToString ();
461 return CharSet.None.ToString ();
464 static string GetLayout (TypeDefinition type)
466 TypeAttributes maskedLayout = type.Attributes & TypeAttributes.LayoutMask;
467 if (maskedLayout == TypeAttributes.AutoLayout)
468 return LayoutKind.Auto.ToString ();
470 if (maskedLayout == TypeAttributes.ExplicitLayout)
471 return LayoutKind.Explicit.ToString ();
473 if (maskedLayout == TypeAttributes.SequentialLayout)
474 return LayoutKind.Sequential.ToString ();
479 FieldDefinition [] GetFields (TypeDefinition type) {
480 ArrayList list = new ArrayList ();
482 FieldDefinitionCollection fields = type.Fields;
483 foreach (FieldDefinition field in fields) {
484 if (field.IsSpecialName)
487 // we're only interested in public or protected members
488 FieldAttributes maskedVisibility = (field.Attributes & FieldAttributes.FieldAccessMask);
489 if (maskedVisibility == FieldAttributes.Public
490 || maskedVisibility == FieldAttributes.Family
491 || maskedVisibility == FieldAttributes.FamORAssem) {
496 return (FieldDefinition []) list.ToArray (typeof (FieldDefinition));
500 internal static PropertyDefinition [] GetProperties (TypeDefinition type) {
501 ArrayList list = new ArrayList ();
503 PropertyDefinitionCollection properties = type.Properties;//type.GetProperties (flags);
504 foreach (PropertyDefinition property in properties) {
505 MethodDefinition getMethod = property.GetMethod;
506 MethodDefinition setMethod = property.SetMethod;
508 bool hasGetter = (getMethod != null) && MustDocumentMethod (getMethod);
509 bool hasSetter = (setMethod != null) && MustDocumentMethod (setMethod);
511 // if neither the getter or setter should be documented, then
513 if (hasGetter || hasSetter) {
518 return (PropertyDefinition []) list.ToArray (typeof (PropertyDefinition));
521 private MethodDefinition[] GetMethods (TypeDefinition type)
523 ArrayList list = new ArrayList ();
525 MethodDefinitionCollection methods = type.Methods;//type.GetMethods (flags);
526 foreach (MethodDefinition method in methods) {
527 if (method.IsSpecialName && !method.Name.StartsWith ("op_"))
530 // we're only interested in public or protected members
531 if (!MustDocumentMethod(method))
534 if (IsFinalizer (method))
540 return (MethodDefinition []) list.ToArray (typeof (MethodDefinition));
543 static bool IsFinalizer (MethodDefinition method)
545 if (method.Name != "Finalize")
548 if (!method.IsVirtual)
551 if (method.Parameters.Count != 0)
557 private MethodDefinition [] GetConstructors (TypeDefinition type)
559 ArrayList list = new ArrayList ();
561 ConstructorCollection ctors = type.Constructors;//type.GetConstructors (flags);
562 foreach (MethodDefinition constructor in ctors) {
563 // we're only interested in public or protected members
564 if (!MustDocumentMethod(constructor))
567 list.Add (constructor);
570 return (MethodDefinition []) list.ToArray (typeof (MethodDefinition));
573 private EventDefinition[] GetEvents (TypeDefinition type)
575 ArrayList list = new ArrayList ();
577 EventDefinitionCollection events = type.Events;//type.GetEvents (flags);
578 foreach (EventDefinition eventDef in events) {
579 MethodDefinition addMethod = eventDef.AddMethod;//eventInfo.GetAddMethod (true);
581 if (addMethod == null || !MustDocumentMethod (addMethod))
587 return (EventDefinition []) list.ToArray (typeof (EventDefinition));
591 class FieldData : MemberData
593 public FieldData (XmlDocument document, XmlNode parent, FieldDefinition [] members)
594 : base (document, parent, members)
598 protected override CustomAttributeCollection GetCustomAttributes (MemberReference member) {
599 return ((FieldDefinition) member).CustomAttributes;
602 protected override string GetName (MemberReference memberDefenition)
604 FieldDefinition field = (FieldDefinition) memberDefenition;
608 protected override string GetMemberAttributes (MemberReference memberDefenition)
610 FieldDefinition field = (FieldDefinition) memberDefenition;
611 return ((int) field.Attributes).ToString (CultureInfo.InvariantCulture);
614 protected override void AddExtraData (XmlNode p, MemberReference memberDefenition)
616 base.AddExtraData (p, memberDefenition);
617 FieldDefinition field = (FieldDefinition) memberDefenition;
618 AddAttribute (p, "fieldtype", Utils.CleanupTypeName (field.FieldType));
620 if (field.IsLiteral) {
621 object value = field.Constant;//object value = field.GetValue (null);
622 string stringValue = null;
623 //if (value is Enum) {
624 // // FIXME: when Mono bug #60090 has been
625 // // fixed, we should just be able to use
626 // // Convert.ToString
627 // stringValue = ((Enum) value).ToString ("D", CultureInfo.InvariantCulture);
630 stringValue = Convert.ToString (value, CultureInfo.InvariantCulture);
633 if (stringValue != null)
634 AddAttribute (p, "value", stringValue);
638 public override string ParentTag {
639 get { return "fields"; }
642 public override string Tag {
643 get { return "field"; }
647 class PropertyData : MemberData
649 public PropertyData (XmlDocument document, XmlNode parent, PropertyDefinition [] members)
650 : base (document, parent, members)
654 protected override CustomAttributeCollection GetCustomAttributes (MemberReference member) {
655 return ((PropertyDefinition) member).CustomAttributes;
658 protected override string GetName (MemberReference memberDefenition)
660 PropertyDefinition prop = (PropertyDefinition) memberDefenition;
664 protected override void AddExtraData (XmlNode p, MemberReference memberDefenition)
666 base.AddExtraData (p, memberDefenition);
667 PropertyDefinition prop = (PropertyDefinition) memberDefenition;
668 AddAttribute (p, "ptype", Utils.CleanupTypeName (prop.PropertyType));
669 MethodDefinition _get = prop.GetMethod;
670 MethodDefinition _set = prop.SetMethod;
671 bool haveGet = (_get != null && TypeData.MustDocumentMethod(_get));
672 bool haveSet = (_set != null && TypeData.MustDocumentMethod(_set));
673 MethodDefinition [] methods;
675 if (haveGet && haveSet) {
676 methods = new MethodDefinition [] { _get, _set };
677 } else if (haveGet) {
678 methods = new MethodDefinition [] { _get };
679 } else if (haveSet) {
680 methods = new MethodDefinition [] { _set };
686 string parms = Parameters.GetSignature (methods [0].Parameters);
687 AddAttribute (p, "params", parms);
689 MethodData data = new MethodData (document, p, methods);
690 //data.NoMemberAttributes = true;
694 protected override string GetMemberAttributes (MemberReference memberDefenition)
696 PropertyDefinition prop = (PropertyDefinition) memberDefenition;
697 return ((int) prop.Attributes).ToString (CultureInfo.InvariantCulture);
700 public override string ParentTag {
701 get { return "properties"; }
704 public override string Tag {
705 get { return "property"; }
709 class EventData : MemberData
711 public EventData (XmlDocument document, XmlNode parent, EventDefinition [] members)
712 : base (document, parent, members)
716 protected override CustomAttributeCollection GetCustomAttributes (MemberReference member) {
717 return ((EventDefinition) member).CustomAttributes;
720 protected override string GetName (MemberReference memberDefenition)
722 EventDefinition evt = (EventDefinition) memberDefenition;
726 protected override string GetMemberAttributes (MemberReference memberDefenition)
728 EventDefinition evt = (EventDefinition) memberDefenition;
729 return ((int) evt.Attributes).ToString (CultureInfo.InvariantCulture);
732 protected override void AddExtraData (XmlNode p, MemberReference memberDefenition)
734 base.AddExtraData (p, memberDefenition);
735 EventDefinition evt = (EventDefinition) memberDefenition;
736 AddAttribute (p, "eventtype", Utils.CleanupTypeName (evt.EventType));
739 public override string ParentTag {
740 get { return "events"; }
743 public override string Tag {
744 get { return "event"; }
748 class MethodData : MemberData
752 public MethodData (XmlDocument document, XmlNode parent, MethodDefinition [] members)
753 : base (document, parent, members)
757 protected override CustomAttributeCollection GetCustomAttributes (MemberReference member) {
758 return ((MethodDefinition) member).CustomAttributes;
761 protected override string GetName (MemberReference memberDefenition)
763 MethodDefinition method = (MethodDefinition) memberDefenition;
764 string name = method.Name;
765 string parms = Parameters.GetSignature (method.Parameters);
767 return string.Format ("{0}({1})", name, parms);
770 protected override string GetMemberAttributes (MemberReference memberDefenition)
772 MethodDefinition method = (MethodDefinition) memberDefenition;
773 return ((int)( method.Attributes)).ToString (CultureInfo.InvariantCulture);
776 protected override void AddExtraData (XmlNode p, MemberReference memberDefenition)
778 base.AddExtraData (p, memberDefenition);
780 if (!(memberDefenition is MethodDefinition))
783 MethodDefinition mbase = (MethodDefinition) memberDefenition;
785 ParameterData parms = new ParameterData (document, p, mbase.Parameters);
788 if (mbase.IsAbstract)
789 AddAttribute (p, "abstract", "true");
791 AddAttribute (p, "virtual", "true");
793 AddAttribute (p, "static", "true");
795 //if (!(member is MethodInfo))
798 //MethodInfo method = (MethodInfo) member;
799 string rettype = Utils.CleanupTypeName (mbase.ReturnType.ReturnType);
800 if (rettype != "System.Void" || !mbase.IsConstructor)
801 AddAttribute (p, "returntype", (rettype));
803 AttributeData.OutputAttributes (document, p, mbase.ReturnType.CustomAttributes);
805 MemberData.OutputGenericParameters (document, p, mbase);
808 public override bool NoMemberAttributes {
809 get { return noAtts; }
810 set { noAtts = value; }
813 public override string ParentTag {
814 get { return "methods"; }
817 public override string Tag {
818 get { return "method"; }
822 class ConstructorData : MethodData
824 public ConstructorData (XmlDocument document, XmlNode parent, MethodDefinition [] members)
825 : base (document, parent, members)
829 public override string ParentTag {
830 get { return "constructors"; }
833 public override string Tag {
834 get { return "constructor"; }
838 class ParameterData : BaseData
840 private ParameterDefinitionCollection parameters;
842 public ParameterData (XmlDocument document, XmlNode parent, ParameterDefinitionCollection parameters)
843 : base (document, parent)
845 this.parameters = parameters;
848 public override void DoOutput ()
850 XmlNode parametersNode = document.CreateElement ("parameters");
851 parent.AppendChild (parametersNode);
853 foreach (ParameterDefinition parameter in parameters) {
854 XmlNode paramNode = document.CreateElement ("parameter");
855 parametersNode.AppendChild (paramNode);
856 AddAttribute (paramNode, "name", parameter.Name);
857 AddAttribute (paramNode, "position", parameter.Method.Parameters.IndexOf(parameter).ToString(CultureInfo.InvariantCulture));
858 AddAttribute (paramNode, "attrib", ((int) parameter.Attributes).ToString());
860 string direction = "in";
862 if (parameter.ParameterType is ReferenceType)
863 direction = parameter.IsOut ? "out" : "ref";
865 TypeReference t = parameter.ParameterType;
866 AddAttribute (paramNode, "type", Utils.CleanupTypeName (t));
868 if (parameter.IsOptional) {
869 AddAttribute (paramNode, "optional", "true");
870 if (parameter.HasConstant)
871 AddAttribute (paramNode, "defaultValue", parameter.Constant == null ? "NULL" : parameter.Constant.ToString ());
874 if (direction != "in")
875 AddAttribute (paramNode, "direction", direction);
877 AttributeData.OutputAttributes (document, paramNode, parameter.CustomAttributes);
882 class AttributeData : BaseData
884 CustomAttributeCollection atts;
886 AttributeData (XmlDocument doc, XmlNode parent, CustomAttributeCollection attributes)
892 public override void DoOutput ()
894 if (document == null)
895 throw new InvalidOperationException ("Document not set");
897 if (atts == null || atts.Count == 0)
900 XmlNode natts = parent.SelectSingleNode("attributes");
902 natts = document.CreateElement ("attributes", null);
903 parent.AppendChild (natts);
906 for (int i = 0; i < atts.Count; ++i) {
907 CustomAttribute att = atts [i];
915 string attName = Utils.CleanupTypeName (att.Constructor.DeclaringType);
916 if (SkipAttribute (att))
919 XmlNode node = document.CreateElement ("attribute");
920 AddAttribute (node, "name", attName);
922 XmlNode properties = null;
924 Dictionary<string, object> attribute_mapping = CreateAttributeMapping (att);
926 foreach (string name in attribute_mapping.Keys) {
927 if (name == "TypeId")
930 if (properties == null) {
931 properties = node.AppendChild (document.CreateElement ("properties"));
934 object o = attribute_mapping [name];
936 XmlNode n = properties.AppendChild (document.CreateElement ("property"));
937 AddAttribute (n, "name", name);
940 AddAttribute (n, "value", "null");
943 string value = o.ToString ();
944 if (attName.EndsWith ("GuidAttribute"))
945 value = value.ToUpper ();
946 AddAttribute (n, "value", value);
949 natts.AppendChild (node);
953 static Dictionary<string, object> CreateAttributeMapping (CustomAttribute attribute)
955 var mapping = new Dictionary<string, object> ();
957 PopulateMapping (mapping, attribute);
959 var constructor = TypeHelper.Resolver.Resolve (attribute.Constructor);
960 if (constructor == null || constructor.Parameters.Count == 0)
963 PopulateMapping (mapping, constructor, attribute);
968 static void PopulateMapping (Dictionary<string, object> mapping, CustomAttribute attribute)
970 foreach (DictionaryEntry entry in attribute.Properties) {
971 var name = (string) entry.Key;
973 mapping.Add (name, GetArgumentValue (attribute.GetPropertyType (name), entry.Value));
977 static Dictionary<FieldReference, int> CreateArgumentFieldMapping (MethodDefinition constructor)
979 Dictionary<FieldReference, int> field_mapping = new Dictionary<FieldReference, int> ();
981 int? argument = null;
983 foreach (Instruction instruction in constructor.Body.Instructions) {
984 switch (instruction.OpCode.Code) {
996 argument = ((ParameterDefinition) instruction.Operand).Sequence;
1000 FieldReference field = (FieldReference) instruction.Operand;
1001 if (field.DeclaringType.FullName != constructor.DeclaringType.FullName)
1004 if (!argument.HasValue)
1007 if (!field_mapping.ContainsKey (field))
1008 field_mapping.Add (field, (int) argument - 1);
1015 return field_mapping;
1018 static Dictionary<PropertyDefinition, FieldReference> CreatePropertyFieldMapping (TypeDefinition type)
1020 Dictionary<PropertyDefinition, FieldReference> property_mapping = new Dictionary<PropertyDefinition, FieldReference> ();
1022 foreach (PropertyDefinition property in type.Properties) {
1023 if (property.GetMethod == null)
1025 if (!property.GetMethod.HasBody)
1028 foreach (Instruction instruction in property.GetMethod.Body.Instructions) {
1029 if (instruction.OpCode.Code != Code.Ldfld)
1032 FieldReference field = (FieldReference) instruction.Operand;
1033 if (field.DeclaringType.FullName != type.FullName)
1036 property_mapping.Add (property, field);
1041 return property_mapping;
1044 static void PopulateMapping (Dictionary<string, object> mapping, MethodDefinition constructor, CustomAttribute attribute)
1046 if (!constructor.HasBody)
1049 var field_mapping = CreateArgumentFieldMapping (constructor);
1050 var property_mapping = CreatePropertyFieldMapping ((TypeDefinition) constructor.DeclaringType);
1052 foreach (var pair in property_mapping) {
1054 if (!field_mapping.TryGetValue (pair.Value, out argument))
1057 mapping.Add (pair.Key.Name, GetArgumentValue (constructor.Parameters [argument].ParameterType, attribute.ConstructorParameters [argument]));
1061 static object GetArgumentValue (TypeReference reference, object value)
1063 var type = TypeHelper.Resolver.Resolve (reference);
1068 if (IsFlaggedEnum (type))
1069 return GetFlaggedEnumValue (type, value);
1071 return GetEnumValue (type, value);
1077 static bool IsFlaggedEnum (TypeDefinition type)
1082 if (type.CustomAttributes.Count == 0)
1085 foreach (CustomAttribute attribute in type.CustomAttributes)
1086 if (attribute.Constructor.DeclaringType.FullName == "System.FlagsAttribute")
1092 static object GetFlaggedEnumValue (TypeDefinition type, object value)
1094 long flags = Convert.ToInt64 (value);
1095 var signature = new StringBuilder ();
1097 for (int i = type.Fields.Count - 1; i >= 0; i--) {
1098 FieldDefinition field = type.Fields [i];
1100 if (!field.HasConstant)
1103 long flag = Convert.ToInt64 (field.Constant);
1108 if ((flags & flag) == flag) {
1109 if (signature.Length != 0)
1110 signature.Append (", ");
1112 signature.Append (field.Name);
1117 return signature.ToString ();
1120 static object GetEnumValue (TypeDefinition type, object value)
1122 foreach (FieldDefinition field in type.Fields) {
1123 if (!field.HasConstant)
1126 if (Comparer.Default.Compare (field.Constant, value) == 0)
1133 static bool SkipAttribute (CustomAttribute attribute)
1135 var type_name = Utils.CleanupTypeName (attribute.Constructor.DeclaringType);
1137 return !TypeHelper.IsPublic (attribute)
1138 || type_name.EndsWith ("TODOAttribute");
1141 public static void OutputAttributes (XmlDocument doc, XmlNode parent, CustomAttributeCollection attributes)
1143 AttributeData ad = new AttributeData (doc, parent, attributes);
1148 static class Parameters {
1150 public static string GetSignature (ParameterDefinitionCollection infos)
1152 if (infos == null || infos.Count == 0)
1155 var signature = new StringBuilder ();
1156 for (int i = 0; i < infos.Count; i++) {
1159 signature.Append (", ");
1161 ParameterDefinition info = infos [i];
1164 if ((info.Attributes & ParameterAttributes.In) != 0)
1166 else if (((int)info.Attributes & 0x8) != 0) // retval
1168 else if ((info.Attributes & ParameterAttributes.Out) != 0)
1171 modifier = string.Empty;
1173 if (modifier.Length > 0)
1174 signature.AppendFormat ("{0} ", modifier);
1176 signature.Append (Utils.CleanupTypeName (info.ParameterType));
1179 return signature.ToString ();
1184 class TypeReferenceComparer : IComparer
1186 public static TypeReferenceComparer Default = new TypeReferenceComparer ();
1188 public int Compare (object a, object b)
1190 TypeReference ta = (TypeReference) a;
1191 TypeReference tb = (TypeReference) b;
1192 int result = String.Compare (ta.Namespace, tb.Namespace);
1196 return String.Compare (ta.Name, tb.Name);
1200 class MemberReferenceComparer : IComparer
1202 public static MemberReferenceComparer Default = new MemberReferenceComparer ();
1204 public int Compare (object a, object b)
1206 MemberReference ma = (MemberReference) a;
1207 MemberReference mb = (MemberReference) b;
1208 return String.Compare (ma.Name, mb.Name);
1212 class MethodDefinitionComparer : IComparer
1214 public static MethodDefinitionComparer Default = new MethodDefinitionComparer ();
1216 public int Compare (object a, object b)
1218 MethodDefinition ma = (MethodDefinition) a;
1219 MethodDefinition mb = (MethodDefinition) b;
1220 int res = String.Compare (ma.Name, mb.Name);
1224 ParameterDefinitionCollection pia = ma.Parameters ;
1225 ParameterDefinitionCollection pib = mb.Parameters;
1226 res = pia.Count - pib.Count;
1230 string siga = Parameters.GetSignature (pia);
1231 string sigb = Parameters.GetSignature (pib);
1232 return String.Compare (siga, sigb);