Reputation: 1060
This is what I'm looking to do:
public class NormalClass
{
[XmlAttribute]
public int Example;
}
[XmlRoot]
public class GenericClass<T> where T : HasXmlElementAttribute
{
[XmlArray]
public List<T> Variables;
}
I thought where T : IXmlSerializable
might work, but it did not.
Is this even possible to do? If so, what is the proper way?
Is there a way to achieve this same goal? Is there a way to only allow classes that can be xml serialized?
Thanks
Upvotes: 1
Views: 116
Reputation: 149020
No, you cannot use generic type constraints to limit the type parameters by what attributes they are decorated with. You can only use generic type constraints to limit type parameters by:
Further Reading:
Upvotes: 0
Reputation: 887413
You cannot constrain a type parameter based on the presence of an attribute.
Side note: This justification is not quite valid; constructors aren't part of the type system either, yet : new()
is a valid constraint.
Upvotes: 1