I've been doing some searching and been confused with the answers that I find. Pardon my lack of tech jargon.
I'm writing an extension for Mvc.
public static MvcHtmlString FormTextBoxFor<TModel, TProperty>( this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression)
{
var attr = expression.GetAttribute<TModel, TProperty, HtmlPropertyAttribute>();
return helper.TextBoxFor(expression, new { placeholder = attr.Value });
}
public static T GetAttribute<TModel, TProperty, T>(this Expression<Func<TModel, TProperty>> expression)
where T : Attribute
{
var memberExpression = expression.Body as MemberExpression;
if (memberExpression == null)
throw new InvalidOperationException("Expression must be a member expression");
return memberExpression.Member.GetAttribute<T>();
}
What I'm wondering is if I can shortcut passing TModel and TProperty into my second extension because the parameter is already contains that information.
Essentially I want to turn this
expression.GetAttribute<TModel, TProperty, HtmlPropertyAttribute>();
into this
expression.GetAttribute<HtmlPropertyAttribute>();