C# 特性 System.ComponentModel 命名空间属性方法大全,System.ComponentModel 命名空间的特性

System.ComponentModel 命名空间是 .NET Framework/.NET Core 中的重要组成部分,广泛用于:

  • 属性描述与元数据特性(Attribute)

  • 数据绑定(INotifyPropertyChanged)

  • 组件设计器支持

  • 类型转换与描述器


🧩 一、System.ComponentModel 命名空间常用特性(Attributes)

这些特性(Attribute)主要用于修饰类、属性、方法等,在设计器、绑定系统、序列化中具有特殊意义。

特性名说明
BrowsableAttribute控制属性是否出现在设计器中(如属性面板)
CategoryAttribute给属性分组显示
DescriptionAttribute添加工具提示说明
DisplayNameAttribute设置属性的 UI 显示名称
DefaultValueAttribute指定属性默认值
ReadOnlyAttribute指示属性是否为只读
BindableAttribute是否支持数据绑定
DesignOnlyAttribute属性是否只在设计时可用
EditorBrowsableAttribute控制 IntelliSense 可见性
TypeConverterAttribute指定类型转换器
NotifyParentPropertyAttribute通知父属性改变
DesignerCategoryAttribute指定设计器类别(如 "Form", "Component")
InheritanceAttribute指示类的继承行为
LicenseProviderAttribute指定许可证提供程序

✍ 示例代码:使用特性定义属性

using System.ComponentModel;

public class Person
{
    [Category("基本信息")]
    [DisplayName("姓名")]
    [Description("用户的全名")]
    [DefaultValue("张三")]
    [Browsable(true)]
    public string Name { get; set; }

    [Category("基本信息")]
    [DisplayName("年龄")]
    [Description("用户的年龄")]
    [ReadOnly(false)]
    [Bindable(true)]
    public int Age { get; set; }
}

🔧 二、常用接口(用于数据绑定和组件交互)

接口名功能说明
INotifyPropertyChanged属性变更通知(用于 MVVM 数据绑定)
INotifyPropertyChanging属性修改前通知
INotifyDataErrorInfo支持数据验证错误通知
IDataErrorInfoWPF 中旧的数据验证机制
IComponent所有组件的基本接口
ISite组件与容器间连接的接口
ICustomTypeDescriptor自定义类型元数据暴露接口

🚀 三、常用类:描述、转换与设计支持

类名功能
TypeDescriptor获取类型的属性、事件、特性等(反射+元数据)
PropertyDescriptor描述单个属性
EventDescriptor描述事件
TypeConverter类型转换器基类
ExpandableObjectConverter支持对象在属性面板中展开
StringConverterBooleanConverter内置类型转换器
LicenseManager控制许可证检查
ComponentContainer组件与容器设计支持
EventHandlerList高效管理多个事件委托

🧪 示例:INotifyPropertyChanged 应用(数据绑定)

public class User : INotifyPropertyChanged
{
    private string name;

    public string Name
    {
        get => name;
        set 
        {
            if (value != name)
            {
                name = value;
                OnPropertyChanged(nameof(Name));
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
    }
}

🔍 四、TypeDescriptor 动态读取属性信息

var props = TypeDescriptor.GetProperties(typeof(Person));
foreach (PropertyDescriptor prop in props)
{
    Console.WriteLine($"属性名: {prop.Name}, 描述: {prop.Description}, 默认值: {prop.GetValue(new Person())}");
}

✅ 五、实际应用场景

场景使用特性
WinForms/WPF 属性编辑器[Category][Description] 等
Blazor 或绑定系统INotifyPropertyChanged
控件/组件开发ComponentDesignerCategoryAttribute
类型转换器TypeConverterExpandableObjectConverter
设计时支持BrowsableReadOnlyEditorBrowsable

🧠 小结

类型示例
属性特性[Category][Description][ReadOnly]
绑定接口INotifyPropertyChangedINotifyDataErrorInfo
设计支持ComponentISiteDesignerCategory
元数据工具TypeDescriptorTypeConverterPropertyDescriptor

如果你需要我生成一个完整的 WinForms/WPF 控件或 Blazor 示例,或者说明如何动态读取这些属性/特性,请随时告诉我。也可以补充你使用场景,我来具体定制示例代码。


✅ 示例目标

  • 创建一个自定义类 Person,使用属性特性 [Category][Description][DisplayName][ReadOnly] 等。

  • 使用 PropertyGrid 控件在窗体中动态显示这些属性。

  • 实现 INotifyPropertyChanged,支持数据变更通知。


🧪 示例代码(WinForms)

1️⃣ 创建类 Person.cs

using System.ComponentModel;

public class Person : INotifyPropertyChanged
{
    private string name;
    private int age;
    private string email;

    [Category("基本信息")]
    [DisplayName("姓名")]
    [Description("请输入您的真实姓名")]
    [DefaultValue("张三")]
    public string Name
    {
        get => name;
        set { name = value; OnPropertyChanged(nameof(Name)); }
    }

    [Category("基本信息")]
    [DisplayName("年龄")]
    [Description("请输入年龄(整数)")]
    public int Age
    {
        get => age;
        set { age = value; OnPropertyChanged(nameof(Age)); }
    }

    [Category("联系信息")]
    [DisplayName("电子邮件")]
    [Description("请输入有效的 Email 地址")]
    public string Email
    {
        get => email;
        set { email = value; OnPropertyChanged(nameof(Email)); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string name)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
    }
}

2️⃣ 创建 WinForms 窗体 MainForm.cs

using System;
using System.Windows.Forms;

public class MainForm : Form
{
    private PropertyGrid propertyGrid;
    private Button showDataButton;
    private Person person;

    public MainForm()
    {
        Text = "System.ComponentModel 演示";
        Width = 600;
        Height = 400;

        propertyGrid = new PropertyGrid
        {
            Dock = DockStyle.Left,
            Width = 300
        };

        showDataButton = new Button
        {
            Text = "打印当前数据",
            Dock = DockStyle.Top,
            Width = 150
        };
        showDataButton.Click += ShowDataButton_Click;

        person = new Person { Name = "张三", Age = 30, Email = "zhang@example.com" };
        propertyGrid.SelectedObject = person;

        Controls.Add(propertyGrid);
        Controls.Add(showDataButton);
    }

    private void ShowDataButton_Click(object sender, EventArgs e)
    {
        MessageBox.Show($"姓名: {person.Name}\n年龄: {person.Age}\nEmail: {person.Email}", "当前信息");
    }

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new MainForm());
    }
}

💡 效果说明

运行后将看到:

  • PropertyGrid 自动识别 Person 中的属性及其描述。

  • 属性分组、描述、显示名、默认值都按特性设置。

  • 点击“打印当前数据”按钮可以输出当前输入值。


🧠 技术点总结

功能使用的特性或接口
分组[Category("组名")]
说明[Description("提示")]
显示名[DisplayName("中文名")]
默认值[DefaultValue(...)]
数据通知INotifyPropertyChanged

如你想扩展支持:

  • ✅ WPF 应用版本;

  • ✅ 绑定到 DataGrid;

  • ✅ 属性动态只读或隐藏;

  • ✅ 类型转换器扩展(TypeConverter);

只需告诉我即可,我可以为你定制代码片段和说明。是否需要我加一个 TypeConverter 示例?或者 Blazor 动态表单演示?

C# 特性 System.ComponentModel 命名空间属性方法大全,System.ComponentModel 命名空间的特性 - 菜鸟-创作你的创作

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值