简体   繁体   中英

Get model type and StringLength attribute

I want to get the StringLength attribute.

Class code :

var type1 = Type.GetType("MvcApplication4.Models.Sample.SampleMasterModel");
var metadata = ModelMetadataProviders.Current.GetMetadataForType(null, type1);
var properties = metadata.Properties;
var prop = properties.FirstOrDefault(p => p.PropertyName == "Remark");

?? Get StringLength attr?

Model Code:

public class SampleModel 
{
    [StringLength(50)]
    public string Remark { get; set; }
}

Based on wudzik and Habib help. I modified the code. Final Code:

PropertyInfo propertyInfo = type1.GetProperties().FirstOrDefault(p => p.Name == "Remark");
if (propertyInfo != null)
{
    var attributes = propertyInfo.GetCustomAttributes(true);
    var stringLengthAttrs =
        propertyInfo.GetCustomAttributes(typeof (StringLengthAttribute), true).First();
    var stringLength = stringLengthAttrs != null ? ((StringLengthAttribute)stringLengthAttrs).MaximumLength : 0;
}

You can get CustomAttributes through PropertyInfo like:

PropertyInfo propertyInfo = type1.GetProperties().FirstOrDefault(p=> p.Name == "Remark");
if (propertyInfo != null)
{
    var attributes = propertyInfo.GetCustomAttributes(true);
}
    /// <summary>
    /// Returns the StringLengthAttribute for a property based on the property name passed in.
    /// Use this method in the class or in a base class
    /// </summary>
    /// <param name="type">This type of the class where you need the property StringLengthAttribute.</param>
    /// <param name="propertyName">This is the property name.</param>
    /// <returns>
    /// StringLengthAttribute of the propertyName passed in, for the Type passed in
    /// </returns>
    public static StringLengthAttribute GetStringLengthAttribute(Type type, string propertyName)
    {
        StringLengthAttribute output = null;

        try
        {
            output = (StringLengthAttribute)type.GetProperty(propertyName).GetCustomAttribute(typeof(StringLengthAttribute));
        }
        catch (Exception ex)
        {
            //error handling
        }

        return output;

    } //GetStringLengthAttribute


    /// <summary>
    /// Returns the StringLengthAttribute for a property based on the property name passed in.
    /// Use this method in the class or in a base class
    /// </summary>
    /// <param name="propertyName">This is the property name.</param>
    /// <returns>
    /// StringLengthAttribute of the propertyName passed in, for the current class
    /// </returns>
    public StringLengthAttribute GetStringLengthAttribute(string propertyName)
    {
        StringLengthAttribute output = null;

        try
        {
            output = (StringLengthAttribute)this.GetType().GetProperty(propertyName).GetCustomAttribute(typeof(StringLengthAttribute));
        }
        catch (Exception ex)
        {
            //error handling
        }

        return output;

    } //GetStringLengthAttribute

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM