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 | 支持数据验证错误通知 |
IDataErrorInfo | WPF 中旧的数据验证机制 |
IComponent | 所有组件的基本接口 |
ISite | 组件与容器间连接的接口 |
ICustomTypeDescriptor | 自定义类型元数据暴露接口 |
🚀 三、常用类:描述、转换与设计支持
| 类名 | 功能 |
|---|---|
TypeDescriptor | 获取类型的属性、事件、特性等(反射+元数据) |
PropertyDescriptor | 描述单个属性 |
EventDescriptor | 描述事件 |
TypeConverter | 类型转换器基类 |
ExpandableObjectConverter | 支持对象在属性面板中展开 |
StringConverter, BooleanConverter | 内置类型转换器 |
LicenseManager | 控制许可证检查 |
Component, Container | 组件与容器设计支持 |
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 |
| 控件/组件开发 | Component, DesignerCategoryAttribute |
| 类型转换器 | TypeConverter、ExpandableObjectConverter |
| 设计时支持 | Browsable, ReadOnly, EditorBrowsable |
🧠 小结
| 类型 | 示例 |
|---|---|
| 属性特性 | [Category], [Description], [ReadOnly] |
| 绑定接口 | INotifyPropertyChanged, INotifyDataErrorInfo |
| 设计支持 | Component, ISite, DesignerCategory |
| 元数据工具 | TypeDescriptor, TypeConverter, PropertyDescriptor |
如果你需要我生成一个完整的 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 命名空间的特性 - 菜鸟-创作你的创作
2万+

被折叠的 条评论
为什么被折叠?



