Skip to content

Using TypeBuilder as a generic type parameter throws NotSupportedException in AssemblyBuilder created by DefinePersistedAssembly #99505

@mateuszradny

Description

@mateuszradny

Description

Creating types which use dynamically create types as generic types parameters throws exceptions as below:

System.NotSupportedException: Specified method is not supported. at System.Reflection.Emit.TypeBuilderInstantiation.GetConstructorImpl(BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) at System.Reflection.Emit.TypeBuilderImpl.DefineDefaultConstructorInternal(MethodAttributes attributes) at System.Reflection.Emit.TypeBuilderImpl.CreateTypeInfoCore() at Handyflows.Core.Customization.Snapshots.DummyCustomizationSnapshotProvider..ctor()

Reproduction Steps

public class BaseType<T> { }

// code
var assemblyBuilder = AssemblyBuilder.DefinePersistedAssembly(new AssemblyName("test"), typeof(object).Assembly);
var module = assemblyBuilder.DefineDynamicModule("test");
var typeBuilder = module
    .DefineType("test.Test", TypeAttributes.Public | TypeAttributes.Class);

var type = typeBuilder.CreateType();
var baseType = typeof(BaseType<>).GetGenericTypeDefinition().MakeGenericType(type);

var typeBuilder2 = module
    .DefineType("TestService",
    TypeAttributes.Public | TypeAttributes.Class,
    baseType 
    );

typeBuilder2.CreateType(); // exception

Expected behavior

Actual behavior

The same code for DefineDynamicAssembly works fine, but problem exists only for DefinePersistedAssembly.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions