zoukankan      html  css  js  c++  java
  • 属性控制类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
  • 相关阅读:
    白话排序算法--快速排序
    白话排序算法--插入排序
    javamail模拟邮箱功能--邮件回复-中级实战篇【邮件回复方法】(javamail API电子邮件实例)
    javamail模拟邮箱功能获取邮件内容-中级实战篇【内容|附件下载方法】(javamail API电子邮件实例)
    JS替换地址栏参数值
    JAVA中使用FTPClient上传下载
    js事件的捕获和冒泡阶段
    js刷新页面方法大全
    js检测上传文件大小
    java正则:不包含某个规则字符串【转】
  • 原文地址:https://www.cnblogs.com/kingkoo/p/1703413.html
Copyright © 2011-2022 走看看