Ejemplo n.º 1
0
        public virtual bool TryGetAttributeString(CustomAttribute attribute, out string rval, string prefix = null, bool withBrackets = true)
        {
            if (attribute == null)
            {
                if (string.IsNullOrEmpty(prefix))
                {
                    rval = null;
                    return(false);
                }
                rval = withBrackets ? PrefixBrackets + prefix + SurfixBrackets : prefix;
                return(true);
            }

            if (IsIgnoredAttribute(attribute))
            {
                rval = null;
                return(false);
            }

            TypeDefinition attrType = attribute.AttributeType as TypeDefinition;

            if (attrType != null && !DocUtils.IsPublic(attrType) ||
                (FormatterManager.SlashdocFormatter.GetName(attribute.AttributeType) == null) ||
                Array.IndexOf(IgnorableAttributes, attribute.AttributeType.FullName) >= 0)
            {
                rval = null;
                return(false);
            }

            var fields = new List <string>();

            for (int i = 0; i < attribute.ConstructorArguments.Count; ++i)
            {
                CustomAttributeArgument argument = attribute.ConstructorArguments[i];
                fields.Add(MakeAttributesValueString(
                               argument.Value,
                               argument.Type));
            }
            var namedArgs =
                (from namedArg in attribute.Fields
                 select new { Type = namedArg.Argument.Type, Name = namedArg.Name, Value = namedArg.Argument.Value })
                .Concat(
                    (from namedArg in attribute.Properties
                     select new { Type = namedArg.Argument.Type, Name = namedArg.Name, Value = namedArg.Argument.Value }))
                .OrderBy(v => v.Name);

            foreach (var d in namedArgs)
            {
                fields.Add(MakeNamedArgumentString(d.Name, MakeAttributesValueString(d.Value, d.Type)));
            }

            string a2 = String.Join(", ", fields.ToArray());

            if (a2 != "")
            {
                a2 = "(" + a2 + ")";
            }

            string name = attribute.GetDeclaringType();

            if (name.EndsWith("Attribute"))
            {
                name = name.Substring(0, name.Length - "Attribute".Length);
            }
            rval = withBrackets ? PrefixBrackets + prefix + name + a2 + SurfixBrackets
                : prefix + name + a2;
            return(true);
        }
Ejemplo n.º 2
0
        protected override string GetTypeDeclaration(TypeDefinition type)
        {
            string visibility = GetTypeVisibility(type.Attributes);

            if (visibility == null)
            {
                return(null);
            }

            StringBuilder buf = new StringBuilder();

            buf.Append(".class ");
            if (type.IsNested)
            {
                buf.Append("nested ");
            }
            buf.Append(visibility).Append(" ");
            if (type.IsInterface)
            {
                buf.Append("interface ");
            }
            if (type.IsSequentialLayout)
            {
                buf.Append("sequential ");
            }
            if (type.IsAutoLayout)
            {
                buf.Append("auto ");
            }
            if (type.IsAnsiClass)
            {
                buf.Append("ansi ");
            }
            if (type.IsAbstract)
            {
                buf.Append("abstract ");
            }
            if (type.IsSerializable)
            {
                buf.Append("serializable ");
            }
            if (type.IsSealed)
            {
                buf.Append("sealed ");
            }
            if (type.IsBeforeFieldInit)
            {
                buf.Append("beforefieldinit ");
            }
            var state = MemberFormatterState;

            MemberFormatterState = MemberFormatterState.WithinGenericTypeParameters;
            buf.Append(GetName(type));
            MemberFormatterState = state;
            var full = new ILFullMemberFormatter(this.TypeMap);

            if (type.BaseType != null)
            {
                buf.Append(" extends ");
                if (type.BaseType.FullName == "System.Object")
                {
                    buf.Append("System.Object");
                }
                else
                {
                    buf.Append(full.GetName(type.BaseType).Substring("class ".Length));
                }
            }
            bool first = true;

            foreach (var name in type.Interfaces.Where(i => DocUtils.IsPublic(i.InterfaceType.Resolve()))
                     .Select(i => full.GetName(i.InterfaceType))
                     .OrderBy(n => n))
            {
                if (first)
                {
                    buf.Append(" implements ");
                    first = false;
                }
                else
                {
                    buf.Append(", ");
                }
                buf.Append(name);
            }

            return(buf.ToString());
        }