Solved

Serialize custom attribute of properties

Posted on 2010-09-06
3
1,004 Views
Last Modified: 2012-05-10
 
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
Comment
Question by:RETAILREALM
  • 2
3 Comments
 
LVL 18

Accepted Solution

by:
Richard Lee earned 500 total points
Comment Utility
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
 

Author Closing Comment

by:RETAILREALM
Comment Utility
Expecting related solution
0
 
LVL 18

Expert Comment

by:Richard Lee
Comment Utility
Aladdin might be able to provide that solution
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Here I am going to explain creating proxies at runtime for WCF Service. So basically we use to generate proxies using Add Service Reference and then giving the Url of the WCF service then generate proxy files at client side. Ok, what if something ge…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now