Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取模型类型和StringLength属性_C#_Asp.net Mvc - Fatal编程技术网

C# 获取模型类型和StringLength属性

C# 获取模型类型和StringLength属性,c#,asp.net-mvc,C#,Asp.net Mvc,我想获得StringLength属性 类别代码: var type1 = Type.GetType("MvcApplication4.Models.Sample.SampleMasterModel"); var metadata = ModelMetadataProviders.Current.GetMetadataForType(null, type1); var properties = metadata.Properties; var prop = properties.FirstOrDe

我想获得StringLength属性

类别代码:

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");
???获取StringLength属性

型号代码:

public class SampleModel 
{
    [StringLength(50)]
    public string Remark { get; set; }
}
基于wudzik和Habib的帮助。我修改了密码。 最终代码:

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;
}

您可以通过
PropertyInfo
获得CustomAttributes,如:

PropertyInfo propertyInfo = type1.GetProperties().FirstOrDefault(p=> p.Name == "Remark");
if (propertyInfo != null)
{
    var attributes = propertyInfo.GetCustomAttributes(true);
}
//
///基于传入的属性名称返回属性的StringLengthAttribute。
///在类或基类中使用此方法
/// 
///此类型的类需要属性StringLengthAttribute。
///这是属性名。
/// 
///StringLength为传入的类型传入的propertyName的属性
/// 
公共静态StringLengthAttribute GetStringLengthAttribute(类型类型,字符串属性名称)
{
StringLengthAttribute输出=null;
尝试
{
输出=(StringLengthAttribute)类型.GetProperty(propertyName).GetCustomAttribute(typeof(StringLengthAttribute));
}
捕获(例外情况除外)
{
//错误处理
}
返回输出;
}//GetStringLengthatt贡
/// 
///基于传入的属性名称返回属性的StringLengthAttribute。
///在类或基类中使用此方法
/// 
///这是属性名。
/// 
///StringLength为当前类传入的propertyName属性
/// 
public StringLengthAttribute GetStringLengthAttribute(字符串属性名称)
{
StringLengthAttribute输出=null;
尝试
{
output=(StringLengthAttribute)this.GetType().GetProperty(propertyName).GetCustomAttribute(typeof(StringLengthAttribute));
}
捕获(例外情况除外)
{
//错误处理
}
返回输出;
}//GetStringLengthatt贡
}

typeof(SampleMasterModel)
    /// <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

}