Skip to content

Creating Property of Generic Type fails when creating an Assembly using Reflection.Emit and MetadataLoadContext. #112155

@AugustoRuiz

Description

@AugustoRuiz

Describe the bug

Cannot create a property of a Generic Type whose Type parameter is created using Reflection.Emit, when creating an assembly using MetadataLoadContext to use Reference Assemblies.

I am creating a DLL using Reflection.Emit from a JSON based description. This DLL has entity types that have properties with basic types, or with types already defined in the assembly that is being created. That works, but if the property is of a generic type, such as a List, and the generic type parameter is a type defined in the assembly being created, a System.ArgumentException is raised when invoking Type.MakeGenericType([entityType]).

The error states:

This type 'Type: <entityType>' was not loaded by the MetadataLoadContext that loaded the generic type or method'.
   at System.Reflection.TypeLoading.RoDefinitionType.MakeGenericType(Type[] typeArguments)

To Reproduce

See the repository:

Repo in GitHub

Exceptions (if any)

System.ArgumentException:

An unhandled exception of type 'System.ArgumentException' occurred in System.Reflection.MetadataLoadContext.dll: 'This type 'Type: PurchaseOrderItem' was not loaded by the MetadataLoadContext that loaded the generic type or method.' at System.Reflection.TypeLoading.RoDefinitionType.MakeGenericType(Type[] typeArguments) at Program.<Main>$(String[] args) in ...\MetadataLoadContextError\Program.cs:line 37

Further technical details

  • Include the output of dotnet --info
SDK DE .NET:
 Version:           9.0.101
 Commit:            eedb237549
 Workload version:  9.0.100-manifests.3068a692
 MSBuild version:   17.12.12+1cce77968

Entorno de tiempo de ejecución:
 OS Name:     Windows
 OS Version:  10.0.19045
 OS Platform: Windows
 RID:         win-x64
 Base Path:   C:\Program Files\dotnet\sdk\9.0.101\

Cargas de trabajo de .NET instaladas:
No hay cargas de trabajo instaladas para mostrar.
Configurado para usar loose manifests al instalar nuevos manifiestos.

Host:
  Version:      9.0.0
  Architecture: x64
  Commit:       9d5a6a9aa4

.NET SDKs installed:
  8.0.300 [C:\Program Files\dotnet\sdk]
  9.0.101 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 8.0.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 9.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 6.0.29 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 9.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 6.0.29 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 8.0.5 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 9.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
  x86   [C:\Program Files (x86)\dotnet]
    registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables:
  Not set

global.json file:
  Not found

Learn more:
  https://aka.ms/dotnet/info

Download .NET:
  https://aka.ms/dotnet/download
  • The IDE (VS / VS Code/ VS4Mac) you're running on, and its version
Version: 1.96.4 (user setup)
Commit: cd4ee3b1c348a13bafd8f9ad8060705f6d4b9cba
Date: 2025-01-16T00:16:19.038Z
Electron: 32.2.6
ElectronBuildId: 10629634
Chromium: 128.0.6613.186
Node.js: 20.18.1
V8: 12.8.374.38-electron.0
OS: Windows_NT x64 10.0.19045

Metadata

Metadata

Assignees

Labels

area-System.Reflectionbugin-prThere is an active PR which will close this issue when it is merged

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions