public Assembly GenerateFacade(IAssembly contractAssembly, IAssemblyReference seedCoreAssemblyReference, bool ignoreMissingTypes, IAssembly overrideContractAssembly = null) { Assembly assembly; if (overrideContractAssembly != null) { MetadataDeepCopier copier = new MetadataDeepCopier(_seedHost); assembly = copier.Copy(overrideContractAssembly); // Use non-empty partial facade if present } else { MetadataDeepCopier copier = new MetadataDeepCopier(_contractHost); assembly = copier.Copy(contractAssembly); ReferenceAssemblyToFacadeRewriter rewriter = new ReferenceAssemblyToFacadeRewriter(_seedHost, _contractHost, seedCoreAssemblyReference, _assemblyFileVersion != null); rewriter.Rewrite(assembly); } IEnumerable<string> docIds = _docIdTable[contractAssembly.AssemblyIdentity.Name.Value]; // Add all the type forwards bool error = false; HashSet<string> existingDocIds = new HashSet<string>(assembly.AllTypes.Select(typeDef => typeDef.RefDocId())); IEnumerable<string> missingDocIds = docIds.Where(id => !existingDocIds.Contains(id)); foreach (string docId in missingDocIds) { IReadOnlyList<INamedTypeDefinition> seedTypes; if (!_typeTable.TryGetValue(docId, out seedTypes)) { if (!ignoreMissingTypes) { Trace.TraceError("Did not find type '{0}' in any of the seed assemblies.", docId); error = true; } continue; } INamedTypeDefinition seedType = GetSeedType(docId, seedTypes); if (seedType == null) { TraceDuplicateSeedTypeError(docId, seedTypes); error = true; continue; } AddTypeForward(assembly, seedType); } if (error) { return null; } if (_assemblyFileVersion != null) { assembly.AssemblyAttributes.Add(CreateAttribute("System.Reflection.AssemblyFileVersionAttribute", seedCoreAssemblyReference.ResolvedAssembly, _assemblyFileVersion.ToString())); assembly.AssemblyAttributes.Add(CreateAttribute("System.Reflection.AssemblyInformationalVersionAttribute", seedCoreAssemblyReference.ResolvedAssembly, _assemblyFileVersion.ToString())); } if (_buildDesignTimeFacades) { assembly.AssemblyAttributes.Add(CreateAttribute("System.Runtime.CompilerServices.ReferenceAssemblyAttribute", seedCoreAssemblyReference.ResolvedAssembly)); assembly.Flags |= ReferenceAssemblyFlag; } if (_clearBuildAndRevision) { assembly.Version = new Version(assembly.Version.Major, assembly.Version.Minor, 0, 0); } AddWin32VersionResource(contractAssembly.Location, assembly); return assembly; }
public Assembly GenerateFacade(IAssembly contractAssembly, IAssemblyReference seedCoreAssemblyReference, bool ignoreMissingTypes, IAssembly overrideContractAssembly = null) { Assembly assembly; if (overrideContractAssembly != null) { MetadataDeepCopier copier = new MetadataDeepCopier(_seedHost); assembly = copier.Copy(overrideContractAssembly); // Use non-empty partial facade if present } else { MetadataDeepCopier copier = new MetadataDeepCopier(_contractHost); assembly = copier.Copy(contractAssembly); ReferenceAssemblyToFacadeRewriter rewriter = new ReferenceAssemblyToFacadeRewriter(_seedHost, _contractHost, seedCoreAssemblyReference, _assemblyFileVersion != null); rewriter.Rewrite(assembly); } IEnumerable <string> docIds = _docIdTable[contractAssembly.AssemblyIdentity.Name.Value]; // Add all the type forwards bool error = false; HashSet <string> existingDocIds = new HashSet <string>(assembly.AllTypes.Select(typeDef => typeDef.RefDocId())); IEnumerable <string> missingDocIds = docIds.Where(id => !existingDocIds.Contains(id)); foreach (string docId in missingDocIds) { IReadOnlyList <INamedTypeDefinition> seedTypes; if (!_typeTable.TryGetValue(docId, out seedTypes)) { if (!ignoreMissingTypes) { Trace.TraceError("Did not find type '{0}' in any of the seed assemblies.", docId); error = true; } continue; } INamedTypeDefinition seedType = GetSeedType(docId, seedTypes); if (seedType == null) { TraceDuplicateSeedTypeError(docId, seedTypes); error = true; continue; } AddTypeForward(assembly, seedType); } if (error) { return(null); } if (_assemblyFileVersion != null) { assembly.AssemblyAttributes.Add(CreateAttribute("System.Reflection.AssemblyFileVersionAttribute", seedCoreAssemblyReference.ResolvedAssembly, _assemblyFileVersion.ToString())); assembly.AssemblyAttributes.Add(CreateAttribute("System.Reflection.AssemblyInformationalVersionAttribute", seedCoreAssemblyReference.ResolvedAssembly, _assemblyFileVersion.ToString())); } if (_buildDesignTimeFacades) { assembly.AssemblyAttributes.Add(CreateAttribute("System.Runtime.CompilerServices.ReferenceAssemblyAttribute", seedCoreAssemblyReference.ResolvedAssembly)); assembly.Flags |= ReferenceAssemblyFlag; } if (_clearBuildAndRevision) { assembly.Version = new Version(assembly.Version.Major, assembly.Version.Minor, 0, 0); } AddWin32VersionResource(contractAssembly.Location, assembly); return(assembly); }
public Assembly GenerateFacade(IAssembly contractAssembly, IAssemblyReference seedCoreAssemblyReference, bool ignoreMissingTypes, IAssembly overrideContractAssembly = null, bool buildPartialReferenceFacade = false) { Assembly assembly; if (overrideContractAssembly != null) { MetadataDeepCopier copier = new MetadataDeepCopier(_seedHost); assembly = copier.Copy(overrideContractAssembly); // Use non-empty partial facade if present } else { MetadataDeepCopier copier = new MetadataDeepCopier(_contractHost); assembly = copier.Copy(contractAssembly); // if building a reference facade don't strip the contract if (!buildPartialReferenceFacade) { ReferenceAssemblyToFacadeRewriter rewriter = new ReferenceAssemblyToFacadeRewriter(_seedHost, _contractHost, seedCoreAssemblyReference, _assemblyFileVersion != null); rewriter.Rewrite(assembly); } } string contractAssemblyName = contractAssembly.AssemblyIdentity.Name.Value; IEnumerable <string> docIds = _docIdTable[contractAssemblyName]; // Add all the type forwards bool error = false; Dictionary <string, INamedTypeDefinition> existingDocIds = assembly.AllTypes.ToDictionary(typeDef => typeDef.RefDocId(), typeDef => typeDef); IEnumerable <string> docIdsToForward = buildPartialReferenceFacade ? existingDocIds.Keys : docIds.Where(id => !existingDocIds.ContainsKey(id)); Dictionary <string, INamedTypeReference> forwardedTypes = new Dictionary <string, INamedTypeReference>(); foreach (string docId in docIdsToForward) { IReadOnlyList <INamedTypeDefinition> seedTypes; if (!_typeTable.TryGetValue(docId, out seedTypes)) { if (!ignoreMissingTypes && !buildPartialReferenceFacade) { Trace.TraceError("Did not find type '{0}' in any of the seed assemblies.", docId); error = true; } continue; } INamedTypeDefinition seedType = GetSeedType(docId, seedTypes); if (seedType == null) { TraceDuplicateSeedTypeError(docId, seedTypes); error = true; continue; } if (buildPartialReferenceFacade) { // honor preferSeedType for keeping contract type string preferredSeedAssembly; bool keepType = _seedTypePreferences.TryGetValue(docId, out preferredSeedAssembly) && contractAssemblyName.Equals(preferredSeedAssembly, StringComparison.OrdinalIgnoreCase); if (keepType) { continue; } assembly.AllTypes.Remove(existingDocIds[docId]); forwardedTypes.Add(docId, seedType); } AddTypeForward(assembly, seedType); } if (buildPartialReferenceFacade) { if (forwardedTypes.Count == 0) { Trace.TraceError("Did not find any types in any of the seed assemblies."); return(null); } else { // for any thing that's now a typeforward, make sure typerefs point to that rather than // the type previously inside the assembly. TypeReferenceRewriter typeRefRewriter = new TypeReferenceRewriter(_seedHost, oldType => { INamedTypeReference newType = null; return(forwardedTypes.TryGetValue(oldType.DocId(), out newType) ? newType : oldType); }); typeRefRewriter.Rewrite(assembly); } } if (error) { return(null); } if (_assemblyFileVersion != null) { assembly.AssemblyAttributes.Add(CreateAttribute("System.Reflection.AssemblyFileVersionAttribute", seedCoreAssemblyReference.ResolvedAssembly, _assemblyFileVersion.ToString())); assembly.AssemblyAttributes.Add(CreateAttribute("System.Reflection.AssemblyInformationalVersionAttribute", seedCoreAssemblyReference.ResolvedAssembly, _assemblyFileVersion.ToString())); } if (_buildDesignTimeFacades) { assembly.AssemblyAttributes.Add(CreateAttribute("System.Runtime.CompilerServices.ReferenceAssemblyAttribute", seedCoreAssemblyReference.ResolvedAssembly)); assembly.Flags |= ReferenceAssemblyFlag; } if (_clearBuildAndRevision) { assembly.Version = new Version(assembly.Version.Major, assembly.Version.Minor, 0, 0); } AddWin32VersionResource(contractAssembly.Location, assembly); return(assembly); }