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

  • Home
  • SEARCH
  • 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 9131547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:09:32+00:00 2026-06-17T08:09:32+00:00

I am trying to serialize an object that contains an interface. However, interfaces cannot

  • 0

I am trying to serialize an object that contains an interface. However, interfaces cannot be serialized. Normally, I would use something like the NonSerialized tag, but I cannot figure out how to apply this attribute to a class that I cannot modify, such as one of the predefined .NET classes (e.g.: System.Diagnostics.Process).

For example, consider the following code:

using System.Diagnostics
using System.Xml.Serialization;

class Program
{
    static void Main(string[] args)
    {
        try
        {
            XmlSerializer x = new XmlSerializer(typeof(Process));
        }
        catch (Exception e)
        {
            Console.WriteLine(e.InnerException.InnerException.Message);
        }
    }
}

This prints the following result:

Cannot serialize member System.ComponentModel.Component.Site of type System.ComponentModel.ISite because it is an interface.

Is there a way to do any of the following in a class that I cannot modify, such as a system class?

  1. selectively ignore child elements during serialization, so that the child element does not get serialized at all
  2. mark an element with something that accomplishes the same thing as NonSerialized

I’ve thought of some solutions like using reflection to dynamically generate a class that contains all the same members as the class to be serialized, doing some type of deep copy, and serializing that. However, I’m curious to see if there is any simpler way to accomplish this serialization task other than going the class generating reflection route.

  • 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-17T08:09:33+00:00Added an answer on June 17, 2026 at 8:09 am

    If serialization of an existing type gets complicated, the best option is always: create a separate DTO model – that looks kinda similar to your domain entity, but which only exists to play nicely with serialization – usually very simple (parameterless constructors, basic accessors, no validation, etc). Then map between them. Otherwise, you’ll be playing a game of whack-a-mole with configuring a serializer for a type it doesn’t really like.

    If you want a game of whack-a-mole with XmlSerializer: you can create an XmlAttributeOverrides instance, configure it by hand for your specific type (adding the attribute instances), and pass it into the XmlSerializer constructor. But this is ugly, quite fiddly, and you must must must cache and re-use the serializer instance (the normal automatic assembly cache/re-use doesn’t apply if you use that overload of the constructor). You can obtain (from the XmlAttributeOverrides instance) an XmlAttributes instance per-type or per-member, and then the XmlIgnore property to true as necessary. Frankly, I advise against this approach.

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

Sidebar

Related Questions

I'm trying to use the DataContractJsonSerializer to serialize and deserialize a c# object that
I'm trying to serialize an object to XML that has a number of properties,
I have an XML file that I'm trying to serialize into an object. Some
I'm trying to make a genric code to serialize and deserialize any object that
I am trying to serialize a PagedList object ( https://github.com/martijnboland/MvcPaging/blob/master/src/MvcPaging/PagedList.cs ) to Json, like
I'm trying to Serialize some Object, that contain an IList with Children Elements. The
Trying to pass complex class type that contains primitive types with interfaces and lists
I'm trying to serialize an object that have a duplicate node name using C#,
I am currently trying to serialize an object for a webproject that is hosted
I am trying to serialize a .net object contains another data contract object as

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.