Repository: dotnet/runtime
File: src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/TypeMapLazyDictionary.cs (C#)

Code before:
                    Utf8TypeNameLen = arg->StringLen2
                };
                context->ProxyTypeMap.Add(parsedSource, sourceTypeName, proxyTypeName, context->CurrentAssembly);
            }
            catch (Exception ex)
            {
                context->CreationException = ExceptionDispatchInfo.Capture(ex);
                return Interop.BOOL.FALSE; // Stop processing.
            }
            finally
            {
                sourceTypeBuffer.Dispose();
            }

            return Interop.BOOL.TRUE; // Continue processing.
        }

        private static unsafe CallbackContext CreateMaps(
            RuntimeType groupType,
            delegate* unmanaged<CallbackContext*, ProcessAttributesCallbackArg*, Interop.BOOL> newExternalTypeEntry,
            delegate* unmanaged<CallbackContext*, ProcessAttributesCallbackArg*, Interop.BOOL> newProxyTypeEntry)
        {
            RuntimeAssembly? startingAssembly = (RuntimeAssembly?)Assembly.GetEntryAssembly();
            if (startingAssembly is null)
            {
                throw new InvalidOperationException(SR.InvalidOperation_TypeMapMissingEntryAssembly);
            }

            CallbackContext context;
            ProcessAttributes(
                new QCallAssembly(ref startingAssembly),
                new QCallTypeHandle(ref groupType),
                newExternalTypeEntry,
                newProxyTypeEntry,
                &context);

            // If an exception was thrown during the processing of
            // the attributes, rethrow it.
            context.CreationException?.Throw();

            return context;
        }

        public static IReadOnlyDictionary<string, Type> CreateExternalTypeMap(RuntimeType groupType)
        {
            unsafe
            {
                return CreateMaps(
                    groupType,
                    &NewExternalTypeEntry,
                    null).ExternalTypeMap;

Review comment (suggestion):
This is odd. The `as` operator implies the returned type could be a non `RuntimeAssembly`. I don't believe that is true. This is a confusing cast. It should just be `(RuntimeAssembly)`.

Code after:
                    Utf8TypeNameLen = arg->StringLen2
                };
                context->ProxyTypeMap.Add(parsedSource, sourceTypeName, proxyTypeName, context->CurrentAssembly);
            }
            catch (Exception ex)
            {
                context->CreationException = ExceptionDispatchInfo.Capture(ex);
                return Interop.BOOL.FALSE; // Stop processing.
            }
            finally
            {
                sourceTypeBuffer.Dispose();
            }

            return Interop.BOOL.TRUE; // Continue processing.
        }

        private static unsafe CallbackContext CreateMaps(
            RuntimeType groupType,
            delegate* unmanaged<CallbackContext*, ProcessAttributesCallbackArg*, Interop.BOOL> newExternalTypeEntry,
            delegate* unmanaged<CallbackContext*, ProcessAttributesCallbackArg*, Interop.BOOL> newProxyTypeEntry)
        {
            RuntimeAssembly? startingAssembly;
            if (AppContext.GetData("System.Runtime.InteropServices.TypeMappingEntryAssembly") is string entryAssemblyName)
            {
                startingAssembly = Assembly.Load(entryAssemblyName) as RuntimeAssembly;
            }
            else
            {
                startingAssembly = Assembly.GetEntryAssembly() as RuntimeAssembly;
            }

            if (startingAssembly is null)
            {
                throw new InvalidOperationException(SR.InvalidOperation_TypeMapMissingEntryAssembly);
            }

            CallbackContext context;
            ProcessAttributes(
                new QCallAssembly(ref startingAssembly),
                new QCallTypeHandle(ref groupType),
                newExternalTypeEntry,
                newProxyTypeEntry,
                &context);

            // If an exception was thrown during the processing of
            // the attributes, rethrow it.
            context.CreationException?.Throw();

            return context;
        }