With this definition
public static HtmlTable<TRowModel> DisplayTable<TModel, TRows, TRowModel>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TRows>> expression, TRowModel rowModel = default(TRowModel)) where TRows : IEnumerable<TRowModel>
{
Func<TModel, TRows> deleg = expression.Compile();
TRows result = deleg(helper.ViewData.Model);
return new HtmlTable<TRowModel>(helper.ViewData.Model, result);
}
i can call my extensions method like this
@(Html.DisplayTable(m => m.ListTest, new RowViewModel()).Render())
I would like to be able to explicitly specify only the TRowModel
type so I can call my extension like so
@(Html.DisplayTable<RowViewModel>(m => m.ListTest).Render())
or, even better, like so
@(Html.DisplayTable(m => m.ListTest).Render())
where RowViewModel
would be infered from the fact that I restricted my lambda parameter with where TRows : IEnumerable<TRowModel>
Is this possible in C#6 ? If not, what are my alternatives to avoid passing an empty object just so I don't have to specify explicitly every type in the diamond ?
Note: this post is only meant to let me tag this question as answered
Answer: In C#, when generics are involved they must ALL be specified explicitly except if they can ALL be inferred usage call. The this
parameter is the only exception to this rule.
Thanks to @Peter B and @Atlasmaybe for your answers.