Пример #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        internal static List <MemberDefinition> GenerateMemberDefinitions(Type type)
        {
            var lines   = new List <MemberDefinition>();
            var members = type.GetMembers(VisibleMembers).ToList();

            members.Sort(new MemberComparer(type));

            foreach (var info in members)
            {
                var infoString = string.Empty;
                switch (info.MemberType)
                {
                case MemberTypes.Constructor:
                    infoString = ReadableTypeOutputFactory.GetConstructorInfoString(type, info as ConstructorInfo);
                    break;

                case MemberTypes.Event:
                    infoString = ReadableTypeOutputFactory.GetEventInfoString(type, info as EventInfo);
                    break;

                case MemberTypes.Field:
                    infoString = ReadableTypeOutputFactory.GetFieldInfoString(type, info as FieldInfo);
                    break;

                case MemberTypes.Method:
                    infoString = ReadableTypeOutputFactory.GetMethodInfoString(type, info as MethodInfo);
                    break;

                case MemberTypes.Property:
                    infoString = ReadableTypeOutputFactory.GetPropertyInfoString(type, info as PropertyInfo);
                    break;

                case MemberTypes.NestedType:
                    //DumpClassAPI(builder, info as Type);
                    break;

                default:
                    infoString = $" {info.Name} ";
                    break;
                }

                if (!string.IsNullOrWhiteSpace(infoString))
                {
                    var attributes = ReadableTypeOutputFactory.GetCustomAttributesList(info.GetCustomAttributes(true));
                    lines.Add(new MemberDefinition(infoString, attributes));
                }
            }

            return(lines);
        }
Пример #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="assemblyName"></param>
        /// <returns></returns>
        internal static List <TypeDefinition> GenerateTypeDefinitionsForAssembly(string assemblyName)
        {
            var typesList       = new List <Type>();
            var typeDefinitions = new List <TypeDefinition>();

            try
            {
                var assembly = File.Exists(assemblyName) ? Assembly.LoadFrom(assemblyName) : Assembly.Load(assemblyName);
                typesList.AddRange(assembly.GetTypes());
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (Exception ex)
#pragma warning restore CA1031 // Do not catch general exception types
            {
                Console.WriteLine("Exception loading types from assembly '{0}':", assemblyName);
                Console.WriteLine(ex.ToString());
                return(typeDefinitions);
            }

            foreach (Type type in typesList.Where(c => !c.IsSpecialName && !c.FullName.StartsWith("<PrivateImplementationDetails>")).OrderBy(c => c.FullName))
            {
                /*
                 * if(0 <= Array.BinarySearch(_ignoreTypes,typeFullName)) {
                 *  continue;
                 * }
                 */

                Type declaringType = type;
                while (declaringType != null)
                {
                    switch (TypeAttributes.VisibilityMask & declaringType.Attributes)
                    {
                    case TypeAttributes.Public:
                    case TypeAttributes.NestedPublic:
                    case TypeAttributes.NestedFamily:
                    case TypeAttributes.NestedFamANDAssem:
                    case TypeAttributes.NestedFamORAssem:
                        declaringType = declaringType.DeclaringType;
                        continue;

                    case TypeAttributes.NotPublic:
                    case TypeAttributes.NestedPrivate:
                    case TypeAttributes.NestedAssembly:
                        Debug.Assert(null != declaringType, "null declaringType");
                        break;

                    default:
                        Debug.Assert(false, "unknown type");
                        throw new InvalidOperationException(declaringType.Attributes.ToString());
                    }
                    break;
                }

                if (typeof(TypeConverter).IsAssignableFrom(type))
                {
                    ConstructorInfo ctor = type.GetConstructor(BindingFlags.Public | BindingFlags.CreateInstance | BindingFlags.Instance, null, EmptyTypes, EmptyParameterModifiers);
                }

                var declarationString = ReadableTypeOutputFactory.GetTypeDeclarationString(type);
                if (string.IsNullOrWhiteSpace(declarationString))
                {
                    continue;
                }

                var classAttributes   = ReadableTypeOutputFactory.GetCustomAttributesList(type.GetCustomAttributes(false));
                var memberDefinitions = GenerateMemberDefinitions(type);

                typeDefinitions.Add(new TypeDefinition(declarationString, classAttributes, memberDefinitions));
            }

            return(typeDefinitions);
        }