-1

in old application i use seperate map for convert any model to view model and inverse. now i want use generic function but generic function not call sightly map

public class GenericBll<TVModel, TMModel> where TVModel : class where TMModel : class
{
    public virtual IEnumerable<TVModel> GetAll()
    {
        var a = Instance.GetAll_asQuery().ToList();
        var b = a.Select(q=> Mapper.Map<TVModel,TMModel>(q)).ToList();
        //mapper not return true thing
        return b;
    }
}

it is my generic mapper

public partial class Mapper
{
    internal static TVModel Map<TMModel>(TMModel q)  where TMModel : class where TVModel : class
    {
    //want to this function call another but always run this :(
        throw new NotImplementedException();
    }

    public static MM.GroupDevides Map(GroupDevides e)
    {
        //map to MM.GroupDevides
    }
    public static GroupDevides Map(MM.GroupDevides e)
    {
         //map to GroupDevides
    }
}

i,m new in generic type please help

1 Answers1

1

I'm not sure, what you are trying to do, but if you want to call Mapper.Map(q) where q is of type GroupDevides somewhere, it uses the generic method and passes the parameter casted to TMModel.

If you want to call Mapper.Map(GroupDevides e), you need to call it directly or let Mapper.Map(q) call it based on the type of q. Code for that would look something like that:

class Mapper
{
    public static object Map<TMModel>(TMModel q) where TMModel : class
    {
        MethodInfo methodInfo = typeof(Mapper).GetMethod("Map", new Type[] { q.GetType() });

        if (methodInfo != null)
            return methodInfo.Invoke(null, new object[] { q });

        return null;
    }

    public static GroupDevides Map(MM.GroupDevides e)
    {
        //map to GroupDevides
    }
}