Como adicionar o atributo XmlInclude dinamicamente
Eu tenho as seguintes aulas
[XmlRoot]
public class AList
{
public List<B> ListOfBs {get; set;}
}
public class B
{
public string BaseProperty {get; set;}
}
public class C : B
{
public string SomeProperty {get; set;}
}
public class Main
{
public static void Main(string[] args)
{
var aList = new AList();
aList.ListOfBs = new List<B>();
var c = new C { BaseProperty = "Base", SomeProperty = "Some" };
aList.ListOfBs.Add(c);
var type = typeof (AList);
var serializer = new XmlSerializer(type);
TextWriter w = new StringWriter();
serializer.Serialize(w, aList);
}
}
Agora, quando tento executar o código, recebi uma InvalidOperationException na última linha dizendo que
O tipo XmlTest.C não era esperado. Use o atributo XmlInclude ou SoapInclude para especificar tipos que não são conhecidos estaticamente.
Eu sei que adicionar um atributo [XmlInclude (typeof (C))] com [XmlRoot] resolveria o problema. Mas quero alcançá-lo dinamicamente. Porque no meu projeto a classe C não é conhecida antes do carregamento. A classe C está sendo carregada como um plug-in, portanto, não é possível adicionar o atributo XmlInclude lá.
Eu tentei tambem com
TypeDescriptor.AddAttributes(typeof(AList), new[] { new XmlIncludeAttribute(c.GetType()) });
antes
var type = typeof (AList);
mas não adianta. Ainda está dando a mesma exceção.
Alguém tem alguma idéia de como alcançá-lo?