• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1016
  • Last Modified:

Serialize custom attribute of properties

 
We have datacontract as below and another class DwPropertyUAttribute (serialized with datacontract) for custom attribute.  Properties are serialized but I need to be serialized custom attribute too. I tried this but no luck. Please commnet experts. Thanks in advance. If you need more details pls ask for the same.

[DataContract(),Serializable()]
    public class wsPrefs
    {
        [DwPropertyUI("TestName","TestLable")]
        [DataMember()]
        public bool SuppressCustomerMode
        {
            get { return true; }
            set { }
        }

        [DataMember()]
        [DwPropertyUI("TestName", "TestLable")]
        public string CustomerDefaultAreaCode
        {
            get { return ""; }
            set { }
        }
     
    }
}


// Custom attribute class

[DataContract(), Serializable()]
    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Class)]
    public class DwPropertyUIAttribute : Attribute
    {

        protected string m_Name;
        protected string m_Label;

        #region "Properties"

        [DataMember()]
        public string Name
        {
            get { return m_Name; }
            set { m_Name = value; }
        }

        [DataMember()]
        public string Label
        {
            get { return m_Label; }
            set { m_Label = value; }
        }

// Reading custom attribute like this

          wsPrefs ws = new wsPrefs();
           
            foreach (PropertyInfo pi in ws.GetType().GetProperties())
      {
                              uiAttributes = (DwPropertyUIAttribute[])pi.GetCustomAttributes(typeof(DwPropertyUIAttribute), false);

}

0
RETAILREALM
Asked:
RETAILREALM
  • 2
1 Solution
 
Richard LeeSoftware EnthusiastCommented:
Src: http://stackoverflow.com/questions/2829976/send-objects-with-custom-attributes-via-a-wcf-service

WCF is a message based service - you serialize your message on one side, send it across, and receive it on the other side. You're not sending objects as you put it - you only send serialized messages. That's quite an important difference!

Your server and client are totally separate - they don't share anything but the service description (the list of service methods), and the data contracts in the form of a XML schema.

What you get when you create a client is a proxy for the service methods, and a copy of the data contract (looks the same, but different namespace, typically) that has the same signature in the serialized format. That's all there is. You get a new, separate class on the client side, that will serialize into the same format as your original class on the server side.

This means: you'll get the same fields and properties - but that's it. Anything else (like interfaces that are implemented, .NET attributes and more) are not replicated. They cannot be - after all, WCF is interoperable - the client could be a PHP app or a Ruby program. How are they going to handle .NET custom attributes?

So in brief: anything that is .NET specific and goes beyond the simple XML-schema-based data representation cannot be used across a WCF service.

There is a loop hole - if you control both ends of the communication - both server and client - and both are .NET, then you could:

put all your service and data contracts into a separate, shared assembly
reference that assembly in your service implementation on the server side
reference that assembly in your client side proxy
Using this, you'll have one assembly with the same data type on both the server and the client side - and with this "loophole", you can preserve such .NET specifics like attributes between server and client.

DaTribe
0
 
RETAILREALMAuthor Commented:
Expecting related solution
0
 
Richard LeeSoftware EnthusiastCommented:
Aladdin might be able to provide that solution
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now