Esempio n. 1
0
        internal void LoadFromMetadata(IAssemblySymbol assemblySymbol)
        {
            _isLoading = true;
            IterateAllTypeDefinitions(assemblySymbol, namedTypeSymbol =>
            {
                RegisterType(namedTypeSymbol, null);

                // Return false so we don't process nested types. RegisterType will register nested type recursively.
                return(false);
            });

            // Finalize Definitions
            IterateAllTypeDefinitions(assemblySymbol,
                                      namedTypeAction: namedTypeSymbol =>
            {
                var typeData = (TypeDefinitionData)GetTypeData(namedTypeSymbol);
                if (typeData == null)
                {
                    return(false);
                }

                typeData.FinalizeDefinition(namedTypeSymbol);
                return(true);
            },
                                      typeParameterAction: typeParameterSymbol =>
            {
                GetGenericTypeParameterData(typeParameterSymbol).FinalizeDefinition(typeParameterSymbol);
            });

            // Populate Members
            IterateAllTypeDefinitions(assemblySymbol,
                                      namedTypeAction: namedTypeSymbol =>
            {
                var typeData = (TypeDefinitionData)GetTypeData(namedTypeSymbol);
                if (typeData == null)
                {
                    return(false);
                }

                typeData.PopulateMembers(namedTypeSymbol);
                return(true);
            });

            while (_constructedGenericsToFinalizeAfterLoad.Count != 0)
            {
                var temp = _constructedGenericsToFinalizeAfterLoad.ToArray();
                _constructedGenericsToFinalizeAfterLoad.Clear();
                foreach (var type in temp)
                {
                    type.FinalizeDefiniton();
                }
            }

            var metadataReader = assemblySymbol.Modules.First().GetMetadata().GetMetadataReader();
            var forwarders     = metadataReader.ExportedTypes.Select(metadataReader.GetExportedType).Where(e => e.IsForwarder);

            foreach (var forwarder in forwarders)
            {
                var metadataName = metadataReader.GetString(forwarder.Name);
                if (!forwarder.Namespace.IsNil)
                {
                    metadataName = $"{metadataReader.GetString(forwarder.Namespace)}.{metadataName}";
                }
                var forwardedType = assemblySymbol.ResolveForwardedType(metadataName);
                if (forwardedType != null)
                {
                    var forwardedTypeData = (TypeDefinitionData)Context.GetTypeData(forwardedType);
                    if (forwardedTypeData != null)
                    {
                        AddForwardedType(forwardedTypeData);
                    }
                }
            }

            foreach (var referenceIdentity in assemblySymbol.Modules.SelectMany(m => m.ReferencedAssemblies))
            {
                if (Context._cachedAssemblyDatas.TryGetValue(referenceIdentity, out AssemblyData reference))
                {
                    _referencedAssemblies.Add(reference);
                }
            }

            _isLoading = false;
        }