Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 941797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:10:21+00:00 2026-05-15T22:10:21+00:00

I have an existing set of classes that use the [Serializable] attribute. I need

  • 0

I have an existing set of classes that use the [Serializable] attribute. I need to expose them in a WCF service and so I need them to have the [DataContract] attribute. It works with just Serializable but then the properties get funny names like …k__BackingField.

These classes are also used elsewhere and I’m wondering whether I risk breaking anything by replacing the attribute. Also, it is possible and is it a good idea to have them both?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T22:10:22+00:00Added an answer on May 15, 2026 at 10:10 pm

    When you just specify a DataContractAttribute but do not specifically attribute the members that you want included, the default behavior is to serialize all fields of the class, including private ones. So the names you are getting are because you are using automatically implemented properties, I assume.

    So in other words, change the class definition to resemble the following. Note the DataMember attributes on the properties that I want serialized.

    [DataContract]
    public class MyClass {
    
        [DataMember]
        public string SomeString {
            get;
            set;
        }
    
        [DataMember]
        public int SomeInt {
            get;
            set;
        }
    
        public string DontSerializeThis {
            get;
            set;
        }
    
    }
    

    This will cause the DataContractSerializer to serialize the properties and not their compiler-generated backing fields. However, it does require that the properties be read/write public properties because it’s going to go through these property accessors to get and set the serialized data.

    The other option is to change your automatically implemented properties to “normal” properties which means adding your own backing fields. Then you can either leave all the DataMember attributes off which means they’ll all be serialized, or you can add the DataMember attributes to the new fields you created which lets you rename them in the serialized output if you want.

    Finally, to your point about whether it’s a good idea to make a class serializable in both systems, not really. If you want the class to participate in classic serialization using something like BinaryFormatter or XmlSerializer, then you should just target that scenario since the DataContractSerializer can already serialize these classes.

    If your goal is fast, efficient, .NET 3+ to .NET 3+ (or Silverlight) communication, DataContract is the way to go. If your goal is interoperability and/or control over the XML represenation, stick with the XML serialization attributes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an existing set of .net libraries that I wish to call from
I have a set of existing WCF services hosted in a .NET 3.0 app.
I have existing java code and need to create Design Document based on that.
I have an existing panel where I set the html manually with a variable
I have an existing program we've developed as a large set of Eclipse plugins.
I have existing code that uses CMNewProfileSearch to find then iterate over the color
I have generated a set of classes based on a set of respective *.xsd
We have recently developed a library of classes that are used within an MVC3
I have 2 classes that implment an interface. The interface is passed into a
I'm new to Xcode and have a set of instructions for a library that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.