Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Serialize custom attribute of properties

Posted on 2010-09-06
3
Medium Priority
?
1,012 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 18

Accepted Solution

by:
Richard Lee earned 1500 total points
ID: 33609754
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
ID: 33795860
Expecting related solution
0
 
LVL 18

Expert Comment

by:Richard Lee
ID: 33797939
Aladdin might be able to provide that solution
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

664 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