Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are two ways to do this:</p> <pre><code>Models.Test test = new Models.Test(); string DisplayName = test.GetDisplayName(t =&gt; t.Name); string DisplayName = Helpers.GetDisplayName&lt;Models.Test&gt;(t =&gt; t.Name); </code></pre> <p>The first one works by virtue of writing a generic extension method to any TModel (which is all types). This means it will be available on any object and not just your model. Not really recommended but nice because of it's concise syntax.</p> <p>The second method requires you to pass in the Type of the model it is - which you're already doing but as a parameter instead. This method is required to define type via Generics because Func expects it.</p> <p>Here are the methods for you to check out. </p> <h3>Static extension method to all objects</h3> <pre><code>public static string GetDisplayName&lt;TModel, TProperty&gt;(this TModel model, Expression&lt;Func&lt;TModel, TProperty&gt;&gt; expression) { Type type = typeof(TModel); MemberExpression memberExpression = (MemberExpression)expression.Body; string propertyName = ((memberExpression.Member is PropertyInfo) ? memberExpression.Member.Name : null); // First look into attributes on a type and it's parents DisplayAttribute attr; attr = (DisplayAttribute)type.GetProperty(propertyName).GetCustomAttributes(typeof(DisplayAttribute), true).SingleOrDefault(); // Look for [MetadataType] attribute in type hierarchy // http://stackoverflow.com/questions/1910532/attribute-isdefined-doesnt-see-attributes-applied-with-metadatatype-class if (attr == null) { MetadataTypeAttribute metadataType = (MetadataTypeAttribute)type.GetCustomAttributes(typeof(MetadataTypeAttribute), true).FirstOrDefault(); if (metadataType != null) { var property = metadataType.MetadataClassType.GetProperty(propertyName); if (property != null) { attr = (DisplayAttribute)property.GetCustomAttributes(typeof(DisplayNameAttribute), true).SingleOrDefault(); } } } return (attr != null) ? attr.Name : String.Empty; } </code></pre> <h3>Signature for type specific method - same code as above just different call</h3> <pre><code>public static string GetDisplayName&lt;TModel&gt;(Expression&lt;Func&lt;TModel, object&gt;&gt; expression) { } </code></pre> <p>The reason you can't just use <code>Something.GetDisplayName(t =&gt; t.Name)</code> on it's own is because in the Razor engine you're actually passing an instantiated object of <code>HtmlHelper&lt;TModel&gt;</code> which is why the first method requires an instantiated object - This is only required for the compiler to infer what types belong to which generic name.</p> <h3>Update with recursive properties</h3> <pre><code>public static string GetDisplayName&lt;TModel&gt;(Expression&lt;Func&lt;TModel, object&gt;&gt; expression) { Type type = typeof(TModel); string propertyName = null; string[] properties = null; IEnumerable&lt;string&gt; propertyList; //unless it's a root property the expression NodeType will always be Convert switch (expression.Body.NodeType) { case ExpressionType.Convert: case ExpressionType.ConvertChecked: var ue = expression.Body as UnaryExpression; propertyList = (ue != null ? ue.Operand : null).ToString().Split(".".ToCharArray()).Skip(1); //don't use the root property break; default: propertyList = expression.Body.ToString().Split(".".ToCharArray()).Skip(1); break; } //the propert name is what we're after propertyName = propertyList.Last(); //list of properties - the last property name properties = propertyList.Take(propertyList.Count() - 1).ToArray(); //grab all the parent properties Expression expr = null; foreach (string property in properties) { PropertyInfo propertyInfo = type.GetProperty(property); expr = Expression.Property(expr, type.GetProperty(property)); type = propertyInfo.PropertyType; } DisplayAttribute attr; attr = (DisplayAttribute)type.GetProperty(propertyName).GetCustomAttributes(typeof(DisplayAttribute), true).SingleOrDefault(); // Look for [MetadataType] attribute in type hierarchy // http://stackoverflow.com/questions/1910532/attribute-isdefined-doesnt-see-attributes-applied-with-metadatatype-class if (attr == null) { MetadataTypeAttribute metadataType = (MetadataTypeAttribute)type.GetCustomAttributes(typeof(MetadataTypeAttribute), true).FirstOrDefault(); if (metadataType != null) { var property = metadataType.MetadataClassType.GetProperty(propertyName); if (property != null) { attr = (DisplayAttribute)property.GetCustomAttributes(typeof(DisplayNameAttribute), true).SingleOrDefault(); } } } return (attr != null) ? attr.Name : String.Empty; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload