Override dictionary constructor for a WCF DataMember

891 Views Asked by At

Is it possible specify which constructor the WCF serialization engine uses when deserializing a data member?

For example: I want to use this constructor to make a case-insensitive dictionary without creating a new class that inherits from Dictionary.

[DataMember]
Dictionary<string, string> Values { get; set; }

// Values should be created with this constructor
new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
2

There are 2 best solutions below

1
On BEST ANSWER

Is it possible specify which constructor the WCF serialization engine uses when deserializing a data member?

No it isn't, unless you use implement your custom serialization.

1
On

If you provide an implementation of your property setter method, you can instantiate for yourself the new case-insensitive Dictionary instance which will be the value of the property, and just copy into it the items from the instance provided by the serializer as the value parameter.

You'll need to be prepared to handle any exceptions due to key clashes if the source of the serialized Dictionary was case-sensitive.