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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:07:21+00:00 2026-05-16T21:07:21+00:00

Consider the following. You have a class that you want to serialize with XmlSerializer

  • 0

Consider the following. You have a class that you want to serialize with XmlSerializer which has a public generic method with a type constraint where the type is in a different assembly:

using BarStuff;

namespace FooStuff {
  public class Foo {
    ...
    public T GetBar<TBar, T>( string key ) where TBar : Bar<T> {
      ...
    }
  }
}

You wouldn’t expect the XmlSerializer to even concern itself with methods, and generally it doesn’t. The following both work fine:

//private, serializer doesn't care about it
private T GetBar<TBar, T>( string key ) where TBar : Bar<T> {
  ...
}

//no generic type constraint, serializer also doesn't care about it
public Bar GetBar( string key ) {
  ...
}   

Also, if the type Bar is in the same assembly as Foo then the serializer will also be perfectly happy.

When you execute the first example, if Bar is defined in a separate assembly you will get a runtime exception saying that you need to add a reference to the assembly containing Bar, even if you already have that assembly in your project references. You can get around this by using XmlInclude:

[XmlInclude(typeof(Bar))]
public class Foo {
  public T GetBar<TBar, T>( string key ) where TBar : Bar<T> {
    ...
  }
}

However if Bar is not serializable, and there’s no reason why it should be, you’ll now get a runtime exception the first time it hits something it can’t serialize, such as a public property that returns an interface as its type, a class without a parameterless constructor etc.!

Related but not as detailed: XmlSerializer is throwing InvalidOperationException when using the generic type constraint where

Also: Microsoft’s take on the problem

  • 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-16T21:07:22+00:00Added an answer on May 16, 2026 at 9:07 pm

    Some workarounds:

    • Use a different serializer such as DataContractSerializer
    • Ensure that the types are in the same assembly so that the XmlInclude is unnecessary (yuck)
    • Change Bar to make it serializable (yuck)
    • Avoid having methods of this kind, ie by only serializing DTO type objects and having any such functionality elsewhere
    • This is sneaky and hackish… include a dummy class that is serializable in the same assembly as your type Bar, then XmlInclude that instead, this will make the serializer happy, ie:

    Example:

    namespace BarStuff {
      //the serializer is perfectly happy with me
      public class DummyBar{}
    
      //the serializer doesn't like me
      public class Bar{
      ...
      }
    
      ...
    }
    
    using BarStuff;
    namespace FooStuff {
      [XmlInclude(typeof(DummyBar))]
      public class Foo {
        public T GetBar<TBar, T>( string key ) where TBar : Bar<T> {
          ...
        }
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following problem: You have a class 'A' that serves as a base
Please consider the following three .NET types: I have an interface, an abstract class,
Consider following scenario: I have RESTful URL /articles that returns list of articles user
Please consider the following: we have a Spring bean B which is advised with
Consider the following scenario We have a simple database that involves two entities: user
Consider the following situation: I have two android projects named P1 and P2 which
Consider the following code. public interface IFoo { } public class Bar { public
I'm trying to implement the following: I have an Items Manager, that has an
I have a template class which defines operators, that works on the template parameters.
Consider the following example: interface IPropertyCollection { public MethodWrapper GetPropertySetterByName(string name); //<<-- I want

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.