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 7841535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:07:16+00:00 2026-06-02T16:07:16+00:00

I don’t know how to Serialize a object by DataContractSerialize. Here is my code:

  • 0

I don’t know how to Serialize a object by DataContractSerialize.
Here is my code:

    public static string DataContractSerialize(object target)
    {
        var formatter = new DataContractSerializer(target.GetType());
        using (var stream = new MemoryStream())
        {
            formatter.WriteObject(stream, target);
            stream.Position = 0;
            return Encoding.UTF8.GetString(stream.ToArray());
        }
    }

and entity

[Serializable, DataContract(Namespace = "CommunicationModel.Entity")]
[KnownType(typeof(Message))]
[KnownType(typeof(int))]
[KnownType(typeof(string))]
[KnownType(typeof(Type))]
[KnownType(typeof(object))]
public class Message : IDisposable
{
    public Message(string stringInfo)
    {
        MessageValue = stringInfo;
        MessageType = typeof (string);
    }

    public Message(int intInfo)
    {
        MessageValue = intInfo;
        MessageType = typeof (int);
    }
    [DataMember]
    public Type MessageType { get; private set; }
    [DataMember]
    public object MessageValue { get; private set; }

    #region Implementation of IDisposable

    public void Dispose()
    {
    }

    #endregion
}

When I run the DataContractSerialize like this:

var sData = SerializerHelper.DataContractSerialize(msg);

it will throw a exception. What can I do?

  • 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-06-02T16:07:17+00:00Added an answer on June 2, 2026 at 4:07 pm

    First, you cannot have a type that is both [Serializable] and [DataContract]. This is not recommended and is meaningless. Just have [DataContract]. For more information on why, see this post on the data contract programming model.

    Anyway, the problem here is that you’re actually trying to serialize a RuntimeType, because MessageType is represented as a RuntimeType. RuntimeType is an internal class that’s a child of Type and that is not public, so that you can’t knowingly refer to it as a known type. See What's the difference between System.Type and System.RuntimeType in C#? for more information on what RuntimeType is and why it is the way it is.

    So, you have two options here:

    • Consider adding a KnownTypes attribute that takes a static method name. From your static method, you can return the various types you really want, including potentially RuntimeType if you use reflection.

    • The option I’d recommend is to make MessageType a TypeHandle (a RuntimeTypeHandle.) The advantage of this is that you can actually make RuntimeTyepHandle a known type since it’s public. It’s also serializable and deserializable just like any other type. See this excellent blog post on what I mean.

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

Sidebar

Related Questions

don't know better title for this, but here's my code. I have class user
I don't know if this is a well known 'thing' or something new in
Don't know what's wrong here, when I run the application it says Specified method
Don't know why I can't reply to people on here with only a small
Don't think there's much to say, here's my code for (int i = 0;
Don't know a better title but here is what im trying to do. I
Don't know how to frase this but I found this code wich works as
Don't be scared of the extensive code. The problem is general. I just provided
Don't really know how to formulate the title, but it should be pretty obvious
Don't know how to google for such, but is there a way to query

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.