C# 自定义特性

1.代码视图:


2.RecordAttribute.cs

using System;

namespace 自定义特性
{
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = false)]
    public class RecordAttribute : Attribute
    {
        private readonly string _author; // 作者     
        private readonly DateTime _date; // 更新/创建 日期     
        private readonly string _recordType; // 记录类型:更新/创建     

        // 构造函数,构造函数的参数在特性中也称为“位置参数”。     
        //NOTE:注意构造函数的参数 date,必须为一个常量、Type类型、或者是常量数组,所以不能直接传递DateTime类型。
        public RecordAttribute(string recordType, string author, string date)
        {
            _recordType = recordType;
            _author = author;
            _date = Convert.ToDateTime(date);
        }

        // 对于位置参数,通常只提供get访问器     
        public string RecordType
        {
            get { return _recordType; }
        }

        public string Author
        {
            get { return _author; }
        }

        public DateTime Date
        {
            get { return _date; }
        }

        // 构建一个属性,在特性中也叫“命名参数”     
        public string Memo { get; set; }
    }
}

3.Program.cs

using System;
using System.Reflection;

namespace 自定义特性
{ 
    internal class Program
    {
        private static void Main(string[] args)
        {
            var demo = new DemoClass();
            Console.WriteLine(demo.ToString());

            MemberInfo reflection = typeof(DemoClass);
            var recordAttributes =
                Attribute.GetCustomAttributes(reflection, typeof(RecordAttribute)) as RecordAttribute[];
            if (recordAttributes != null)
            {
                foreach (RecordAttribute attribute in recordAttributes)
                {
                    if (attribute != null)
                    {
                        Console.WriteLine("时间:{0}", attribute.Date.ToString("yyyy-MM-dd HH:mm:ss"));
                        Console.WriteLine("作者:{0}", attribute.Author);
                        Console.WriteLine("备注:{0}", attribute.Memo);
                    }
                }
            }

            Console.Read();
        }
    }
    [Record("更新", "王五", "2012-12-24", Memo = "修改 ToString()方法")]
    [Record("更新", "李四", "2012-12-18", Memo = "修改文件")]
    [Record("创建", "张三", "2012-11-15", Memo = "创建文件")]
    public class DemoClass
    {
        public override string ToString()
        {
            return "This is a demo class";
        }
    }
}

4.运行结果:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值