Silverlight: Using Custom Attributes Server-Side


  • I have a Silverlight 5 application in which I'm using Entity Framework. On one of my EF entities, I am adding an XmlElementAttribute custom attribute to some of the properties (in metadata file located on server) of my entity. On the client side, I am able to use reflection to get the custom attributes from the properties of that data type. I'm using linq to do this as seen below.

    entityType.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance)
    .Where(p => p.GetCustomAttributes(typeof(XmlElementAttribute), false)
    .Length > 0)

    I would like to do this same thing server side, however when I get the custom attributes from the property on the server side, the XmlElementAttribute attribute doesn't exist. Only two attributes exist for my property (EdmScalarPropertyAttribute and DataMemberAttributte) Is there a way to get at the XmlElementAttribute attribute from server side code?

    Any help would be much appreciated...

    Friday, June 29, 2012 2:25 PM

All replies

  • Does the XmlElementAttribute custom attribute apply to the properties of your entity on the server side? Based on my understanding, the GetProperties will only return Public properties by default, but see the above code, it seems that it could get custom attributes from the properties  in that data type on the server side, could you post the related code (data type which uses XmlElementAttribute)?

    Please mark the replies as answers if they help or unmark if not. If you have any feedback about my replies, please contact Microsoft One Code Framework

    Wednesday, July 04, 2012 8:40 AM
  • First of all, thanks for the reply!

    The properties on my entity on the server side are public, but I wonder if it has something to do with the way I'm using a metadata file.  Basically, I setup my entity in an entity data model based on a view in my database (for this example, the name of my entity is "vwEntity1").

    I then created a metadata file named vwEntity1.metadata.cs.  This is where I am applying the custom attributes.  Here is an example of the my metadata file.  Note the use of the "XmlElement" attribute on each of the properties of my entity.

    namespace MySilverlightApp.Web.Models
        using System;
        using System.ComponentModel.DataAnnotations;
        using System.Linq;
        using System.ServiceModel.DomainServices.Server;
        using System.Xml.Serialization;
        public partial class vwEntity1
            internal sealed class vwEntity1        {
                // Metadata classes are not meant to be instantiated.
                private vwEntity1Metadata()
                [XmlElement(Order = 2)]
                public string TestProp2 { get; set; }
                [XmlElement(Order = 1)]
                public string TestProp1 { get; set; }
                [XmlElement(Order = 3)]
                public string TestProp3 { get; set; }

    Now, when I try to get the custom attributes using the code in the initial post, "GetCustomAttributes()", The XmlElement custom attribute isn't in the collection of attributes.  I'm also having the same issue with other custom attributes that I apply in the metadata file.  However, they are accessible on the client using the same code.

    Thursday, July 05, 2012 1:45 PM