This question already has an answer here:
- Partial type inference 3 answers
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, its more a fact finding mission
Update
For those of you playing at home, with the help of scotts comment i managed to find an additional solve 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));
}
However AutoMapper aside, this is not the answer to the actual question and will mark on merit accordingly
In case you are wondering why this is just not possible, I'd think the problem lies with ambiguity:
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.
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.
If you introduce a second (chained) function call, you can achieve this:
Inferred generic parameters is an all-or-nothing deal; if there aren't enough "actual" parameters passed into the method for the compiler to figure out the all of generic parameters, the compiler forces you to specify each generic parameters manually.
To be able to use the above snippet, you need to have one method call with one (the only) generic parameter inferred, and one method call with one manually specified generic parameter.
The
To<>
method can be implemented like this: