Skip to content

TypeAdapterConfig.GlobalSettings.Compile() throws exception when open generics are configured #925

@cjung95

Description

@cjung95

When a mapping for a generic type is configured like described here TypeAdapterConfig.GlobalSettings.Compile() throws a Mapster.CompileException: 'Operation is not valid due to the current state of the object.' with an inner ArgumentException: Type ClassA`1[T] is a generic type definition (Parameter 'type').

Minimal working example:

using Mapster;

TypeAdapterConfig.GlobalSettings.ForType(typeof(ClassA<>), typeof(ClassB<>));
TypeAdapterConfig.GlobalSettings.Compile();

var classA = new ClassA<int> { Variable = 15 };
var classB = classA.Adapt<ClassB<int>>();

class ClassA<T> { public T? Variable { get; set; } = default; }
class ClassB<T> { public T? Variable { get; set; } = default; }

When removing TypeAdapterConfig.GlobalSettings.Compile(); the mapping works just fine.

The exception is thrown in Mapster.Adapters.BaseAdapter.CreateAdaptFunc(CompileArgument arg) when calling Expression.Parameter(arg.SourceType).

Mapster version 10.0.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions