Search code examples
silverlight-4.0dynamicpropertyinfosetattribute

how to set name property dynamically in display attributes in silverlight 4


I want to give name property dynamically in the display attributes for any property.

for exam:

    [Display(Name = "Test")]
    public bool Task1
    {
        get { return this.m_Task1; }
        set
        {
            if (value != this.m_Task1)
            {
                this.m_Task1 = value;
                NotifyPropertyChanged("TaskName");
            }
        }
    }

in that property i want to give the name property dynamically means "Test" and that values will comes from the database. So how can i give the name property dynamically in display attributes while generating the property? Can any one help me to find out the solution?


Solution

  • Try this:

    [Display(Name = "Tu edad")]
    public int Edad
    {
      get { bla, bla...; }
      set { bla, bla...; }
    }
    
    public void ChangeEdad()
    {
      var TheProperty =
        this.GetType().GetProperties().Where(x => x.Name == "Edad").FirstOrDefault();
    
      object TheAttribute = 
        TheProperty.GetCustomAttributes(typeof(DisplayAttribute), false)[0];
    
      DisplayAttribute DA = TheAttribute as DisplayAttribute;
      DA.Name = "Your Age";
    }