2015-11-06 2 views
1

J'ai une classeAjouter attribut d'affichage à une variable sur l'exécution

class demo 
{ 
    public int var1 {set; get;} 
} 

Je veux ajouter les attributs d'affichage sur cette variable sur l'exécution au lieu de le faire,

class Demo 
{ 
    [Display (Name="Any Name", GroupName= "My Group 1")] 
    public int var1 {set; get;} 
} 

De toute façon possible de changer ou Attribuer ces attributs de n'importe quelle autre classe?

+0

Utiliser la recherche ... Il est impossible, mais vous pouvez utiliser TypeDescriptor, par exemple. – Spawn

+0

Pas sans construire un nouveau type ou faire une sorte de tissage IL. Les attributs sont définis au moment de la compilation et compilés dans l'assembly. –

+0

@Spawn N'importe quel exemple comment utiliser TypeDescriptor? J'ai fait l'objet TypeDescriptor mais je n'ai aucune idée comment le lier avec n'importe quelle variable. –

Répondre

1

Exemple très simple, il peut être construit et dans PropertyGrid vous verrez quelque chose. Vous avez besoin de lire à propos de ICustomTypeDescriptor et PropertyDescriptor.

sur Form Load:

propertyGrid1.SelectedObject = new MyType(new[] { "Property1", "Property2" }); 

Types:

public class MyType : ICustomTypeDescriptor 
{ 
    private string[] _properties; 
    public MyType(string[] properties) 
    { 
     _properties = properties; 
    } 

    public AttributeCollection GetAttributes() 
    { 
     return null; 
    } 

    public string GetClassName() 
    { 
     return nameof(MyType); 
    } 

    public string GetComponentName() 
    { 
     throw new NotImplementedException(); 
    } 

    public TypeConverter GetConverter() 
    { 
     return null; 
    } 

    public EventDescriptor GetDefaultEvent() 
    { 
     throw new NotImplementedException(); 
    } 

    public PropertyDescriptor GetDefaultProperty() 
    { 
     return null; 
    } 

    public object GetEditor(Type editorBaseType) 
    { 
     return null; 
    } 

    public EventDescriptorCollection GetEvents() 
    { 
     throw new NotImplementedException(); 
    } 

    public EventDescriptorCollection GetEvents(Attribute[] attributes) 
    { 
     throw new NotImplementedException(); 
    } 

    public PropertyDescriptorCollection GetProperties() 
    { 
     return GetProperties(null); 
    } 

    public PropertyDescriptorCollection GetProperties(Attribute[] attributes) 
    { 
     var props = new PropertyDescriptor[_properties.Length]; 
     for (int i = 0; i < _properties.Length; i++) 
      props[i] = new CustomPropertyDescriptor(_properties[i], 
       new Attribute[] 
       { 
        new DisplayNameAttribute(@"Displ Value " + i), 
        new CategoryAttribute("Category" + i%2) 
       }); 
     return new PropertyDescriptorCollection(props); 
    } 

    public object GetPropertyOwner(PropertyDescriptor pd) 
    { 
     return this; 
    } 
} 

public class CustomPropertyDescriptor : PropertyDescriptor 
{ 
    public CustomPropertyDescriptor(string name, Attribute[] attrs) : base(name, attrs) 
    { 
    } 

    public override bool CanResetValue(object component) 
    { 
     return true; 
    } 

    public override object GetValue(object component) 
    { 
     return "1"; 
    } 

    public override void ResetValue(object component) 
    { 
     throw new NotImplementedException(); 
    } 

    public override void SetValue(object component, object value) 
    { 
     throw new NotImplementedException(); 
    } 

    public override bool ShouldSerializeValue(object component) 
    { 
     return false; 
    } 

    public override Type ComponentType { get; } 
    public override bool IsReadOnly { get { return false; } } 
    public override Type PropertyType { get { return typeof (string); } } 
}