C#自定义特性

元数据,就是C#中封装的一些类,无法修改.类成员的特性被称为元数据中的注释.

1、什么是特性

     (1)属性与特性的区别

         属性(Property):属性是面向对象思想里所说的封装在类里面的数据字段,Get,Set方法。

         特性(Attribute):  官方解释:特性是给指定的某一声明的一则附加的声明性信息。 允许类似关键字的描述声明。它对程序中的元素进行标注,如类型、字段、方法、属性等。从.net角度看,特性是一种 类,这些类继承于System.Attribute类,用于对类、属性、方法、事件等进行描述,主要用在反射中。但从面向对象的级别看,其实Attribute是类型级别的,而不是对象级别。

         Attributes和.net文件的元素据保存在一起,可以用来向运行时描述你的代码,或者在程序运行的时候影响程序的行为。

2、特性的应用

    (1).net中特性用来处理多种问题,比如序列化、程序的安全特性、防止即时编译器对程序代码进行优化从而代码容易调试等等。

     定植特性的本质上是一个类的元素上去添加附加信息,并在运行其通过反射得到该附加信息(在使用数据实体对象时经常用到)

    (2)Attribute 作为编译器的指令时的应用

         Conditional:起条件编译的作用,只有满足条件,才允许编译器对它的代码进行编译。一般在程序调试的时候使用

         DllImport: 用来标记费.net的函数,表明该方法在一个外部的DLL中定义。

         Obsolete: 这个属性用来标记当前的方法已经废弃,不再使用

       注:Attribute是一个类,因此DllImport也是一个类,Attribute类是在编译的时候实例化,而不是像通常那样在运行时实例化。

         CLSCompliant: 保证整个程序集代码遵守CLS,否则编译将报错。

 3、自定义特性

      使用AttributeUsage,来控制如何应用新定义的特性

     [AttributeUsageAttribute(AttributeTargets.All  可以应用到任何元素

      ,AllowMultiple=true, 允许应用多次,我们的定值特性能否被重复放在同一个程序实体前多次。

      ,Inherited=false,不继承到派生

        )]

      特性也是一个类,必须继承于System.Attribute类,命名规范为“类名”+Attribute。不管是直接还是间接继承,都会成为一个特性类,特性类的声明定义了一种可以放置在声明之上新的特性。

     public class MyselfAttribute:System.Attribute

4、自定义特性案例
     以下用一个类似于Hibernate中Session的Save()方法效果,自动持久化对象信息到数据库来说明自定义特性的使用。

(1)建立Dept表

Create Table Dept(
	deptNo int identity(1,1) primary key,
	dname nvarchar(10) not null,
	description nvarchar(100)
)
Go
(2)自定义特性

/**************自定义特性类*****************/
/// <summary>
/// 作用:用来说明表名是什么
/// AttributeUsage:说明特性的目标元素是什么
/// AttributeTargets.Class:代表目标元素为Class
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
public class TableAttribute : Attribute{

    /// <summary>
    /// 表名
    /// </summary>
    public string TableName { get; set; }

    #region 构造方法,可选的

    public TableAttribute() {    }
    public TableAttribute(string tableName) {
        this.TableName = tableName;
    } 

    #endregion
}

/**************自定义特性类*****************/
/// <summary>
/// 作用:说明列是否为自动增长列
/// </summary>
[AttributeUsage(AttributeTargets.Property)]
class IdentityAttribute: Attribute
{
    /// <summary>
    /// true:是; false:否
    /// </summary>
    public bool IsIdentity { get; set; }
}

/****************实体类***************/
/// <summary>
/// 有意将类名定义成与表名不一致
/// 用Table特性来说明实体类对应的表名是什么
/// </summary>
[Table(TableName = "Dept")]
public class Department {

    /// <summary>
    /// 部门编号,用特性标注为自动增长
    /// </summary>
    [Identity(IsIdentity=true)]
    public int DeptNo { get; set; }

    /// <summary>
    /// 部门名称
    /// </summary>
    public string Dname { get; set; }

    /// <summary>
    /// 部门描述
    /// </summary>
    public string Description { get; set; }

    public Department( string name, string desc) {
        Dname = name;
        Description = desc;
    }
}

/****************执行持久化操作类***************/
/// <summary>
/// 执行持久化操作类
/// </summary>
public class ADOManager {

    /// <summary>
    /// 将对象的属性值作为表中对应列的值来添加
    /// </summary>
    /// <param name="obj">要添加的对象</param>
    public int Save(Object obj) {
        //1.取得类名:代表表名,用到反射
        string tableName = obj.GetType().Name;
        //如果类有TableAttribute特性,在采用特性说明的类名
        TableAttribute attr = Attribute.GetCustomAttribute(obj.GetType(), typeof(TableAttribute)) as TableAttribute;
        if (attr != null) {//说明类加了Table特性
            tableName = attr.TableName;//取得表名
        }

        //sql语句模板:insert into Dept(deptno,dname,description) values('2','','');
        StringBuilder sql = new StringBuilder("insert into ");
        sql.Append(tableName);
        sql.Append(" (");

        //循环对象的属性名:取得列名
        foreach (PropertyInfo item in obj.GetType().GetProperties()) {
            //取得是否有自动增长的特性
            IdentityAttribute att = Attribute.GetCustomAttribute(item, typeof(IdentityAttribute)) as IdentityAttribute;
            if (att == null || !att.IsIdentity) {//没有,则添加列
                sql.Append(item.Name);
                sql.Append(",");
            }
        }
        //去除最后一个逗号'
        sql.Remove(sql.Length - 1, 1);
        sql.Append(") values(");
        //循环取出对象的属性值:为列赋值
        foreach (PropertyInfo item in obj.GetType().GetProperties()) {
            //取得是否有自动增长的特性
            IdentityAttribute att = Attribute.GetCustomAttribute(item, typeof(IdentityAttribute)) as IdentityAttribute;
            if (att == null) {//没有,则追加列的值
                //GetValue():obj代表什么对象,null代表没有参数
                sql.Append("'" + item.GetValue(obj, null) + "'");
                sql.Append(",");
            }
        }

        //去除最后一个逗号'
        sql.Remove(sql.Length - 1, 1);
        sql.Append(")");
        //查看完整的sql语句
        Console.WriteLine(sql.ToString());

        //执行sql语句
        SqlConnection conn = new SqlConnection("server=.;database=test;integrated security=true");
        SqlCommand comm = new SqlCommand(sql.ToString(), conn);
        conn.Open();
        int r = comm.ExecuteNonQuery();
        conn.Close();

        return r;//返回执行结果
    }
}

/****************测试类关键代码***************/
Department dept = new Department("开发部", "负责产品的研发");
ADOManager manager = new ADOManager();
int r = manager.Save(dept);

Console.WriteLine(r==0?"失败":"成功");

小结:

C#的特性类和Java中的元注释一样

特性其本质就是一个继承了Attribute的类

使用使可以省略Attribute结尾,如:TableAttribute =>> Table

特性将会影响其作用的目标元素的编译和运行过程

使用自定义特性的步骤:

1. 定义特性类,类必须直接或间接继承字Attribute类

2. 在需要用的该特性的目标元素上添加特性

3. 在使用添加了特性的类的使用,获取并使用自特定特性的信息


  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C# 中的自定义控件特性可以通过使用 .NET Framework 中的 `System.ComponentModel` 命名空间来实现。这些特性可以为控件提供额外的元数据,使其在设计时和运行时具有更丰富的功能和行为。 以下是一些常见的自定义控件特性: 1. `BrowsableAttribute`: 指示控件的属性是否应该在属性窗格中显示。如果将其设置为 `false`,则在设计时不会显示该属性,默认值为 `true`。 2. `CategoryAttribute`: 定义属性在属性窗格中所属的类别。可以根据需要将属性分组,并使其更易于组织和查找。 3. `DescriptionAttribute`: 提供对属性或控件的简短描述。这对于在属性窗格中显示有关属性的提示信息很有用。 4. `DefaultValueAttribute`: 指定属性的默认值。当用户在设计时创建一个新实例或重置属性时,将使用该值。 5. `EditorAttribute`: 指定与属性关联的自定义编辑器。自定义编辑器可以提供更复杂的交互和编辑体验,例如下拉列表或日期选择器。 6. `BindableAttribute`: 标识是否可以将属性绑定到数据源。如果设置为 `true`,则可以通过数据绑定机制将该属性与其他数据进行关联。 这些特性可以通过在自定义控件的属性上应用相应的特性来实现。例如: ```csharp using System.ComponentModel; public class MyCustomControl : Control { [Browsable(true)] [Category("MyCategory")] [Description("This is my custom property.")] [DefaultValue(true)] public bool MyProperty { get; set; } [Browsable(true)] [Category("MyCategory")] [Description("This is my custom event.")] public event EventHandler MyEvent; // ... } ``` 通过使用这些自定义控件特性,可以提供更好的设计时体验,并使控件更易于使用和配置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值