How to pass 2 types of generics to an extension method

I created the following extension method

public static T Map<TEntity,T>(this TEntity entity) where TEntity : IEntity
{
    return Mapper.Map<TEntity, T>(entity);        
}

This allows you to use

Db.ExchangeSets.FirstOrDefault().Map<ExchangeSet, ExchangeSetSimpleViewModel>()

However, I am wondering if there is a way to change the extension method, so I can name the short version as follows

Db.ExchangeSets.FirstOrDefault().Map<ExchangeSetSimpleViewModel>()

Note:

Whether to use an autoperper like this is outside the scope of the question, the larger the fact-finding mission

<h / "> Update

For those of you who play at home, using scotts comment, I managed to find an additional option for the above function in the Universal extension method for automapper

public static T Map<T>(this IEntity entity) 
{
    return (T)Mapper.Map(entity, entity.GetType(), typeof(T));  
}

However, AutoMapper aside, this is not the answer to the real question and will be honored accordingly

+4
3

, , , :

public static T Map<TEntity,T>(this TEntity entity) where TEntity : IEntity
{
    return Mapper.Map<TEntity, T>(entity);        
}

public static T Map<T>(this ExchangeSet set)
{
    // ...
}

, ? , . , , , , , / - . , .

+2

, , . , , . , , . , , .

+1

() , :

Db.ExchangeSets.FirstOrDefault().Map().To<ExchangeSetSimpleViewModel>()

" "; "" , , .

To be able to use the above snippet, you need to have one method call with one (single) common parameter and one method call with one manually specified parameter.

The method To<>can be implemented as follows:

public static MapExtensionHelper<TEntity> Map<TEntity>(this TEntity entity) where TEntity : IEntity
{
    return new MapExtensionHelper<TEntity>(entity);        
}

public class MapExtensionHelper<TEntity> where TEntity : IEntity
{
    public MapExtensionHelper(TEntity entity)
    {
        _entity = entity;
    }

    private readonly TEntity _entity;

    public T To<T>()
    {
        return Mapper.Map<TEntity, T>(_entity);
    }
}
+1
source

Source: https://habr.com/ru/post/1539855/


All Articles