Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does XmlSerializer require types which inherit from IEnumerable to have an implementation of Add(System.Object)?

I am using xml serialization but now came across a runtime error I haven't seen before.

"To be XML serializable, types which inherit from IEnumerable must have an implementation of Add(System.Object) at all levels of their inheritance hierarchy. ImageEditor.EffectOptions does not implement Add(System.Object)"

It seems a little weird to be forced to implement a method via runtime exception, rather than compile time error such as missing methods from implemented interfaces.

Is this by design? Should this not be enforced via some sort of interface like XmlSerializable, etc?

Other than this I am wondering if the serializer guarantees passing a value of the right type where I can just cast it to the type, which in my case is EffectOption.

Or should I implement this Add (object) method to see if the object is of type EffectOption and if not throw an exception?

I haven't implemented this Add (object) method before but my guess is it's safer to just cast it to EffectOption and add it to EffectOptions collection.

EDIT: Here's the type itself:

public class EffectOptions : IEnumerable<EffectOption>
{
    public List<EffectOption> Options { get; private set; }

    //IEnumerable methods
}
like image 557
Joan Venge Avatar asked Mar 02 '11 20:03

Joan Venge


2 Answers

Because sub classes implicitly implement interface methods because of the base class but xmlserializer is using reflection and that is why you get the error at runtime and not compile time.

Try explicitly implementing and see what happens. I have not had this issue before so I'm not sure why you are unless you're doing something custom.

If you have your sub classes explicitly implementing the interface but not doing any implementation code (letting the implicit implementation of methods happen) then remove the interface from your sub type declaration as it should still be valid due to your base type. (someone tell me if i'm off here)

like image 194
Dustin Davis Avatar answered Nov 07 '22 15:11

Dustin Davis


I've just run into this issue and solved it by adding an add method:

public class EffectOptions : IEnumerable<EffectOption>
{
    public List<EffectOption> Options { get; private set; }

    public void Add(object o){
        this.Options.Add(o as EffectOption); //you may want to extend the code to check that this cast can be made,
                                             //and throw an appropriate error (otherwise it'll add null to your list)
    }

    //IEnumerable methods
}

I hope this helps.

like image 2
Iain Sproat Avatar answered Nov 07 '22 14:11

Iain Sproat