예제 #1
0
        private TypeMetadataDto LoadGenericParameterTypeObject(Type type, AssemblyMetadataStorage metaStore)
        {
            TypeMetadataDto metadataType;
            string          id = $"{type.DeclaringType.FullName}<{type.Name}>";

            _logger.Trace("Adding generic argument type with Id =" + id);

            if (!metaStore.TypesDictionary.ContainsKey(id))
            {
                metadataType = new TypeMetadataDto()
                {
                    Id                    = id,
                    TypeName              = type.Name,
                    NamespaceName         = type.Namespace,
                    Modifiers             = null,
                    TypeKind              = GetTypeKind(type),
                    Attributes            = new List <Attribute>(),
                    Properties            = new List <PropertyMetadataDto>(),
                    Constructors          = new List <MethodMetadataDto>(),
                    ImplementedInterfaces = new List <TypeMetadataDto>(),
                    Methods               = new List <MethodMetadataDto>(),
                    NestedTypes           = new List <TypeMetadataDto>(),
                    GenericArguments      = new List <TypeMetadataDto>()
                };
                metaStore.TypesDictionary.Add(metadataType.Id, metadataType);
                return(metadataType);
            }
            else
            {
                return(metaStore.TypesDictionary[id]);
            }
        }
예제 #2
0
        private TypeMetadataDto LoadSimpleTypeObject(Type type, AssemblyMetadataStorage metaStore)
        {
            TypeMetadataDto metadataType;

            metadataType = new TypeMetadataDto // add only basic information
            {
                Id                    = type.FullName,
                TypeName              = type.Name,
                NamespaceName         = type.Namespace,
                Modifiers             = EmitModifiers(type),
                TypeKind              = GetTypeKind(type),
                Attributes            = type.GetCustomAttributes(false).Cast <Attribute>(),
                Properties            = new List <PropertyMetadataDto>(),
                Constructors          = new List <MethodMetadataDto>(),
                ImplementedInterfaces = new List <TypeMetadataDto>(),
                Methods               = new List <MethodMetadataDto>(),
                NestedTypes           = new List <TypeMetadataDto>()
            };

            metaStore.TypesDictionary.Add(type.FullName, metadataType);

            if (type.IsGenericTypeDefinition)
            {
                metadataType.GenericArguments = EmitGenericArguments(type.GetGenericArguments(), metaStore);
                metadataType.TypeName         =
                    $"{type.Name}<{metadataType.GenericArguments.Select(a => a.TypeName).Aggregate((p, n) => $"{p}, {n}")}>";
            }
            else
            {
                metadataType.GenericArguments = new List <TypeMetadataDto>();
            }

            _logger.Trace("Adding type not declared in assembly being inspected: Id =" + metadataType.Id + " ; Name = " + metadataType.TypeName);
            return(metadataType);
        }
예제 #3
0
        private TypeMetadataDto EmitDeclaringType(Type declaringType, AssemblyMetadataStorage metaStore)
        {
            if (declaringType == null)
            {
                return(null);
            }

            return(LoadTypeMetadataDto(declaringType, metaStore));
        }
예제 #4
0
        private TypeMetadataDto EmitExtends(Type baseType, AssemblyMetadataStorage metaStore)
        {
            if (baseType == null || baseType == typeof(Object) || baseType == typeof(ValueType) ||
                baseType == typeof(Enum))
            {
                return(null);
            }

            return(LoadTypeMetadataDto(baseType, metaStore));
        }
예제 #5
0
        protected void With_AssemblyMetadata()
        {
            _assemblyMetadata = new AssemblyMetadataDto()
            {
                Id         = _assemblyName,
                Name       = _assemblyName,
                Namespaces = new List <NamespaceMetadataDto>()
            };

            _storage = new AssemblyMetadataStorage(_assemblyMetadata);
        }
예제 #6
0
        internal AssemblyMetadataStorage LoadAssemblyMetadata(Assembly assembly)
        {
            AssemblyMetadataDto assemblyMetadata = new AssemblyMetadataDto()
            {
                Id   = assembly.ManifestModule.FullyQualifiedName,
                Name = assembly.ManifestModule.Name,
            };

            AssemblyMetadataStorage metaStore = new AssemblyMetadataStorage(assemblyMetadata);

            assemblyMetadata.Namespaces = (from Type type in assembly.GetTypes()
                                           where type.IsVisible()
                                           group type by type.GetNamespace() into namespaceGroup
                                           orderby namespaceGroup.Key
                                           select LoadNamespaceMetadata(namespaceGroup.Key, namespaceGroup, metaStore)).ToList();

            return(metaStore);
        }
예제 #7
0
        internal MethodMetadataDto LoadMethodMetadataDto(MethodBase method, AssemblyMetadataStorage metaStore)
        {
            if (method == null)
            {
                throw new ArgumentNullException($"{nameof(method)} argument is null.");
            }

            MethodMetadataDto methodMetadataDto = new MethodMetadataDto()
            {
                Name      = method.Name,
                Modifiers = EmitModifiers(method),
                Extension = IsExtension(method)
            };

            methodMetadataDto.GenericArguments = !method.IsGenericMethodDefinition ? new List <TypeMetadataDto>() : EmitGenericArguments(method.GetGenericArguments(), metaStore);
            methodMetadataDto.ReturnType       = EmitReturnType(method, metaStore);
            methodMetadataDto.Parameters       = EmitParameters(method.GetParameters(), metaStore).ToList();

            string parameters = methodMetadataDto.Parameters.Any()
                ? methodMetadataDto.Parameters.Select(methodInstance => methodInstance.Name)
                                .Aggregate((current, next) => current + ", " + next)
                : "none";

            string generics = methodMetadataDto.GenericArguments.Any()
                ? methodMetadataDto.GenericArguments.Select(typeInstance => typeInstance.Id)
                              .Aggregate((c, n) => $"{c}, {n}")
                : "none";

            methodMetadataDto.Id = $"{method.DeclaringType.FullName}{method.Name} args {parameters} generics {generics} declaredBy {method.DeclaringType.FullName}";

            if (!metaStore.MethodsDictionary.ContainsKey(methodMetadataDto.Id))
            {
                _logger.Trace("Adding method to dictionary: Id =" + methodMetadataDto.Id);
                metaStore.MethodsDictionary.Add(methodMetadataDto.Id, methodMetadataDto);
                return(methodMetadataDto);
            }
            else
            {
                _logger.Trace("Using method already added to dictionary: Id =" + methodMetadataDto.Id);
                return(metaStore.MethodsDictionary[methodMetadataDto.Id]);
            }
        }
예제 #8
0
        private TypeMetadataDto LoadFullTypeObject(Type type, AssemblyMetadataStorage metaStore)
        {
            TypeMetadataDto metadataType = new TypeMetadataDto()
            {
                Id            = type.FullName,
                TypeName      = type.Name,
                NamespaceName = type.Namespace,
                Modifiers     = EmitModifiers(type),
                TypeKind      = GetTypeKind(type),
                Attributes    = type.GetCustomAttributes(false).Cast <Attribute>()
            };

            _logger.Trace("Adding type: Id =" + metadataType.Id + " ; Name = " + metadataType.TypeName);
            metaStore.TypesDictionary.Add(type.FullName, metadataType);

            metadataType.DeclaringType         = EmitDeclaringType(type.DeclaringType, metaStore);
            metadataType.ImplementedInterfaces = EmitImplements(type.GetInterfaces(), metaStore);
            metadataType.BaseType    = EmitExtends(type.BaseType, metaStore);
            metadataType.NestedTypes = EmitNestedTypes(type.GetNestedTypes(), metaStore);

            if (type.IsGenericTypeDefinition)
            {
                metadataType.GenericArguments = EmitGenericArguments(type.GetGenericArguments(), metaStore);
                metadataType.TypeName         =
                    $"{type.Name}<{metadataType.GenericArguments.Select(a => a.TypeName).Aggregate((p, n) => $"{p}, {n}")}>";
            }
            else
            {
                metadataType.GenericArguments = new List <TypeMetadataDto>();
            }

            metadataType.Constructors = EmitMethods(type.GetConstructors(), metaStore);
            metadataType.Methods      = EmitMethods(type.GetMethods(BindingFlags.DeclaredOnly), metaStore);

            metadataType.Properties = EmitProperties(type.GetProperties(), metaStore);
            return(metadataType);
        }
예제 #9
0
        internal TypeMetadataDto LoadTypeMetadataDto(Type type, AssemblyMetadataStorage metaStore)
        {
            if (type == null)
            {
                throw new ArgumentNullException($"{nameof(type)} argument is null.");
            }

            if (type.IsGenericParameter)
            {
                return(LoadGenericParameterTypeObject(type, metaStore));
            }
            else
            {
                if (!metaStore.TypesDictionary.ContainsKey(type.FullName))
                {
                    TypeMetadataDto metadataType;

                    // if type is not declared in assembly being inspected
                    if (type.Assembly.ManifestModule.FullyQualifiedName != metaStore.AssemblyMetadata.Id) // load basic info
                    {
                        metadataType = LoadSimpleTypeObject(type, metaStore);
                    }
                    else // load full type information
                    {
                        metadataType = LoadFullTypeObject(type, metaStore);
                    }

                    return(metadataType);
                }
                else
                {
                    _logger.Trace("Using type already added to dictionary with key: " + type.FullName);
                    return(metaStore.TypesDictionary[type.FullName]);
                }
            }
        }
예제 #10
0
 internal IEnumerable <MethodMetadataDto> EmitMethods(IEnumerable <MethodBase> methods, AssemblyMetadataStorage metaStore)
 {
     return((from MethodBase currentMethod in methods
             where currentMethod.IsVisible()
             select LoadMethodMetadataDto(currentMethod, metaStore)).ToList());
 }
예제 #11
0
 private IEnumerable <TypeMetadataDto> EmitImplements(IEnumerable <Type> interfaces, AssemblyMetadataStorage metaStore)
 {
     return((from currentInterface in interfaces
             select LoadTypeMetadataDto(currentInterface, metaStore)).ToList());
 }
예제 #12
0
 private IEnumerable <TypeMetadataDto> EmitNestedTypes(IEnumerable <Type> nestedTypes, AssemblyMetadataStorage metaStore)
 {
     return((from type in nestedTypes
             where type.IsVisible()
             select LoadTypeMetadataDto(type, metaStore)).ToList());
 }
예제 #13
0
 internal IEnumerable <TypeMetadataDto> EmitGenericArguments(IEnumerable <Type> arguments, AssemblyMetadataStorage metaStore)
 {
     return((from Type argument in arguments select LoadTypeMetadataDto(argument, metaStore)).ToList());
 }
예제 #14
0
        internal IEnumerable <PropertyMetadataDto> EmitProperties(IEnumerable <PropertyInfo> props, AssemblyMetadataStorage metaStore)
        {
            List <PropertyMetadataDto> properties = new List <PropertyMetadataDto>();

            foreach (PropertyInfo property in props)
            {
                if (property.GetGetMethod().IsVisible() || property.GetSetMethod().IsVisible())
                {
                    string id = $"{property.DeclaringType.FullName}.{property.Name}";
                    if (metaStore.PropertiesDictionary.ContainsKey(id))
                    {
                        _logger.Trace("Using property already added to dictionary: Id =" + id);
                        properties.Add(metaStore.PropertiesDictionary[id]);
                    }
                    else
                    {
                        PropertyMetadataDto newProperty = new PropertyMetadataDto()
                        {
                            Id   = id,
                            Name = property.Name
                        };
                        _logger.Trace("Adding new property to dictionary: " + newProperty.Id + " ;Name = " + newProperty.Name);
                        metaStore.PropertiesDictionary.Add(newProperty.Id, newProperty);
                        properties.Add(newProperty);

                        newProperty.TypeMetadata = LoadTypeMetadataDto(property.PropertyType, metaStore);
                    }
                }
            }

            return(properties);
        }
예제 #15
0
        private IEnumerable <ParameterMetadataDto> EmitParameters(IEnumerable <ParameterInfo> parameters, AssemblyMetadataStorage metaStore)
        {
            List <ParameterMetadataDto> parametersMetadata = new List <ParameterMetadataDto>();

            foreach (var parameter in parameters)
            {
                string id = $"{parameter.ParameterType.FullName}.{parameter.Name}";
                if (metaStore.ParametersDictionary.ContainsKey(id))
                {
                    _logger.Trace("Using parameter already added to dictionary: Id =" + id);
                    parametersMetadata.Add(metaStore.ParametersDictionary[id]);
                }
                else
                {
                    ParameterMetadataDto newParameter = new ParameterMetadataDto(parameter.Name, LoadTypeMetadataDto(parameter.ParameterType, metaStore));
                    newParameter.Id = id;
                    metaStore.ParametersDictionary.Add(id, newParameter);
                    _logger.Trace("Adding parameter to dictionary: Id =" + id);
                    parametersMetadata.Add(newParameter);
                }
            }

            return(parametersMetadata);
        }
예제 #16
0
        private TypeMetadataDto EmitReturnType(MethodBase method, AssemblyMetadataStorage metaStore)
        {
            MethodInfo methodInfo = method as MethodInfo;

            return(methodInfo == null ? null : LoadTypeMetadataDto(methodInfo.ReturnType, metaStore));
        }
예제 #17
0
        internal NamespaceMetadataDto LoadNamespaceMetadata(string name, IEnumerable <Type> types, AssemblyMetadataStorage metaStore)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentException($"{nameof(name)} is null/empty/whitespace");
            }

            NamespaceMetadataDto namespaceMetadata = new NamespaceMetadataDto()
            {
                Id            = name,
                NamespaceName = name
            };

            _logger.Trace("Adding Namespace to dictionary: " + namespaceMetadata.NamespaceName);
            metaStore.NamespacesDictionary.Add(namespaceMetadata.NamespaceName, namespaceMetadata);

            namespaceMetadata.Types = (from type in types orderby type.Name select LoadTypeMetadataDto(type, metaStore)).ToList();

            return(namespaceMetadata);
        }