c# - Type comparison not returning expected result -
i using following code compare types datacontractserializer re-initialize correct type if necessary.
private void initializeserializer(type type) { if (this.serializer == null) { this.serializer = new datacontractserializer(type); this.typetoserialize = type; } else { if (this.typetoserialize != null) { if (this.typetoserialize.gettype() != type.gettype()) { this.serializer = new datacontractserializer(type); this.typetoserialize = type; } } } }
for reason when compare 2 types result true , never enter final 'if' statement , re-initialize serialiser.
i can set break point @ comparison , see 2 types list<host>
(this.typetoserialize.gettype()) , post
(type.gettype())
both host , post share common ancestor shouldn't affecting result.
you calling gettype()
on system.type
. return system.type
object describes system.type
itself.
this makes code
if (this.typetoserialize.gettype() != type.gettype()) { ... }
equivalent to:
if(typeof(system.type) != typeof(system.type)) // false { ... // never enters here }
i'm guessing mean doing is:
if(typetoserialize != type) { ... }
Comments
Post a Comment