diff --git a/src/Generator/Generators/CLI/CLIHeadersTemplate.cs b/src/Generator/Generators/CLI/CLIHeadersTemplate.cs index 3792b926..6806f651 100644 --- a/src/Generator/Generators/CLI/CLIHeadersTemplate.cs +++ b/src/Generator/Generators/CLI/CLIHeadersTemplate.cs @@ -97,7 +97,13 @@ namespace CppSharp.Generators.CLI // Create a new tree of namespaces out of the type references found. var rootNamespace = new TranslationUnit(); - foreach (var typeRef in typeReferences) + var sortedRefs = typeReferences.ToList(); + sortedRefs.Sort((ref1, ref2) => + string.CompareOrdinal(ref1.FowardReference, ref2.FowardReference)); + + var forwardRefs = new SortedSet(); + + foreach (var typeRef in sortedRefs) { if (string.IsNullOrWhiteSpace(typeRef.FowardReference)) continue; @@ -106,6 +112,12 @@ namespace CppSharp.Generators.CLI if (!(declaration.Namespace is Namespace)) continue; + if (!forwardRefs.Add(typeRef.FowardReference)) + continue; + + if (typeRef.Include.InHeader) + continue; + var @namespace = FindCreateNamespace(rootNamespace, declaration); @namespace.TypeReferences.Add(typeRef); }