Search code examples
c#dictionarywcfconstructorcase-insensitive

WCF CollectionDataContract Dictionary constructor


I'm using a WCF CollectionDataContract to pass data using a Dictionary and it is working very well.

[CollectionDataContract]
public class MyDataDictionary : Dictionary<string, MyItemValue> { }

[DataContract]
public class MyItemValue
{
    [DataMember] public int ID { get; set; }           
    [DataMember] public string Name { get; set; }
    // ...
    public IP21ItemValue() {
        ID = -1;
        Name = string.Empty;
        // ...
    }
}

I want to enhance my dictionary so it is case insensitive and I've tried a variety of things similar to these:

// Attempt to put the argument on the declaration.
[CollectionDataContract]
public class MyDataDictionary : Dictionary<string, MyItemValue>(StringComparer.InvariantCultureIgnoreCase) { }

// Attempt to instantiate it within the constructor...
[CollectionDataContract]
public class MyDataDictionary : Dictionary<string, MyItemValue> {
    public MyDataDictionary() {
        this = new Dictionary<string, MyItemValue>(StringComparer.InvariantCultureIgnoreCase);
    }
}

But I can't seem to make anything work. I'd really like to avoid wrapping the whole thing in a class where the dictionary is a data member. Is there a syntax that will do this?


Solution

  • Call the base class constructor, like this:

    public class MyDataDictionary : Dictionary<string, MyItemValue> {
        public MyDataDictionary() 
            :base(StringComparer.InvariantCultureIgnoreCase)
        {
        }
    }