有一个朋友提到:KeyValuePair默认不能被序列化,你须要自定义一个KeyValuePair并将其标志为[Serializable]” ,在这里再次谢谢他,谢谢。 问题一:应该怎样自定义呢??? “KeyValuePair”类型作为“服务接口方法的参数”会报错,但是作为实体类的属性没有问题。 实体对象属性如下: public KeyValuePair<Period, string> Date
{
get; set;
} 服务接口方法: public List<Monitoring> GetMonitoringEntityData(Monitoring.EnergyType energytype, KeyValuePair<Monitoring.Period, string> monitordate,KeyValuePair<Monitoring.MonitorType, string> monitortype, string meterusetype, bool issubstation)问题二:
“NET简单类型,你可以使用Serializable属性,这些你就不须要显示定义DataContract了,其它的复杂类型(例如类中还包含有其它自定类型的属性时),这时我就要使用DataContract与DataMember来显示标记类与属性等”“Serializable属性”怎么被使用的???什么也没做啊?是不是 [SerializableAttribute]
[ComVisibleAttribute(true)]
publicsealedclass String : IComparable,
ICloneable, IConvertible, IComparable<string>, IEnumerable<char>,
IEnumerable, IEquatable<string>[SerializableAttribute]
public struct KeyValuePair<TKey, TValue>
是不是“SerializableAttribute” 标记??? “KeyValuePair”和“String”类型都有啊
{
get; set;
} 服务接口方法: public List<Monitoring> GetMonitoringEntityData(Monitoring.EnergyType energytype, KeyValuePair<Monitoring.Period, string> monitordate,KeyValuePair<Monitoring.MonitorType, string> monitortype, string meterusetype, bool issubstation)问题二:
“NET简单类型,你可以使用Serializable属性,这些你就不须要显示定义DataContract了,其它的复杂类型(例如类中还包含有其它自定类型的属性时),这时我就要使用DataContract与DataMember来显示标记类与属性等”“Serializable属性”怎么被使用的???什么也没做啊?是不是 [SerializableAttribute]
[ComVisibleAttribute(true)]
publicsealedclass String : IComparable,
ICloneable, IConvertible, IComparable<string>, IEnumerable<char>,
IEnumerable, IEquatable<string>[SerializableAttribute]
public struct KeyValuePair<TKey, TValue>
是不是“SerializableAttribute” 标记??? “KeyValuePair”和“String”类型都有啊
上面的红色部分就是“MSDN”上的啊。“KeyValuePair”作为“服务接口方法”的参数时,报错的啊
{
get; set;
}自动实现的属性不能序列化
你有没有看我最后给你的建议。ServiceKnownType
谢谢您,“ServiceKnownType”是用来解决抽象类问题的,那个问题解决了啊。
做Demo试了下,用“[DataContract]”和“[DataMember]”进行了标记,返回错误。错误信息:格式化程序尝试对消息反序列化时引发异常: 尝试对参数 进行反序列化时出错: testone。InnerException 消息是““EndElement”命名空间“”中的“testone”并非所需元素。所需元素应为“key”。”。有关详细信息,请参见 InnerException。Demo:1、WCF方法 namespace KeyValuePairTest.Web
{
[ServiceContract(Namespace = "")]
[SilverlightFaultBehavior]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MyService
{
[OperationContract]
public List<keyvaluepairtest> DoWork(KeyValuePair<string,string> testone)
{
List<keyvaluepairtest> keyvaluepairtest = new List<keyvaluepairtest> ();
keyvaluepairtest test = new keyvaluepairtest { one = new KeyValuePair<string, string>("1", "one") }; keyvaluepairtest.Add(test);
return keyvaluepairtest;
} // 在此处添加更多操作并使用 [OperationContract] 标记它们
}
[DataContract]
public class keyvaluepairtest
{
[DataMember]
public KeyValuePair<string, string> one
{
get;
set;
}
}
}2、Silverlightpublic partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent(); ServiceReference1.MyServiceClient client = new ServiceReference1.MyServiceClient();
KeyValuePair<string,string> keyvaluepair=new KeyValuePair<string,string>("1","1");
client.DoWorkAsync(keyvaluepair);
client.DoWorkCompleted += new EventHandler<ServiceReference1.DoWorkCompletedEventArgs>(client_DoWorkCompleted);
} void client_DoWorkCompleted(object sender, ServiceReference1.DoWorkCompletedEventArgs e)
{
ObservableCollection<ServiceReference1.keyvaluepairtest> test = e.Result; textBox1.Text = test.FirstOrDefault().one.Value;
}
}问题: 怎么回事??? 用“[DataContract]”和“[DataMember]”标记一样报错
为了解决这个麻烦的BUG,建议你直接使用Dictionary<string, string>来传输,Dictionary是支持序列化的,且工作正常,而访问其内部的KeyValuePair也方便,直接Linq表达式一下搞定,如果你传输的KeyValuePair和Dictionary是完全无关的类型,那么完全可以自定义一个其它类而不要使用系统自带的那个只有Get而没有Set的KeyValuePair类型。
Ok,使用“Dictionary”应该没有问题的吧