Example #1
0
        static EnumValue[] GetEnumValues(Type enumType, XmlTypeDescription xmlDoc)
        {
            var fields = enumType.GetFields(BindingFlags.Public | BindingFlags.Static);
            var values = new EnumValue[fields.Length];

            for (var i = 0; i < fields.Length; i++)
            {
                var field = fields[i];

                var id          = field.GetRawConstantValue();
                var name        = field.Name;
                var displayName = field.GetCustomAttribute <DisplayNameAttribute>()?.DisplayName ?? name;
                var isActive    = field.GetCustomAttribute <ObsoleteAttribute>() == null;

                // first preference is to get the description from the Description attribute
                var description = field.GetCustomAttribute <DescriptionAttribute>()?.Description;

                if (description == null)
                {
                    // second choice is an XML summary comment
                    xmlDoc?.FieldSummaries?.TryGetValue(name, out description);

                    if (description == null) // sadly we don't have any description available
                    {
                        description = "";
                    }
                }

                values[i] = new EnumValue(id, name, displayName, isActive, description);
            }

            Array.Sort(values, ValueComparer);

            return(values);
        }
Example #2
0
        static List <EnumInfo> GetEnumInfos(List <AssemblyInfo> asmInfos, string attributeName, Logger logger)
        {
            if (!attributeName.EndsWith("Attribute"))
            {
                attributeName += "Attribute";
            }

            var enumInfos = new List <EnumInfo>();

            foreach (var asmInfo in asmInfos)
            {
                foreach (var type in asmInfo.Assembly.GetTypes())
                {
                    if (!type.IsEnum)
                    {
                        continue;
                    }

                    XmlTypeDescription xmlDoc = null;
                    asmInfo.XmlDocument?.TypesDescriptions?.TryGetValue(type.FullName, out xmlDoc);

                    var enumInfo = TryGetEnumInfoFromType(type, attributeName, xmlDoc);
                    if (enumInfo != null)
                    {
                        enumInfos.Add(enumInfo);
                    }
                }
            }

            // log information
            using (logger.OpenBlock($"Found {enumInfos.Count} enums"))
            {
                if (enumInfos.Count > 0)
                {
                    var sb = new StringBuilder();

                    var maxEnumName = enumInfos.Select(ei => ei.FullName.Length).Max();
                    var format      = $"    {{0,-{Math.Max(4, maxEnumName)}}}  {{1}}";

                    sb.AppendFormat(format, "Enum", "SQL Table");
                    sb.AppendLine();
                    sb.AppendFormat(format, "----", "---------");
                    foreach (var ei in enumInfos)
                    {
                        sb.AppendLine();
                        sb.AppendFormat(format, ei.FullName, ei.Table);
                    }

                    logger.Info(sb.ToString());
                }
            }

            return(enumInfos);
        }
Example #3
0
        static EnumInfo TryGetEnumInfoFromType(Type enumType, string attributeName, XmlTypeDescription xmlDoc)
        {
            var attrInfo = DuckTyping.GetAttributeInfo(enumType, attributeName);

            if (attrInfo == null) // the enum wasn't marked with the EnumToSql attribute
            {
                return(null);
            }

            var values = GetEnumValues(enumType, xmlDoc);

            return(new EnumInfo(enumType, attrInfo, values));
        }
Example #4
0
        static Dictionary <string, XmlTypeDescription> ReadTypesFromMembersXml(XmlNode membersNode)
        {
            var types = new Dictionary <string, XmlTypeDescription>();

            if (membersNode == null)
            {
                return(types);
            }

            var sb       = new StringBuilder();
            var children = membersNode.ChildNodes;
            var count    = children.Count;

            for (var i = 0; i < count; i++)
            {
                var child = children[i];
                if (child.NodeType != XmlNodeType.Element || child.Name != "member")
                {
                    continue;
                }

                var name = child.Attributes?.GetNamedItem("name")?.Value;
                if (name == null || name[0] != 'T' && name[0] != 'F') // we only care about enums - which means types and fields
                {
                    continue;
                }

                var summary = GetSummary(child, sb);

                var    isType = name[0] == 'T';
                string typeName, fieldName;

                if (isType)
                {
                    typeName  = name.Substring(2);
                    fieldName = null;
                }
                else
                {
                    var lastDot = name.LastIndexOf('.');
                    typeName  = name.Substring(2, lastDot - 2);
                    fieldName = name.Substring(lastDot + 1);
                }

                XmlTypeDescription info;
                if (!types.TryGetValue(typeName, out info))
                {
                    info            = new XmlTypeDescription(typeName);
                    types[typeName] = info;
                }

                if (isType)
                {
                    info.Summary = summary;
                }
                else
                {
                    info.FieldSummaries[fieldName] = summary;
                }
            }

            return(types);
        }