共用方式為


CodeDomSerializer 類別

定義

序列化物件 Graph 為一系列的 CodeDOM 陳述式 (Statement)。 這個類別為序列化工具提供抽象基底類別。

public ref class CodeDomSerializer abstract
public ref class CodeDomSerializer : System::ComponentModel::Design::Serialization::CodeDomSerializerBase
public abstract class CodeDomSerializer
public class CodeDomSerializer : System.ComponentModel.Design.Serialization.CodeDomSerializerBase
type CodeDomSerializer = class
type CodeDomSerializer = class
    inherit CodeDomSerializerBase
Public MustInherit Class CodeDomSerializer
Public Class CodeDomSerializer
Inherits CodeDomSerializerBase
繼承
CodeDomSerializer
繼承
CodeDomSerializer
衍生

範例

下列程式代碼範例說明如何建立衍生自 CodeDomSerializer的自定義CodeDOM串行化程式。

#using <System.Drawing.dll>
#using <System.dll>
#using <System.Design.dll>

using namespace System;
using namespace System::CodeDom;
using namespace System::ComponentModel;
using namespace System::ComponentModel::Design;
using namespace System::ComponentModel::Design::Serialization;
using namespace System::Drawing;
using namespace System::Windows::Forms;

namespace CodeDomSerializerSample
{
   ref class MyComponent;
   private ref class MyCodeDomSerializer: public CodeDomSerializer
   {
   public:
      Object^ Deserialize( IDesignerSerializationManager^ manager, Object^ codeObject ) new
      {
         // This is how we associate the component with the serializer.
         CodeDomSerializer^ baseClassSerializer = (CodeDomSerializer^)(
            manager->GetSerializer(
               MyComponent::typeid->BaseType, CodeDomSerializer::typeid ));
         
         /* This is the simplest case, in which the class just calls the base class
            to do the work. */
         return baseClassSerializer->Deserialize( manager, codeObject );
      }

      Object^ Serialize( IDesignerSerializationManager^ manager, Object^ value ) new
      {
         /* Associate the component with the serializer in the same manner as with
            Deserialize */
         CodeDomSerializer^ baseClassSerializer = (CodeDomSerializer^)(
            manager->GetSerializer(
               MyComponent::typeid->BaseType, CodeDomSerializer::typeid ));

         Object^ codeObject = baseClassSerializer->Serialize( manager, value );
         
         /* Anything could be in the codeObject.  This sample operates on a
            CodeStatementCollection. */
         if ( (CodeStatementCollection^)(codeObject) )
         {
            CodeStatementCollection^ statements = (CodeStatementCollection^)(codeObject);
            
            // The code statement collection is valid, so add a comment.
            String^ commentText = "This comment was added to this object by a custom serializer.";
            CodeCommentStatement^ comment = gcnew CodeCommentStatement( commentText );
            statements->Insert( 0, comment );
         }
         return codeObject;
      }
   };

   [DesignerSerializer(CodeDomSerializerSample::MyCodeDomSerializer::typeid,
      CodeDomSerializer::typeid)]
   public ref class MyComponent: public Component
   {
   private:
      String^ localProperty;

   public:
      MyComponent()
      {
         localProperty = "Component Property Value";
      }

      property String^ LocalProperty 
      {
         String^ get()
         {
            return localProperty;
         }
         void set( String^ value )
         {
            localProperty = value;
         }
      }
   };
}
using System;
using System.CodeDom;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.ComponentModel.Design.Serialization;
using System.Drawing;
using System.Windows.Forms;
 
namespace CodeDomSerializerSample
{
    internal class MyCodeDomSerializer : CodeDomSerializer {
        public override object Deserialize(IDesignerSerializationManager manager, object codeObject) {
            // This is how we associate the component with the serializer.
                CodeDomSerializer baseClassSerializer = (CodeDomSerializer)manager.
                GetSerializer(typeof(MyComponent).BaseType, typeof(CodeDomSerializer));

            /* This is the simplest case, in which the class just calls the base class
                to do the work. */
            return baseClassSerializer.Deserialize(manager, codeObject);
        }
 
        public override object Serialize(IDesignerSerializationManager manager, object value) {
            /* Associate the component with the serializer in the same manner as with
                Deserialize */
            CodeDomSerializer baseClassSerializer = (CodeDomSerializer)manager.
                GetSerializer(typeof(MyComponent).BaseType, typeof(CodeDomSerializer));
 
            object codeObject = baseClassSerializer.Serialize(manager, value);
 
            /* Anything could be in the codeObject.  This sample operates on a
                CodeStatementCollection. */
            if (codeObject is CodeStatementCollection) {
                CodeStatementCollection statements = (CodeStatementCollection)codeObject;
 
                // The code statement collection is valid, so add a comment.
                string commentText = "This comment was added to this object by a custom serializer.";
                CodeCommentStatement comment = new CodeCommentStatement(commentText);
                statements.Insert(0, comment);
            }
            return codeObject;
        }
    }
 
    [DesignerSerializer(typeof(MyCodeDomSerializer), typeof(CodeDomSerializer))]
    public class MyComponent : Component {
        private string localProperty = "Component Property Value";
        public string LocalProperty {
            get {
                return localProperty;
            }
            set {
                localProperty = value;
            }
        }
    }
}
Imports System.CodeDom
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.ComponentModel.Design.Serialization
Imports System.Drawing
Imports System.Windows.Forms

Namespace CodeDomSerializerSample
   Friend Class MyCodeDomSerializer
      Inherits CodeDomSerializer

      Public Overrides Function Deserialize(ByVal manager As IDesignerSerializationManager, _
                                                ByVal codeObject As Object) As Object
         ' This is how we associate the component with the serializer.
         Dim baseClassSerializer As CodeDomSerializer = CType(manager.GetSerializer( _
                GetType(MyComponent).BaseType, GetType(CodeDomSerializer)), CodeDomSerializer)

         ' This is the simplest case, in which the class just calls the base class
         '  to do the work. 
         Return baseClassSerializer.Deserialize(manager, codeObject)
      End Function 'Deserialize

      Public Overrides Function Serialize(ByVal manager As IDesignerSerializationManager, _
                                            ByVal value As Object) As Object
         ' Associate the component with the serializer in the same manner as with
         '  Deserialize
         Dim baseClassSerializer As CodeDomSerializer = CType(manager.GetSerializer( _
                GetType(MyComponent).BaseType, GetType(CodeDomSerializer)), CodeDomSerializer)

         Dim codeObject As Object = baseClassSerializer.Serialize(manager, value)

         ' Anything could be in the codeObject.  This sample operates on a
         '  CodeStatementCollection.
         If TypeOf codeObject Is CodeStatementCollection Then
            Dim statements As CodeStatementCollection = CType(codeObject, CodeStatementCollection)

            ' The code statement collection is valid, so add a comment.
            Dim commentText As String = "This comment was added to this object by a custom serializer."
            Dim comment As New CodeCommentStatement(commentText)
            statements.Insert(0, comment)
         End If
         Return codeObject
      End Function 'Serialize
   End Class

   <DesignerSerializer(GetType(MyCodeDomSerializer), GetType(CodeDomSerializer))> _
   Public Class MyComponent
      Inherits Component
      Private localProperty As String = "Component Property Value"

      Public Property LocalProp() As String
         Get
            Return localProperty
         End Get
         Set(ByVal Value As String)
            localProperty = Value
         End Set
      End Property
   End Class

End Namespace

備註

您可以在設計時間實作自定義 CodeDomSerializer ,以控制元件類型的元件初始化程式代碼產生。

若要實作類型的自定義 CodeDomSerializer ,您必須:

  1. 定義衍生自 CodeDomSerializer 的類別。

  2. 實作串行化或還原串行化方法的方法覆寫。 (如需詳細資訊,請參閱下列資訊。)

  3. 使用 DesignerSerializerAttribute將自定義CodeDomSerializer實作與元件類型產生關聯。

若要實作串行化方法,以產生元件的組態程序代碼:

  1. 在衍生自 CodeDomSerializer的類別內,覆寫基類的適當串行化或還原串行化方法。

  2. 如果您想要讓預設串行化程式產生執行預設元件元件組態的程式代碼語句,您必須取得並呼叫元件的基底串行化程式。 若要取得元件的基底串行化程式,請呼叫 GetSerializer 傳遞至方法覆寫的 IDesignerSerializationManager 方法。 GetSerializer傳遞元件的型別以串行化 組態的方法,以及您所要求的串行化程式基底類型,也就是 CodeDomSerializer。 使用 IDesignerSerializationManager 傳遞至方法覆寫的 和 物件,呼叫您在基底串行化程式上覆寫相同名稱的方法。 如果您要實作 Serialize 方法, Serialize 基底串行化程式的方法會傳回 物件。 此物件的類型取決於基底串行化程序的類型,這取決於您要串行化值的元件類型。 如果您要實作 SerializeEventsSerializePropertiesSerializePropertiesToResources 方法,則必須建立新的 CodeStatementCollection ,以包含產生的程式代碼語句,並將它傳遞至 方法。

  3. 如果您已呼叫基底串行化程式方法,您將會有 , CodeStatementCollection 其中包含要產生以初始化元件的 語句。 否則,您應該建立 CodeStatementCollection。 您可以將代表 語句的物件新增 CodeStatement 至這個集合,以在元件組態程式代碼中產生。

  4. 傳回 , CodeStatementCollection 表示要產生以設定元件的原始程式碼。

給實施者的注意事項

當您繼承自 CodeDomSerializer 時,您必須覆寫下列成員:Deserialize(IDesignerSerializationManager, Object)Serialize(IDesignerSerializationManager, Object)

建構函式

CodeDomSerializer()

初始化 CodeDomSerializer 類別的新執行個體。

方法

Deserialize(IDesignerSerializationManager, Object)

將指定序列化的 CodeDOM 物件還原序列化為物件。

DeserializeExpression(IDesignerSerializationManager, String, CodeExpression)

還原序列化指定的運算式。

DeserializeExpression(IDesignerSerializationManager, String, CodeExpression)

將指定的運算式還原序列化為記憶體中物件。

(繼承來源 CodeDomSerializerBase)
DeserializeInstance(IDesignerSerializationManager, Type, Object[], String, Boolean)

傳回指定型別的執行個體。

(繼承來源 CodeDomSerializerBase)
DeserializePropertiesFromResources(IDesignerSerializationManager, Object, Attribute[])

如果已指定篩選且指定的物件符合指定篩選,則還原序列化該物件的屬性。

DeserializePropertiesFromResources(IDesignerSerializationManager, Object, Attribute[])

從不因文化特性而異的資源配套還原序列化指定物件上的屬性。

(繼承來源 CodeDomSerializerBase)
DeserializeStatement(IDesignerSerializationManager, CodeStatement)

還原序列化指定的陳述式。

DeserializeStatement(IDesignerSerializationManager, CodeStatement)

藉由解譯並執行 CodeDOM 陳述式還原序列化陳述式。

(繼承來源 CodeDomSerializerBase)
DeserializeStatementToInstance(IDesignerSerializationManager, CodeStatement)

還原序列化陳述式。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetExpression(IDesignerSerializationManager, Object)

傳回指定物件的運算式。

(繼承來源 CodeDomSerializerBase)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetSerializer(IDesignerSerializationManager, Object)

尋找指定物件值的序列化程式。

(繼承來源 CodeDomSerializerBase)
GetSerializer(IDesignerSerializationManager, Type)

尋找指定型別的序列化程式。

(繼承來源 CodeDomSerializerBase)
GetTargetComponentName(CodeStatement, CodeExpression, Type)

決定指定的陳述式應隸屬於哪一個陳述式群組。

GetType()

取得目前執行個體的 Type

(繼承來源 Object)
GetUniqueName(IDesignerSerializationManager, Object)

傳回指定物件的唯一名稱。

(繼承來源 CodeDomSerializerBase)
IsSerialized(IDesignerSerializationManager, Object)

傳回值,指出指定的物件是否已經序列化。

(繼承來源 CodeDomSerializerBase)
IsSerialized(IDesignerSerializationManager, Object, Boolean)

傳回值,指出指定的物件是否已經序列化 (選擇性地考量預設運算式)。

(繼承來源 CodeDomSerializerBase)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
Serialize(IDesignerSerializationManager, Object)

將指定的物件序列化為 CodeDOM 物件。

SerializeAbsolute(IDesignerSerializationManager, Object)

用預設值將指定的物件序列化。

SerializeCreationExpression(IDesignerSerializationManager, Object, Boolean)

傳回運算式,表示指定物件的建立。

(繼承來源 CodeDomSerializerBase)
SerializeEvent(IDesignerSerializationManager, CodeStatementCollection, Object, EventDescriptor)

將指定的事件序列化至指定的陳述式集合中。

(繼承來源 CodeDomSerializerBase)
SerializeEvents(IDesignerSerializationManager, CodeStatementCollection, Object, Attribute[])

序列化指定物件的所有事件。

SerializeEvents(IDesignerSerializationManager, CodeStatementCollection, Object, Attribute[])

將指定的事件序列化至指定的陳述式集合中。

(繼承來源 CodeDomSerializerBase)
SerializeMember(IDesignerSerializationManager, Object, MemberDescriptor)

在指定的物件上將指定的成員序列化。

SerializeMemberAbsolute(IDesignerSerializationManager, Object, MemberDescriptor)

將指定的成員序列化,說明預設值。

SerializeProperties(IDesignerSerializationManager, CodeStatementCollection, Object, Attribute[])

使用所指定篩選序列化指定物件的所有屬性。

SerializeProperties(IDesignerSerializationManager, CodeStatementCollection, Object, Attribute[])

將指定物件上的屬性,序列化至指定的陳述式集合。

(繼承來源 CodeDomSerializerBase)
SerializePropertiesToResources(IDesignerSerializationManager, CodeStatementCollection, Object, Attribute[])

將指定的屬性序列化為資源。

SerializePropertiesToResources(IDesignerSerializationManager, CodeStatementCollection, Object, Attribute[])

將指定物件上的屬性序列化為不因文化特性而異的資源配套。

(繼承來源 CodeDomSerializerBase)
SerializeProperty(IDesignerSerializationManager, CodeStatementCollection, Object, PropertyDescriptor)

將指定物件上的屬性序列化。

(繼承來源 CodeDomSerializerBase)
SerializeResource(IDesignerSerializationManager, String, Object)

將指定的物件序列化為使用指定資源名稱的資源配套。

SerializeResource(IDesignerSerializationManager, String, Object)

將指定的物件序列化為使用指定資源名稱的資源配套。

(繼承來源 CodeDomSerializerBase)
SerializeResourceInvariant(IDesignerSerializationManager, String, Object)

使用不因文化特性而異將指定的物件序列化為使用指定資源名稱的資源套件組合。

SerializeResourceInvariant(IDesignerSerializationManager, String, Object)

將指定的物件序列化為使用指定資源名稱的資源配套。

(繼承來源 CodeDomSerializerBase)
SerializeToExpression(IDesignerSerializationManager, Object)

序列化指定值為 CodeDOM 運算式。

SerializeToExpression(IDesignerSerializationManager, Object)

將指定的物件序列化為運算式。

(繼承來源 CodeDomSerializerBase)
SerializeToReferenceExpression(IDesignerSerializationManager, Object)
已淘汰.
已淘汰.

序列化指定值為 CodeDOM 運算式。

SerializeToResourceExpression(IDesignerSerializationManager, Object)

將指定的物件序列化為運算式。

(繼承來源 CodeDomSerializerBase)
SerializeToResourceExpression(IDesignerSerializationManager, Object, Boolean)

將指定的物件序列化為適於不因文化特性而異的運算式。

(繼承來源 CodeDomSerializerBase)
SetExpression(IDesignerSerializationManager, Object, CodeExpression)

建立物件與運算式的關聯。

(繼承來源 CodeDomSerializerBase)
SetExpression(IDesignerSerializationManager, Object, CodeExpression, Boolean)

使物件與運算式產生關聯,也可以指定一個預設的運算式。

(繼承來源 CodeDomSerializerBase)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於

另請參閱