Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass two generics types into an extension method [duplicate]

I've 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 the following:

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

However i'm wondering if there is anyway I can modify the extension method so i can call a shorted version as follows:

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

Please Note:

Whether or not automapper should be used like this is not in the scope of the question, it's more a fact finding mission.


Update

For those of you playing at home, with the help of Scott's comment I managed to find an additional solution for the above function at Generic extension method for automapper.

public static T Map<T>(this IEntity entity) 
{
    return (T)Mapper.Map(entity, entity.GetType(), typeof(T));  
}
like image 920
TheGeneral Avatar asked May 09 '14 04:05

TheGeneral


People also ask

How can use generic extension method in C#?

Extension Methods is a new feature in C# 3.0 and the Common Language Runtime, which allows existing classes to use extra methods (extension methods) without being implemented in the same class or obtained by inheritance.

What is generic extension?

Generics are a way to tailor a class or method to a specific type. A generic method or class is designed to work for any type. This is most easily illustrated in the List<T> class, where it can be tailored to be a list of any type. This gives you the type-safety of knowing the list only contains that specific type.

How to make method generics in java?

Generic Methods All generic method declarations have a type parameter section delimited by angle brackets (< and >) that precedes the method's return type ( < E > in the next example). Each type parameter section contains one or more type parameters separated by commas.

What constrains can be applied to generics c#?

The where clause in a generic definition specifies constraints on the types that are used as arguments for type parameters in a generic type, method, delegate, or local function. Constraints can specify interfaces, base classes, or require a generic type to be a reference, value, or unmanaged type.


2 Answers

In case you are wondering why this is just not possible, I'd think the problem lies with ambiguity:

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)
{
    // ...
}

So, which method gets called? Keep in mind this is just a simple example. It's very well possible that there could be a future implementation of partial type inference, but I'd imagine it would be too confusing when it comes to overload resolution and the cost/benefit would be completely out of control. Then again, that's just speculation.

like image 81
myermian Avatar answered Sep 28 '22 22:09

myermian


What you're asking for is not possible. When you call a generic method, either all generic type parameters must be inferable from the input or you must specify them all explicitly. Your method only has one input so you can't possibly infer two generic type parameters, thus they must always both be specified explicitly. Your suggested code implies that the method has only one generic type parameter, which would make it a different method.

like image 39
jmcilhinney Avatar answered Sep 29 '22 00:09

jmcilhinney