I would like to create the .Net Core class library that will be contains following extension method:
public static class MyServiceExtensions
{
public static IServiceCollection AddMyService<TUserDto, TUserDtoKey, TUser, TUserKey>(this IServiceCollection services)
where TUserDto : UserDto<TUserDtoKey>
where TUser : User<TUserKey>
{
services.AddAutoMapper(config =>
{
config.AddProfile<UserMappingProfile<TUserDto, TUserDtoKey, TUser, TUserKey>>();
});
return services;
}
}
I have following Automapper Profile:
public class UserMappingProfile<TUserDto, TUserDtoKey, TUser, TUserKey> : Profile
where TUserDto : UserDto<TUserDtoKey>
where TUser : User<TUserKey>
{
public UserMappingProfile()
{
CreateMap<TUserDto, TUser>(MemberList.Destination)
.ForMember(x => x.Id, opts => opts.MapFrom(x => x.UserId));
CreateMap<TUser, TUserDto > (MemberList.Source)
.ForMember(x => x.UserId, opts => opts.MapFrom(x => x.Id));
}
}
These entities:
public class UserDto<TKey>
{
public TKey UserId { get; set; }
public string UserName { get; set; }
}
public class User<TKey>
{
public TKey Id { get; set; }
public string UserName { get; set; }
}
public class MyUser : User<int>
{
public string Email { get; set; }
}
public class MyUserDto : UserDto<int>
{
public string Email { get; set; }
}
If I try to use it like this:
services.AddMyService<MyUserDto, int, MyUser, int>();
I get this error:
{System.ArgumentException: Cannot create an instance of GenericMapping.Services.Mapping.UserMappingProfile
4[TUserDto,TUserDtoKey,TUser,TUserKey] because Type.ContainsGenericParameters is true. at System.RuntimeType.CreateInstanceCheckThis() at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean wrapExceptions, Boolean skipCheckThis, Boolean fillCache) at System.Activator.CreateInstance(Type type, Boolean nonPublic, Boolean wrapExceptions) at AutoMapper.Configuration.MapperConfigurationExpression.AddProfile(Type profileType) in C:\projects\automapper\src\AutoMapper\Configuration\MapperConfigurationExpression.cs:line 44 at AutoMapper.ServiceCollectionExtensions.<>c__DisplayClass10_0.<AddAutoMapperClasses>g__ConfigAction|4(IMapperConfigurationExpression cfg) in C:\projects\automapper-extensions-microsoft-dependencyinjectio\src\AutoMapper.Extensions.Microsoft.DependencyInjection\ServiceCollectionExtensions.cs:line 83 at AutoMapper.MapperConfiguration.Build(Action1 configure) in C:\projects\automapper\src\AutoMapper\MapperConfiguration.cs:line 307 at AutoMapper.ServiceCollectionExtensions.AddAutoMapperClasses(IServiceCollection services, Action1 additionalInitAction, IEnumerable1 assembliesToScan) in C:\projects\automapper-extensions-microsoft-dependencyinjectio\src\AutoMapper.Extensions.Microsoft.DependencyInjection\ServiceCollectionExtensions.cs:line 89 at GenericMapping.Services.Extensions.MyServiceExtensions.AddMyService[TUserDto,TUserDtoKey,TUser,TUserKey](IServiceCollection services) in C:\Projects\GenericMapping\GenericMapping.Services\Extensions\MyServiceExtensions.cs:line 14 at GenericMapping.Startup.ConfigureServices(IServiceCollection services) in C:\Projects\GenericMapping\GenericMapping\Startup.cs:line 33}
How can I fix this issue?