Exemple #1
0
        private void CreateExportTypeDefinition(LoadedAssembly assemblyData, ExportedTypeHandle handle,
                                                Dictionary <TypeDefinitionKey, TypeDefinition> typeDictionary)
        {
            var exportedType = assemblyData.MetadataReader.GetExportedType(handle);

            if (!exportedType.IsForwarder)
            {
                return;                            // TODO: what does this mean?
            }
            var fullName = GetFullName(
                assemblyData.MetadataReader.GetString(exportedType.Namespace),
                assemblyData.MetadataReader.GetString(exportedType.Name), null);

            var definition = exportedType.Implementation;

            Debug.Assert(definition.Kind == HandleKind.AssemblyReference, "definition.Kind == HandleKind.AssemblyReference; actual = " + definition.Kind);

            var assemblyReference = assemblyData.MetadataReader.GetAssemblyReference((AssemblyReferenceHandle)definition);

            var            key = new TypeDefinitionKey(assemblyData.MetadataReader.GetString(assemblyReference.Name), fullName);
            TypeDefinition type;

            if (typeDictionary.TryGetValue(key, out type))
            {
                var newKey = new TypeDefinitionKey(assemblyData.Assembly.Name, fullName);
                if (!typeDictionary.ContainsKey(newKey))
                {
                    typeDictionary.Add(newKey, type);
                }
            }
        }
Exemple #2
0
        private TypeDefinition CreateTypeDefinition(Dictionary <string, LoadedAssembly> assemblies, LoadedAssembly assemblyData, TypeDefinitionHandle handle, Dictionary <TypeDefinitionKey, TypeDefinition> typeDictionary, IList <TypeDefinition> types)
        {
            var metadataReader = assemblyData.MetadataReader;
            var typeDefinition = metadataReader.GetTypeDefinition(handle);
            var ns             = metadataReader.GetString(typeDefinition.Namespace) ?? string.Empty;
            var name           = metadataReader.GetString(typeDefinition.Name);

            if (SkipCompilerGeneratedTypes && (ns.Contains(CompilerGeneratedNameMarker) || name.Contains(CompilerGeneratedNameMarker)))
            {
                return(null);
            }

            var declaringType = ResolveTypeHandle(assemblies, assemblyData, typeDefinition.GetDeclaringType(), typeDictionary, types);

            var fullName = GetFullName(ns, name, declaringType);

            var            key = new TypeDefinitionKey(assemblyData.Assembly.Name, fullName);
            TypeDefinition def;

            if (typeDictionary.TryGetValue(key, out def))
            {
                return(def);
            }

            var attributeNames = typeDefinition.GetCustomAttributes()
                                 .Select(t => GetAttributeTypeName(metadataReader, t)).ToImmutableArray();

            if (SkipNativeTypes && attributeNames.Contains(NativeClassAttributeName))
            {
                // skip native types
                return(null);
            }

            string baseTypeFullName;
            var    baseType = ResolveTypeHandle(assemblies, assemblyData, typeDefinition.BaseType, typeDictionary, types, out baseTypeFullName);

            var isPublic    = HasAttributes(typeDefinition, TypeAttributes.Public);
            var isInterface = HasAttributes(typeDefinition, TypeAttributes.Interface);

            var isValueType = baseTypeFullName != null && (baseTypeFullName.Equals(ValueTypeName, StringComparison.Ordinal) ||
                                                           baseTypeFullName.Equals(EnumTypeName, StringComparison.Ordinal));


            var isAbstract = HasAttributes(typeDefinition, TypeAttributes.Abstract);

            var interfaces = typeDefinition.GetInterfaceImplementations()
                             .Select(t => ResolveTypeHandle(assemblies, assemblyData,
                                                            metadataReader.GetInterfaceImplementation(t).Interface, typeDictionary, types))
                             .Where(t => t != null)
                             .ToImmutableArray();

            var ctors = typeDefinition.GetMethods().Select(metadataReader.GetMethodDefinition)
                        .Where(t => !HasAttributes(t, MethodAttributes.Static) &&
                               HasAttributes(t, MethodAttributes.SpecialName) &&
                               metadataReader.StringComparer.Equals(t.Name, CtorSpecialName)).ToArray();

            var hasDefaultCtor     = ctors.Any(x => x.GetParameters().Count == 0 && HasAttributes(x, MethodAttributes.Public));
            var hasNonDefaultCtors = ctors.Any(x => x.GetParameters().Count > 0);

            var genericParameterCount = typeDefinition.GetGenericParameters().Count;

            def = new TypeDefinition(assemblyData.Assembly.Name, ns, name, fullName,
                                     baseType,
                                     declaringType,
                                     isPublic,
                                     attributeNames,
                                     isInterface,
                                     interfaces,
                                     isValueType,
                                     isAbstract,
                                     hasDefaultCtor,
                                     hasNonDefaultCtors,
                                     genericParameterCount);

            typeDictionary.Add(key, def);

            if (!assemblyData.IsReferenecOnly)
            {
                types.Add(def);
            }

            return(def);
        }