Skip to content

Commit

Permalink
With IL trimming, missing types is a common scenario and can be nosiy…
Browse files Browse the repository at this point in the history
… if anyone has first chance exceptions enabled for TypeLoadException. Handling it without throwing exception. (#1227)
  • Loading branch information
manodasanW authored Aug 12, 2022
1 parent b18e32e commit 05f36a5
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions src/WinRT.Runtime/TypeNameSupport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
namespace WinRT
{
[Flags]
#if EMBED
internal
#endif
enum TypeNameGenerationFlags
internal enum TypeNameGenerationFlags
{
None = 0,
/// <summary>
Expand All @@ -30,10 +27,7 @@ enum TypeNameGenerationFlags
NoCustomTypeName = 0x2
}

#if EMBED
internal
#endif
static class TypeNameSupport
internal static class TypeNameSupport
{
private static readonly List<Assembly> projectionAssemblies = new List<Assembly>();
private static readonly List<IDictionary<string, string>> projectionTypeNameToBaseTypeNameMappings = new List<IDictionary<string, string>>();
Expand All @@ -45,7 +39,7 @@ public static void RegisterProjectionAssembly(Assembly assembly)
projectionAssemblies.Add(assembly);
}

internal static void RegisterProjectionTypeBaseTypeMapping(IDictionary<string, string> typeNameToBaseTypeNameMapping)
public static void RegisterProjectionTypeBaseTypeMapping(IDictionary<string, string> typeNameToBaseTypeNameMapping)
{
projectionTypeNameToBaseTypeNameMappings.Add(typeNameToBaseTypeNameMapping);
}
Expand Down Expand Up @@ -127,6 +121,10 @@ public static (Type type, int remaining) FindTypeByName(ReadOnlySpan<char> runti
else
{
var (genericTypeName, genericTypes, remaining) = ParseGenericTypeName(runtimeClassName);
if (genericTypeName == null)
{
return (null, -1);
}
return (FindTypeByNameCore(genericTypeName, genericTypes), remaining);
}
}
Expand Down Expand Up @@ -196,9 +194,10 @@ private static Type FindTypeByNameCore(string runtimeClassName, Type[] genericTy
resolvedType = resolvedType.MakeGenericType(genericTypes);
}
return resolvedType;
}

throw new TypeLoadException($"Unable to find a type named '{runtimeClassName}'");
}

Debug.WriteLine($"FindTypeByNameCore: Unable to find a type named '{runtimeClassName}'");
return null;
}

public static Type ResolvePrimitiveType(string primitiveTypeName)
Expand Down Expand Up @@ -257,6 +256,11 @@ private static (string genericTypeName, Type[] genericTypes, int remaining) Pars
{
// Resolve the generic type argument at this point in the parameter list.
var (genericType, endOfGenericArgument) = FindTypeByName(remainingTypeName);
if (genericType == null)
{
return (null, null, -1);
}

remainingIndex += endOfGenericArgument;
genericTypes.Add(genericType);
remainingTypeName = remainingTypeName.Slice(endOfGenericArgument);
Expand Down

0 comments on commit 05f36a5

Please sign in to comment.