Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at

  • Pierre 2 posts 22 karma points
    May 25, 2014 @ 20:31

    Create dynamically all mappings with uMapper

    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);

    Any idea? I think I'm not the first how thought about it...

    Thanks for any help!


  • Lee Kelleher 4013 posts 15773 karma points MVP 12x admin c-trib
    May 26, 2014 @ 12:32
    Lee Kelleher

    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 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.

    - Lee

Please Sign in or register to post replies

Write your reply to: