I’m trying to implement a dictionary for use with WCF. My requirements are:
- actual (private variable or base
class) type equivalent to
Dictionary
- Comparer
=
System.StringComparer.InvariantCultureIgnoreCase
- Custom (override/new) Add(key,
value) method (to include
validations).
- Override ToString()
- Use of the same type on both the client and host
I’ve attempted using this class in a common project shared by the WCF host and client projects:
[Serializable]
public class MyDictionary : Dictionary<string, object>
{
public MyDictionary()
: base(System.StringComparer.InvariantCultureIgnoreCase)
{ }
public new void Add(string key, object value)
{ /* blah */ }
public override string ToString()
{ /* blah */ }
}
[DataContract]
[KnownType(typeof(MyDictionary))]
[KnownType(typeof(object[]))]
[KnownType(typeof(double[]))]
[KnownType(typeof(string[]))]
[KnownType(typeof(DateTime[]))]
public class ResultClass
{
public object Value{ get; set; }
/* More properties */
}
public class ParmData
{
public object Value{ get; set; }
/* More properties */
}
[DataContract]
[KnownType(typeof(MyDictionary))]
[KnownType(typeof(object[]))]
[KnownType(typeof(double[]))]
[KnownType(typeof(string[]))]
[KnownType(typeof(DateTime[]))]
public class ParameterClass
{
public List<ParmData> Data{ get; set; }
/* More properties */
}
[OperationContract]
ResultClass DoSomething(ParameterClass args);
Results:
- When I pass MyDictionary as one of the ParameterClass.Data.Value elements, I get a missing KnownType exception.
- I can safely return MyDictionary in the ResultClass, but it is no longer my type. It is just a Dictionary, and is not castable to
MyDictionary
. Also comparer = System.Collections.Generic.GenericEqualityComparer<string>
, not the case insensitive comparer I’m looking for.
The help I’m asking for is to either fix my failed attempt, or a completely different way to achieve my stated requirements. Any solution should not involve copying one dictionary to another.
Thanks
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…