This is a design question, I have this extension method:
public static IQueryable<R> ToViewModels<T,R>(this IQueryable<T> DBModels_Q)
{
//calling another method (irrilevant for this question)
return ToViewModels<T, R>().Invoke(DBModels_Q.AsExpandable());
}
This method works really well if called with:
//db is the EntityFramework context and Employee is the DB table
db.Employee.ToViewModels<DBModels.Employee, ViewModels.Employee>();
I'm wondering if there is a way to use it without specifying the type of T:
db.Employee.ToViewModels<ViewModels.Employee>();
something like How do I get the calling method name and type using reflection? would be usefull.
UPDATE
like suggested the answer was at Partial generic type inference possible in C#?
sadly the only way is to wrap everything and use 2 methods:
public static ViewModelsWrapper<TSource> LoadViewModels<TSource> (this IQueryable<TSource> DBModels_Q)
{
return new ViewModelsWrapper<TSource>(DBModels_Q);
}
public class ViewModelsWrapper<TSource>
{
private readonly IQueryable<TSource> DBModels_Q;
public ViewModelsWrapper(IQueryable<TSource> DBModels_Q)
{
this.DBModels_Q = DBModels_Q;
}
public IQueryable<TResult> GetViewModels<TResult>()
{
return ToModels<TSource, TResult>().Invoke(this.DBModels_Q.AsExpandable());
}
}
using it like
sv.db.Employee.LoadViewModels().GetViewModels<EmployeeModel>();