Ejemplo n.º 1
0
        public static ClassStructure BuildFromClassWithMemberAndTypes(ClassWithMemberAndTypes record)
        {
            var structure = new ClassStructure();

            var dotPosition = record.ClassInfo.ClassName.IndexOf('.');

            if (dotPosition != -1)
            {
                structure.Namespace = record.ClassInfo.ClassName.Substring(0, dotPosition);
                structure.Name      = record.ClassInfo.ClassName.Substring(dotPosition + 1);
            }
            else
            {
                structure.Name = record.ClassInfo.ClassName;
            }

            foreach (var memberName in record.ClassInfo.MemberNames)
            {
                var type = record.MemberTypeInfo.Types[memberName];
                var info = record.MemberTypeInfo.Infos[memberName];

                var parsedMemberName = memberName;
                // The member name shows up as Class+MemberName if it exists as a subclass and the parent contains the
                // member.
                var splitPosition = parsedMemberName.IndexOf('+');
                if (splitPosition != -1)
                {
                    var parentClassName = parsedMemberName.Substring(0, splitPosition);
                    parsedMemberName = parsedMemberName.Substring(splitPosition + 1);

                    if (structure.Extends == null)
                    {
                        structure.Extends = new ClassStructure()
                        {
                            Name = parentClassName
                        };
                    }
                }

                var isProperty = false;
                var match      = Regex.Match(parsedMemberName, "<([^>]+)>k__BackingField");
                if (match.Success)
                {
                    isProperty       = true;
                    parsedMemberName = match.Groups[1].Value;
                }

                var memberStructure = new ClassStructure.MemberStructure()
                {
                    Name       = parsedMemberName,
                    IsProperty = isProperty,
                };

                switch (type)
                {
                case BinaryType.Primitive:
                    memberStructure.Type = ConvertType((info.Info as AdditionalInfoPrimitiveType).Type);
                    break;

                case BinaryType.String:
                    memberStructure.Type = "string";
                    break;

                case BinaryType.Class:
                    memberStructure.Type = (info.Info as AdditionalInfoClassTypeInfo).ClassName;
                    break;

                case BinaryType.SystemClass:
                    memberStructure.Type = GetTypeString((info.Info as AdditionalInfoSystemClass).ClassName);
                    break;

                default:
                    memberStructure.Type = $"// {type}";
#if DEBUG
                    Debugger.Break();
#endif
                    break;
                }

                if (memberStructure != null)
                {
                    // If we inherited from a parent we need to add the member to it
                    if (splitPosition != -1)
                    {
                        structure.Extends.Members.Add(memberStructure);
                    }
                    else
                    {
                        structure.Members.Add(memberStructure);
                    }
                }
            }

            return(structure);
        }