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: http://stackoverflow.com/questions/2361563/xmlserializer-is-throwing-invalidoperationexception-when-using-the-generic-type-c