web-dev-qa-db-fra.com

Récupère l'attribut [DisplayName] d'une propriété de manière fortement typée

Bonne journée!

J'ai une telle méthode pour obtenir la valeur d'attribut [DisplayName] d'une propriété (qui est attachée directement ou qui utilise l'attribut [MetadataType]). Je l'utilise dans de rares cas où j'ai besoin d'obtenir [DisplayName] dans le code du contrôleur.

public static class MetaDataHelper
{
    public static string GetDisplayName(Type dataType, string fieldName)
    {       
        // First look into attributes on a type and it's parents
        DisplayNameAttribute attr;
        attr = (DisplayNameAttribute)dataType.GetProperty(fieldName).GetCustomAttributes(typeof(DisplayNameAttribute), 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)dataType.GetCustomAttributes(typeof(MetadataTypeAttribute), true).FirstOrDefault();
            if (metadataType != null)
            {
                var property = metadataType.MetadataClassType.GetProperty(fieldName);
                if (property != null)
                {
                    attr = (DisplayNameAttribute)property.GetCustomAttributes(typeof(DisplayNameAttribute), true).SingleOrDefault();
                }
            }
        }
        return (attr != null) ? attr.DisplayName : String.Empty;
    }
}

Cela fonctionne, mais il a deux inconvénients:

  • Il faut un nom de champ sous forme de chaîne
  • Cela ne fonctionne pas si je veux obtenir la propriété d'une propriété

Est-il possible de surmonter les deux problèmes en utilisant lambdas, comme dans ASP.NET MVC:

Html.LabelFor(m => m.Property.Can.Be.Very.Complex.But.Strongly.Typed);  

Mettre à jour

Voici une version mise à jour et vérifiée de la solution BuildStarted. Il est modifié pour utiliser l'attribut DisplayName (vous pouvez revenir à l'attribut Display si vous l'utilisez). Et corrigé des bugs mineurs pour obtenir l'attribut des propriétés imbriquées.

public static string GetDisplayName<TModel>(Expression<Func<TModel, object>> expression)
{
    Type type = typeof(TModel);

    string propertyName = null;
    string[] properties = null;
    IEnumerable<string> 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

    foreach (string property in properties)
    {
        PropertyInfo propertyInfo = type.GetProperty(property);
        type = propertyInfo.PropertyType;
    }

    DisplayNameAttribute attr;
    attr = (DisplayNameAttribute)type.GetProperty(propertyName).GetCustomAttributes(typeof(DisplayNameAttribute), 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 = (DisplayNameAttribute)property.GetCustomAttributes(typeof(DisplayNameAttribute), true).SingleOrDefault();
            }
        }
    }
    return (attr != null) ? attr.DisplayName : String.Empty;
}
39
artvolk

Il y a deux façons de faire ça:

Models.Test test = new Models.Test();
string DisplayName = test.GetDisplayName(t => t.Name);

string DisplayName = Helpers.GetDisplayName<Models.Test>(t => t.Name);

Le premier fonctionne en vertu de l'écriture d'une méthode d'extension générique sur n'importe quel TModel (qui est tous les types). Cela signifie qu'il sera disponible sur n'importe quel objet et pas seulement sur votre modèle. Pas vraiment recommandé mais agréable à cause de sa syntaxe concise.

La deuxième méthode nécessite que vous transmettiez le type du modèle - ce que vous faites déjà, mais en tant que paramètre. Cette méthode est nécessaire pour définir le type via Generics, car Func l'attend.

Voici les méthodes pour que vous puissiez vérifier. 

Méthode d'extension statique à tous les objets

public static string GetDisplayName<TModel, TProperty>(this TModel model, Expression<Func<TModel, TProperty>> 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;


}

Signature pour la méthode spécifique au type - même code que ci-dessus, juste un appel différent

public static string GetDisplayName<TModel>(Expression<Func<TModel, object>> expression) { }

La raison pour laquelle vous ne pouvez pas simplement utiliser Something.GetDisplayName(t => t.Name) seul est que, dans le moteur Razor, vous transmettez en fait un objet instancié de HtmlHelper<TModel>, raison pour laquelle la première méthode requiert un objet instancié - Ceci est uniquement requis pour que le compilateur puisse déduire quels types appartenir à quel nom générique.

Mise à jour avec des propriétés récursives

public static string GetDisplayName<TModel>(Expression<Func<TModel, object>> expression) {

    Type type = typeof(TModel);

    string propertyName = null;
    string[] properties = null;
    IEnumerable<string> 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;



}
37
Buildstarted

En retard au jeu, mais ...

J'ai créé une méthode d'assistance à l'aide de ModelMetadata, comme le mentionnait @Daniel, et j'ai pensé la partager:

public static string GetDisplayName<TModel, TProperty>(
      this TModel model
    , Expression<Func<TModel, TProperty>> expression)
{
    return ModelMetadata.FromLambdaExpression<TModel, TProperty>(
        expression,
        new ViewDataDictionary<TModel>(model)
        ).DisplayName;
}

Exemple d'utilisation:

Models:

public class MySubObject
{
    [DisplayName("Sub-Awesome!")]
    public string Sub { get; set; }
}

public class MyObject
{
    [DisplayName("Awesome!")]
    public MySubObject Prop { get; set; }
}

Use:

HelperNamespace.GetDisplayName(Model, m => m.Prop) // "Awesome!"
HelperNamespace.GetDisplayName(Model, m => m.Prop.Sub) // "Sub-Awesome!"
30
JesseBuesking

Faites juste ceci:

using System.ComponentModel;
using System.Linq;
using System.Reflection;

namespace yournamespace
{
    public static class ExtensionMethods
    {
        public static string GetDisplayName(this PropertyInfo prop)
        {
            if (prop.CustomAttributes == null || prop.CustomAttributes.Count() == 0)
                return prop.Name;

            var displayNameAttribute = prop.CustomAttributes.Where(x => x.AttributeType == typeof(DisplayNameAttribute)).FirstOrDefault();

            if (displayNameAttribute == null || displayNameAttribute.ConstructorArguments == null || displayNameAttribute.ConstructorArguments.Count == 0)
                return prop.Name;

            return displayNameAttribute.ConstructorArguments[0].Value.ToString() ?? prop.Name;
        }
    }
}

Exemple à la demande:

var props = typeof(YourType).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(p => p.CanRead);

var propFriendlyNames = props.Select(x => x.GetDisplayName());
4
Joe

Je suis totalement d'accord avec la solution fournie par BuildStarted. La seule chose que je changerais, c’est que la méthode ExtensionsMethode ne prend pas en charge les traductions. Pour soutenir cela, il est nécessaire d'apporter des modifications mineures. J'aurais mis cela dans les commentaires mais je n'ai pas assez de points pour le faire. Recherchez la dernière ligne de la méthode.

La méthode d'extension

public static string GetDisplayName<TModel, TProperty>(this TModel model, Expression<Func<TModel, TProperty>> expression)
{
        Type type = typeof(TModel);
        IEnumerable<string> 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
        string propertyName = propertyList.Last();
        //list of properties - the last property name
        string[] properties = propertyList.Take(propertyList.Count() - 1).ToArray();

        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 = (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();
                }
            }
        }
        //To support translations call attr.GetName() instead of attr.Name
        return (attr != null) ? attr.GetName() : String.Empty;
 }
2
Bosken85

J'ai trouvé un autre extrait de code Nice ici , et je l'ai légèrement modifié pour le but "Affichage"

    public static string GetDisplayName<TSource, TProperty>(Expression<Func<TSource, TProperty>> expression)
    {
        var attribute = Attribute.GetCustomAttribute(((MemberExpression)expression.Body).Member, typeof(DisplayNameAttribute)) as DisplayNameAttribute;

        if (attribute == null)
        {
            throw new ArgumentException($"Expression '{expression}' doesn't have DisplayAttribute");
        }

        return attribute.DisplayName;
    }

Et les usages

GetDisplayName<ModelName, string>(i => i.PropertyName)
1
Shuvo Amin

Je fais un petit changement est que vous utilisez des ressources pour obtenir le nom d'affichage

    public static string GetDisplayName<TModel>(Expression<Func<TModel, object>> expression)
  {

     string _ReturnValue = string.Empty;

     Type type = typeof(TModel);

     string propertyName = null;
     string[] properties = null;
     IEnumerable<string> 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();
           }
        }
     }

     if (attr != null && attr.ResourceType != null)
        _ReturnValue = attr.ResourceType.GetProperty(attr.Name).GetValue(attr).ToString();
     else if (attr != null)
        _ReturnValue = attr.Name;

     return _ReturnValue;
  }

Code heureux

1
Fernando

Un autre extrait de code avec du code .Net s’utilise lui-même pour

public static class WebModelExtensions
{
    public static string GetDisplayName<TModel, TProperty>(
      this HtmlHelper<TModel> html, 
      Expression<Func<TModel, TProperty>> expression)
    {
        // Taken from LabelExtensions
        var metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);

        string displayName = metadata.DisplayName;
        if (displayName == null)
        {
            string propertyName = metadata.PropertyName;
            if (propertyName == null)
            {
                var htmlFieldName = ExpressionHelper.GetExpressionText(expression);
                displayName = ((IEnumerable<string>) htmlFieldName.Split('.')).Last<string>();
            }
            else
                displayName = propertyName;
        }

        return displayName;
    }
}
// Usage
Html.GetDisplayName(model => model.Password)
0
Siarhei Kuchuk