如何实现在WCF继承字典 [英] How to implement an inherited Dictionary over WCF
本文介绍了如何实现在WCF继承字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想实现一个字典与WCF使用。我的要求是:
- 实际(私有变量或基地
类)类型相当于
字典 - 的Comparer
=System.StringComparer.InvariantCultureIgnoreCase
- 自定义(重写/新)加(键,
值)方法(包括
验证)。 - 覆盖的ToString()
- 在客户机和主机上都使用相同类型的
我在由WCF主机和客户端项目共享一个共同的项目中使用此类尝试:
[Serializable接口]
公共类MyDictionary:字典<字符串对象>
{
公共MyDictionary()
:基地(System.StringComparer.InvariantCultureIgnoreCase)
{}
公共空间的新添加(字符串键,对象值)
{/ *废话* /}
公共重写字符串的ToString()
{/ *废话* /}
}
[DataContract]
[KnownType(typeof运算(MyDictionary))]
[KnownType(typeof运算(对象[]))]
[KnownType(typeof运算(双[]))]
[KnownType(typeof运算(字符串[]))]
[KnownType(typeof运算(DateTime的[]))]
公共类ResultClass
{
公共对象值{搞定;组; }
/ *更多的属性* /
}
公共类ParmData
{
公共对象值{搞定;组; }
/ *更多的属性* /
}
[DataContract]
[KnownType(typeof运算(MyDictionary))]
[KnownType(typeof运算(对象[])) ]
[KnownType(typeof运算(双[]))]
[KnownType(typeof运算(字符串[]))]
[KnownType(typeof运算(DateTime的[]))]
公共类ParameterClass
{
公开名单< ParmData>数据{搞定;组; }
/ *更多的属性* /
}
[OperationContract的]
ResultClass DoSomething的(ParameterClass参数);
结果:
- 当我通过MyDictionary作为ParameterClass.Data.Value元素之一,我得到一个缺少KnownType例外。
- 我可以放心地在ResultClass返回MyDictionary,但它不再我喜欢的类型。这仅仅是一个字典,而不是强制转换为
MyDictionary
。此外比较器=System.Collections.Generic.GenericEqualityComparer<字符串方式>
,不区分大小写的比较器我正在寻找
我要问的帮助是要么修复我的尝试失败,或完全不同的方式来实现我的规定要求。任何解决方案不应涉及复制一本词典到另一个。
感谢
解决方案
<与SvcUtil工具UL> CollectionDataContract
属性作为jezell建议来源: WCF集合类型共享
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
解决方案
- Use the
CollectionDataContract
attribute as jezell suggested - Manually generate the reference (proxy) code with SvcUtil, using the /collectionType parameter. This parameter is not supported by the vs2008 service reference GUI.
Source: WCF Collection Type Sharing
这篇关于如何实现在WCF继承字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文