I have this (in a C# MVC project):
public class Bindable<TEntity>
{
//(...)
public IHtmlString FormControlTextField<T>(string id, Expression<Func<TEntity, T>> member)
{
var prop = (member as MemberExpression).Member;
var strLen = prop.GetCustomAttributes(typeof(StringLengthAttribute), false).OfType<StringLengthAttribute>().FirstOrDefault();
//(...)
}
//(...)
}
and for some weird reason to me, it is throwing a NullReferenceException on the method's second line, since it's returning null from the cast on the first line.
The method is being called like this (from a View):
@empresa.FormControlTextField(p => p.CNPJ, Model.CNPJ)
@* "empresa" is an instance of Bindable<EmpresaCliente> *@
@* "CNPJ" is a property in EmpresaCliente *@
What is wrong with this? Or, is there a workaround to test "p.CNPJ" for custom attributes?