0

I have a huge amount of classes with serialization-attributes as follows:

[XmlRoot(...)]
public MyClass { }

or also

[XmlType(...)]
public MyClass { }

Or whatever exists. How do I get all those types that have xml-serializer-information? I tried by checking if the types implement IXmlSerializable but that gives me only a few as most of the classes have only these attributes set whilst not implementing the interface explicitly.

Is there any way to get all those types that have such an attribute?

EDIT: I know that I can get the classes that have a special attribute by using GetCustomAttributes on the type. This seems to be inconvenient to me because I have to know all these attributes (XmlRoot, XmlType, XmlInclude, any missing that are applayable to classes?). Furthermore such a query would be quite ugly:

if (type.GetCustomAttribute(typeof(XmlRoot)).Length > 0 || 
    type.GetCustomAttribute(typeof(XmlType)).Length > 0 ||
    type.GetCustomAttribute(typeof(XmlRoot)).Length > 0)

Having said this I hoped for another solution.

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
  • http://stackoverflow.com/a/4852926/996081 – cbr Jun 19 '15 at 13:39
  • @cubrr I already knew this article but hoped I do not have to test for every possible xml-attribute – MakePeaceGreatAgain Jun 19 '15 at 13:40
  • Since solution comes to "how find types with X attribute", voted to close. – Dennis Jun 19 '15 at 13:40
  • 1
    Then just get all attributes which are in the `System.Xml.Serialization` namespace: `List types = assembly.GetTypes().Where(t => Attribute.GetCustomAttributes(t).Any(a => a.GetType().Namespace == "System.Xml.Serialization")).ToList();` – cbr Jun 19 '15 at 13:50
  • @cubrr Too sad I can´t accept this as answer, it´s exactly what I was looking for. – MakePeaceGreatAgain Jun 19 '15 at 14:21
  • Since you edited it 20 minutes ago, the question is now in the [reopen review queue](http://meta.stackexchange.com/help/reopen-questions). I'll check back on this question and add it as the answer if it's reopened. – cbr Jun 19 '15 at 14:38

0 Answers0