• 属性控制类2


    代码
    [AttributeUsage(AttributeTargets.Property, Inherited = false)]
    [ComVisible(
    true)]
    public class PropertyKeyAttribute : Attribute
    {
    public PropertyKeyAttribute(int Length)
    {
    this.Length = Length;
    this.NoneBuild = true;
    }
    public PropertyKeyAttribute()
    {
    this.NoneBuild = true;
    }

    public int Length { get; set; }
    public System.Data.SqlDbType SqlDbType { get; set; }
    public string Name { get; set; }
    public int Index { get; set; }
    public bool NoneBuild { get; set; }



    public static int GetLength<T>(string name)
    {
    PropertyKeyAttribute attr
    = GetPropertyKeyAttribute<T>(name);
    if (attr != null)
    return attr.Length;
    return 20;
    }
    public static System.Data.SqlDbType GetSqlDbType<T>(string name)
    {
    PropertyKeyAttribute attr
    = GetPropertyKeyAttribute<T>(name);
    if (attr != null)
    return attr.SqlDbType;
    return System.Data.SqlDbType.VarChar;
    }
    public static bool GetNoneBuild<T>(string name)
    {
    PropertyKeyAttribute attr
    = GetPropertyKeyAttribute<T>(name);
    if (attr != null)
    return attr.NoneBuild;
    return false;
    }

    public static int GetLength(object model, string name)
    {
    PropertyKeyAttribute attr
    = GetPropertyKeyAttribute(model, name);
    if (attr != null)
    return attr.Length;
    return 20;
    }
    public static System.Data.SqlDbType GetSqlDbType(object model, string name)
    {
    PropertyKeyAttribute attr
    = GetPropertyKeyAttribute(model, name);
    if (attr != null)
    return attr.SqlDbType;
    return System.Data.SqlDbType.VarChar;
    }
    public static bool GetNoneBuild(object model, string name)
    {
    PropertyKeyAttribute attr
    = GetPropertyKeyAttribute(model, name);
    if (attr != null)
    return attr.NoneBuild;
    return false;
    }

    #region PropertyKeyAttribute
    private static PropertyKeyAttribute GetPropertyKeyAttribute<T>(string name)
    {
    T model
    = (T)Activator.CreateInstance(typeof(T));
    Type type
    = typeof(PropertyKeyAttribute);

    PropertyInfo pi
    = model.GetType().GetProperty(name);
    object[] attributes = pi.GetCustomAttributes(false);
    foreach (object o in attributes)
    {
    if (o is PropertyKeyAttribute)
    {
    //把得到的属性强制转换成PersonAttribute
    PropertyKeyAttribute pa = (PropertyKeyAttribute)o;
    return pa;
    }
    }
    return null;
    }
    private static PropertyKeyAttribute GetPropertyKeyAttribute(object model, string name)
    {
    try
    {
    Type type
    = typeof(PropertyKeyAttribute);
    PropertyInfo pi
    = model.GetType().GetProperty(name);
    object[] attributes = pi.GetCustomAttributes(false);
    foreach (object o in attributes)
    {
    if (o is PropertyKeyAttribute)
    {
    //把得到的属性强制转换成PersonAttribute
    PropertyKeyAttribute pa = (PropertyKeyAttribute)o;
    return pa;
    }
    }
    }
    catch
    {

    }
    return null;
    }
    #endregion


    }
    千人.NET交流群:18362376,因为有你,代码变得更简单,加群请输入cnblogs
  • 相关阅读:
    小米手机做USB电脑摄像头啦,亲测可用,附有详细教程!
    【DIY文章列表标签】dt_gry_list
    Oracle 10g 设置 PL/SQL 远程
    关于硬盘“4K扇区”对齐的查看与设置方法
    oracle数据误操作恢复【flashback闪回操作】
    CENTOS下安装LNMP环境随笔
    深喉咙使用心得(陆续更新ing....)
    CENTOS6.3环境下安装VSFTPD 便于开通FTP功能随笔
    MYSQL/SQL_SERVER/ORACLE三种数据库自动备份方法
    U盘安装 ubuntu 12.04随笔
  • 原文地址:https://www.cnblogs.com/kingkoo/p/1703413.html
Copyright © 2020-2023  润新知