Hi, I think it's more a C# question than a uComponent question, but I'm trying to register all classes representing a doc type with reflection. I read some stuff regarding open/close generics and the challenge of 'dynamic type resolution' at the runtime but I can't figure out how I can implement it in my case.
Here, is what I'm trying to achieve (all document types inherit from BaseViewModel):
var baseViewModels = Assembly.GetCallingAssembly().GetTypes().Where(t => t.IsSubclassOf(typeof(BaseViewModel))); foreach (var baseViewModel in baseViewModels) { //Doesn't work, just an idea about I'm trying to do... //var genericType = baseViewModel.MakeGenericType(baseViewModel); //uMapper.CreateMap<genericType>(); }
Any idea? I think I'm not the first how thought about it...
Upfront disclaimer: although I maintain uComponents, I didn't develop uMapper - so not sure of it's inner-workings, etc.
From looking at what you are trying to achieve, you could try having a wrapper method in your own code that would take your baseViewModel (no need to call .MakeGenericType()) and then use generics to infer the correct type for the CreateMap call:
public static INodeMappingExpression<TDestination> CreateMap<TDestination>(TDestination baseViewModel)
where TDestination : class, new()
{
return uMapper.CreateMap<TDestination>();
}
I haven't tested this code myself, but hope it helps in pointing you in the right direction.
Create dynamically all mappings with uMapper
Hi,
I think it's more a C# question than a uComponent question, but I'm trying to register all classes representing a doc type with reflection.
I read some stuff regarding open/close generics and the challenge of 'dynamic type resolution' at the runtime but I can't figure out how I can implement it in my case.
Here, is what I'm trying to achieve (all document types inherit from BaseViewModel):
var baseViewModels = Assembly.GetCallingAssembly().GetTypes().Where(t => t.IsSubclassOf(typeof(BaseViewModel)));
foreach (var baseViewModel in baseViewModels)
{
//Doesn't work, just an idea about I'm trying to do...
//var genericType = baseViewModel.MakeGenericType(baseViewModel);
//uMapper.CreateMap<genericType>();
}
Any idea? I think I'm not the first how thought about it...
Thanks for any help!
P.
Hi Pierre,
Upfront disclaimer: although I maintain uComponents, I didn't develop uMapper - so not sure of it's inner-workings, etc.
From looking at what you are trying to achieve, you could try having a wrapper method in your own code that would take your
baseViewModel
(no need to call.MakeGenericType()
) and then use generics to infer the correct type for theCreateMap
call:I haven't tested this code myself, but hope it helps in pointing you in the right direction.
Cheers,
- Lee
is working on a reply...