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
    Pierre
    0

    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.

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    May 26, 2014 @ 12:32
    Lee Kelleher
    0

    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.

    Cheers,
    - Lee

Please Sign in or register to post replies

Write your reply to:

Draft