Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class UserInfo
- {
- [Category("change me!")]
- public int Age
- {
- get;
- set;
- }
- [Category("change me!")]
- public string Name
- {
- get;
- set;
- }
- }
- ASCII[] attrs1=(ASCII[])
- typeof(MyClass).GetCustomAttributes(typeof(ASCII), false);
- attrs1[0].MyData="A New String";
- MessageBox.Show(attrs1[0].MyData);
- ASCII[] attrs3=(ASCII[])
- typeof(MyClass).GetCustomAttributes(typeof(ASCII), false);
- MessageBox.Show(attrs3[0].MyData);
- Dim prop As PropertyDescriptor = TypeDescriptor.GetProperties(GetType(UserInfo))("Age")
- Dim att As CategoryAttribute = DirectCast(prop.Attributes(GetType(CategoryAttribute)), CategoryAttribute)
- Dim cat As FieldInfo = att.GetType.GetField("categoryValue", BindingFlags.NonPublic Or BindingFlags.Instance)
- cat.SetValue(att, "A better description")
- using System;
- using System.ComponentModel;
- using System.Windows.Forms;
- class MyCategoryAttribute : CategoryAttribute {
- public MyCategoryAttribute(string categoryKey) : base(categoryKey) { }
- protected override string GetLocalizedString(string value) {
- return "Whad'ya know? " + value;
- }
- }
- class Person {
- [MyCategory("Personal"), DisplayName("Date of Birth")]
- public DateTime DateOfBirth { get; set; }
- }
- static class Program {
- [STAThread]
- static void Main() {
- Application.EnableVisualStyles();
- Application.Run(new Form { Controls = {
- new PropertyGrid { Dock = DockStyle.Fill,
- SelectedObject = new Person { DateOfBirth = DateTime.Today}
- }}});
- }
- }
- public class UserInfo
- {
- [Category("Must change")]
- public string Name { get; set; }
- }
- public class NewUserInfo : UserInfo
- {
- public NewUserInfo(UserInfo user)
- {
- // transfer all the properties from user to current object
- }
- [Category("Changed")]
- public new string Name {
- get {return base.Name; }
- set { base.Name = value; }
- }
- public static NewUserInfo GetNewUser(UserInfo user)
- {
- return NewUserInfo(user);
- }
- }
- void YourProgram()
- {
- UserInfo user = new UserInfo();
- ...
- // Bind propertygrid to object
- grid.DataObject = NewUserInfo.GetNewUser(user);
- ...
- }
Add Comment
Please, Sign In to add comment