Tuesday, March 1, 2011

How to read Metadata values from Silverlight Client with RIA Services

I have an RIA Services Silverlight 3.0 app using an EF model. In the model metadata I've included several Display Name properties that I'd like to use when referring to the model on the client-side (in TextBoxes, etc.. .)

I'm using reflection now to get the properties of the model on the client so that if the model changes over time, I don't need to update the client code. I just can't figure out how to access the metadata.

private void Field_Loaded(object sender, RoutedEventArgs e)
{
    System.Reflection.MemberInfo[] members = this.ModelType.GetMembers(); 
    foreach (System.Reflection.MemberInfo member in members)
    {
     System.Reflection.PropertyInfo property = member as System.Reflection.PropertyInfo;
     if (property != null && property.PropertyType == typeof(System.String))
     {
      ComboBoxItem item = new ComboBoxItem();
      item.Content = property.Name; // <--- This is where I want to use Display Name
      this._field.Items.Add(item);
     }
    }
}

Thanks in advance,

From stackoverflow
  • You should be able to do this using GetCustomAttributes and passing the DisplayNameAttribute as the type.

    Nick Gotch : Thanks Bryant but I can't find System.ComponentModel.DisplayNameAttribute in the Silverlight assembly. It shows up in the space on the server but not the client. Do I need to reference something special to get it in Silverlight?
    Bryant : I think it is actually call DisplayAttribute, not DisplayNameAttribute.
    Nick Gotch : It's weird, I found DescriptionAttribute and that's working fine but nothing starting with 'Display' shows up in Intellisense.
    Nick Gotch : Found it; it's in System.ComponentModel.DataAnnotations.DisplayAttribute . Thanks!!

0 comments:

Post a Comment