diff --git a/src/GenFacades/Program.cs b/src/GenFacades/Program.cs index 36cfb81a00..c445e9e6a4 100644 --- a/src/GenFacades/Program.cs +++ b/src/GenFacades/Program.cs @@ -485,21 +485,24 @@ private void AddTypeForward(Assembly assembly, INamedTypeDefinition seedType, IA assembly.ExportedTypes = new List(); assembly.ExportedTypes.Add(alias); - INamedTypeDefinition contractType = contractAssembly.GetAllTypes().Where(t => t.FullName() == seedType.FullName()).FirstOrDefault(); - if (contractType == null) - throw new Exception("Type was not found in the contract."); - - // Recursively add forwarders for all the nested types specified in the contract assembly. + // Recursively add forwarders for nested types. // NOTE: Some design-time tools can resolve forwarded nested types with only the top-level forwarder, // but the runtime currently throws a TypeLoadException without explicit forwarders for the nested // types. - foreach (var nestedType in contractType.NestedTypes.OrderBy(t => t.Name.Value)) + INamedTypeDefinition contractType = contractAssembly.GetAllTypes().Where(t => t.FullName() == seedType.FullName()).FirstOrDefault(); + if (contractType != null) // Only add forwarders for the nested types on the contract { - var seedNestedType = seedType.NestedTypes.Where(nt => nt.Name.Value == nestedType.Name.Value).FirstOrDefault(); - if (seedNestedType == null) - throw new Exception("Nested type found in the contract, but not on the seedType."); - AddTypeForward(assembly, seedNestedType, contractAssembly); + foreach (var nestedType in contractType.NestedTypes.OrderBy(t => t.Name.Value)) + { + var seedNestedType = seedType.NestedTypes.Where(nt => nt.Name.Value == nestedType.Name.Value).FirstOrDefault(); + if (seedNestedType == null) + throw new Exception("Nested type found in the contract, but not on the seedType."); + AddTypeForward(assembly, seedNestedType, contractAssembly); + } } + else // Add all the nested types for ExportedTypes. + foreach (var nestedType in seedType.NestedTypes.OrderBy(t => t.Name.Value)) + AddTypeForward(assembly, nestedType, contractAssembly); } private void AddWin32VersionResource(string contractLocation, Assembly facade)