public class Person { public string Name { get; set; } public int Age { get; set; } } public void CreateNewProperty() { PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(Person)); var attribute = new DescriptionAttribute("This property returns the full name"); var fullNamePropertyDescriptor = TypeDescriptor.CreateProperty(typeof(Person), "FullName", typeof(string), attribute); properties.Add(fullNamePropertyDescriptor); }
public class Account { public string AccountName { get; set; } public decimal Balance { get; set; } } public void CreateNewProperty() { PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(Account)); var attribute = new DescriptionAttribute("This property checks if the account is overdrawn"); var isOverdrawnPropertyDescriptor = TypeDescriptor.CreateProperty(typeof(Account), "IsOverdrawn", typeof(bool), attribute, new ReadOnlyAttribute(true)); properties.Add(isOverdrawnPropertyDescriptor); }In this example, a new property descriptor called IsOverdrawn is created for the Account class with a type of bool and a custom DescriptionAttribute. Additionally, a ReadOnlyAttribute is applied to this property to prevent it from being set manually. The TypeDescriptor CreateProperty method is part of the System.ComponentModel.TypeDescriptor namespace in C#.