自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

WCF依賴屬性具體概念詳解

開發(fā) 開發(fā)工具
WCF依賴屬性在.NetFramework中存在著兩種方式,他們分別集成了不同的依賴對象,但是名稱卻相同。在這里我們將會為大家詳細介紹一下相關(guān)內(nèi)容。

WCF開發(fā)插件功能非常強大,其中有很多比較重要的功能特點值得我們?nèi)ド钊胙芯俊T谶@篇文章中我們就為大家詳細介紹了一下其中一個重要的基礎(chǔ)知識,關(guān)于WCF依賴屬性的相關(guān)介紹。#t#

眾所周知.NetFramework中存在著兩種WCF依賴屬性,他們也分別集成著不同但名稱相同的依賴對象:

System.Windows.DependencyProperty:System.Windows.DependencyObject

System.Workflow.ComponentModel.DependencyProperty:System.Workflow.ComponentModel.DependencyObject

System.Window.DependencyProperty主要用于WPF中,我們可以以注冊的形式聲明這種‘特別’的屬性,聲明中可以設(shè)置Metadata,PropertyChangeCallBack...等等,讓我能用幾句簡單的代碼來實現(xiàn)強大的WPF操作。

 

System.Workflow.ComponentModel.DependencyProperty相對于前者,是一個簡化版本,只可以在聲明中可以設(shè)置Metadata,但對于WorkflowFoundation這就足夠用了。

 

兩種WCF依賴屬性對各自的技術(shù),都不同程度的提供了很好的支持,讓我們在實際開發(fā)中能夠更高效的書寫代碼,但是我們能不能像一般的屬性那樣隨意聲明,并運用?至少在WCF中我們很難使用這種特殊的屬性。

以工作流中的System.workflow.ComponentModel.DependencyObject為例

如果我們想像一般自定義類那樣,在聲明完DataContract和DataMember后便可在基于WCF的應(yīng)用程序中應(yīng)用,會遇到UserData這個繼承于IDictionary的討厭屬性在WCF中無法序列化。如:

 

  1. [DataContract]  
  2. public class WorkFlowParameter : DependencyObject  
  3. {  
  4. public static readonly DependencyProperty IDProperty =  
  5. DependencyProperty.Register("ID", typeof(Guid), typeof
    (WorkFlowParameter),new PropertyMetadata("UserDefinedProperty"));  
  6. [DataMember]  
  7. public Guid ID  
  8. {  
  9. get { return (Guid)GetValue(IDProperty); }  
  10. set { SetValue(IDProperty, value); }  
  11. }  

 

 

像這樣一個看起來很平常的類,在WCF應(yīng)用中,我們只能無語了。

 

為了使得包含WCF依賴屬性的自定義類能在WCF中正常使用

我們可以以下面的步驟自己動手寫序列化方法

1.在自定義類中繼承ISerializable接口,并實現(xiàn)構(gòu)造函數(shù)以及GetObjectData方法

如:

 

  1. public class WorkFlowParameter : DependencyObject,ISerializable  
  2. {  
  3. //在Deserialize時使用  
  4. public WorkFlowParameter(SerializationInfo info, 
    StreamingContext context)   
  5. {  
  6. ID = new Guid (info.GetString("ID"));  
  7. ParameterName = info.GetString("ParameterName");  
  8. }  
  9. //在Serialize時調(diào)用,把除了UserData以外我們自定義的屬性添加進來進行序列化  
  10. public void GetObjectData(SerializationInfo info, 
    StreamingContext context)  
  11. {  
  12. IList<DependencyProperty> properties = 
    DependencyProperty.FromType(this.GetType());  
  13. if(properties.Count > 0)  
  14. {  
  15. foreach(DependencyProperty property in properties)  
  16. {  
  17. if(property.Name != "UserData")  
  18. {  
  19. info.AddValue(property.Name, GetValue(property));  
  20. }  
  21. }  
  22. }  
  23. }  

 

 

2.經(jīng)過我們自定義序列化后,我們可以正常使用了

 

如果你遇到類型XXXX不能為 ISerializable,且不能具有 DataContractAttribute 屬性這時候我們需要在WCF服務(wù)中,我們可以把類中的

[DataContract]去掉

 

[DataContract]//去掉

public class WorkFlowParameter : DependencyObject

 

 

再試試,大功告成了。呵呵。

以上就是對WCF依賴屬性的相關(guān)介紹。

責任編輯:曹凱 來源: 博客園
相關(guān)推薦

2010-02-01 14:53:42

C++屬性

2009-12-07 16:57:39

WCF安全性

2009-12-31 17:21:41

Silverlight

2010-02-23 16:32:29

WCF服務(wù)

2009-12-21 17:05:59

WCF自托管宿主

2010-02-26 14:39:27

WCF服務(wù)寄宿

2010-02-24 17:17:04

WCF宿主環(huán)境

2010-02-23 14:48:38

WCF事件通知

2009-12-21 15:33:07

WCF集合元素

2009-12-28 16:54:30

WPF注釋

2009-12-22 14:31:27

WCF序列化依賴屬性

2009-12-30 14:36:29

Silverlight

2010-02-24 16:30:52

WCF常見錯誤

2009-12-24 15:36:41

WPF邏輯樹

2009-12-31 15:31:15

ADO.NET特性

2010-02-26 15:53:35

WCF套接字連接中斷

2010-02-25 11:23:29

WCF返回自定義格式

2009-12-28 14:32:31

WPF窗體對話框

2009-12-21 18:19:19

WCF地址類型

2010-02-25 14:26:48

WCF特點
點贊
收藏

51CTO技術(shù)棧公眾號