Ejemplo n.º 1
0
        static void WriteType(StringBuilder sb, List <string> usings, TypeDefinition type, StubGenOptions opts)
        {
            Action <StringBuilder, List <string>, TypeDefinition, StubGenOptions> typeWriter = null;

            // TODO: security attributes
            if (type.IsSerializable)
                sb.AppendLineIndent ("[Serializable]");

            if (type.HasCustomAttributes)
                sb.Append (type.CustomAttributes.Format ());
            sb.AppendIndent ();

            FormatTypeAttributes (sb, type);
            if (type.IsEnum) {
                sb.Append ("enum");
                typeWriter = EnumWriter;
            } else if (type.IsClass) {
                sb.Append ("class");
                typeWriter = ClassWriter;
            } else if (type.IsInterface) {
                sb.Append ("interface");
                typeWriter = InterfaceWriter;
            } else if (type.IsValueType) {
                if (type.FullName == "System.Delegate" || type.FullName == "System.MulticastDelegate")
                    sb.Append ("delegate");
                else
                    sb.Append ("struct");
            }

            sb.AppendFormat (" {0}", type.FormatName ());

            bool haveColon = false;
            bool first = true;
            TypeReference tref = type.BaseType;
            if (WritableBaseType (tref)) {
                sb.Append (" : ");
                haveColon = true;
                first = false;

                usings.AddUsing (tref.Namespace);
                sb.Append (tref.FormatName ());
            }

            if (type.HasInterfaces) {
                foreach (TypeReference tr in type.Interfaces.OnlyVisible (opts.IncludeNonPublic)) {
                    if (first) {
                        if (!haveColon)
                            sb.Append (" : ");
                        first = false;
                    } else
                        sb.Append (", ");

                    usings.AddUsing (tr.Namespace);
                    sb.Append (tr.FormatName ());
                }
            }

            // TODO: output generic parameter constraints

            sb.AppendLine ();
            sb.AppendLineIndent ("{");

            if (typeWriter != null) {
                Utils.Indent++;
                try {
                    typeWriter (sb, usings, type, opts);
                } finally {
                    Utils.Indent--;
                }
            }

            sb.AppendLineIndent ("}");
        }
Ejemplo n.º 2
0
        static void OutputAssemblyCustomAttributes(StringBuilder sb, List <string> usings, ModuleDefinition module)
        {
            Collection <CustomAttribute> attrs = module.Assembly.CustomAttributes;
            if (attrs == null || attrs.Count == 0)
                return;

            string str;
            TypeReference type;
            bool first;
            foreach (CustomAttribute attr in attrs) {
                type = attr.AttributeType;
                if (type.FullName == "System.Runtime.CompilerServices.ReferenceAssemblyAttribute")
                    continue;

                str = type.Namespace;
                usings.AddUsing (str);

                str = type.Name;
                if (str.EndsWith ("Attribute", StringComparison.Ordinal))
                    str = str.Substring (0, str.Length - 9);

                sb.AppendFormat ("[assembly: {0}", str);
                first = true;
                bool needParen = false;
                if (attr.HasConstructorArguments) {
                    needParen = true;
                    sb.Append (" (");
                    foreach (CustomAttributeArgument arg in attr.ConstructorArguments) {
                        if (!first)
                            sb.Append (", ");
                        else
                            first = false;

                        sb.Append (Utils.FormatValue (arg.Value, arg.Type, usings));
                    }
                }

                if (attr.HasFields) {
                    if (!needParen) {
                        needParen = true;
                        sb.Append (" (");
                    }
                    foreach (Mono.Cecil.CustomAttributeNamedArgument arg in attr.Fields) {
                        if (!first)
                            sb.Append (", ");
                        else
                            first = false;
                        sb.AppendFormat ("{0}={1}", arg.Name, Utils.FormatValue (arg.Argument.Value, arg.Argument.Type, usings));
                    }
                }

                if (attr.HasProperties) {
                    if (!needParen) {
                        needParen = true;
                        sb.Append (" (");
                    }
                    foreach (Mono.Cecil.CustomAttributeNamedArgument arg in attr.Properties) {
                        if (!first)
                            sb.Append (", ");
                        else
                            first = false;
                        sb.AppendFormat ("{0}={1}", arg.Name, Utils.FormatValue (arg.Argument.Value, arg.Argument.Type, usings));
                    }
                }

                if (needParen)
                    sb.Append (")");
                sb.AppendLine ("]");
            }
        }
Ejemplo n.º 3
0
        public static string FormatValue(object v, TypeReference type, List <string> usings)
        {
            if (v == null)
                return "null";

            string ns = type.Namespace;
            usings.AddUsing (ns);

            TypeDefinition tdef = type.ResolveType ();

            string ret;
            if (tdef != null && tdef.IsEnum && tdef.HasFields) {
                Collection <CustomAttribute> attrs = tdef.CustomAttributes;
                CustomAttribute flagsAttr = attrs == null || attrs.Count == 0 ? null : attrs.First ((CustomAttribute attr) => {
                    if (attr.AttributeType.FullName == "System.FlagsAttribute")
                        return true;

                    return false;
                });
                bool flags = flagsAttr != null;
                ns = tdef.Namespace;
                usings.AddUsing (ns);

                string typeName = tdef.FormatName (); //tdef.Name.Replace ("/", ".");
                var sb = new StringBuilder ();
                bool first = true;
                object constant;
                int bits;
                foreach (FieldDefinition fd in tdef.Fields) {
                    if (flags) {
                        constant = fd.Constant;
                        bits = CountBits (constant);
                        if (bits < 0 || bits > 1)
                            continue;

                        if (IsBitSet (v, constant)) {
                            if (first)
                                first = false;
                            else
                                sb.Append (" | ");
                            sb.AppendFormat ("{0}.{1}", typeName, fd.Name);
                        }
                    } else if (v.Equals (fd.Constant)) {
                        sb.AppendFormat ("{0}.{1}", typeName, fd.Name);
                        break;
                    }
                }

                return sb.ToString ();
            }

            ret = v.ToString ();
            if (v is string)
                return "\"" + ret.Replace ("\\", "\\\\") + "\"";

            if (v is bool)
                return ret.ToLower ();

            return ret;
        }