Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle missing types exception free #1227

Merged
merged 2 commits into from
Aug 12, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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