CodeDomSerializer 類別

定義

序列化物件 Graph 為一系列的 CodeDOM 陳述式 (Statement)。Serializes an object graph to a series of CodeDOM statements. 這個類別為序列化工具提供抽象基底類別。This class provides an abstract base class for a serializer.

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

範例

下列程式碼範例說明如何建立從 CodeDomSerializer衍生的自訂 CodeDOM 序列化程式。The following code example illustrates how to create a custom CodeDOM serializer that derives from CodeDomSerializer.

#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 來控制元件類型的元件初始化程式碼的產生。You can implement a custom CodeDomSerializer to control the generation of component initialization code for a type of component at design time.

若要為型別執行自訂 CodeDomSerializer,您必須:To implement a custom CodeDomSerializer for a type, you must:

  1. 定義衍生自 CodeDomSerializer 的類別。Define a class that derives from CodeDomSerializer.

  2. 針對序列化或還原序列化方法,執行方法覆寫。Implement method overrides for serialization or deserialization methods. (如需詳細資料,請參閱下列資訊)。(See the information below for details.)

  3. 使用 DesignerSerializerAttribute,將您的自訂 CodeDomSerializer 執行與元件的類型建立關聯。Associate your custom CodeDomSerializer implementation with a type of component using a DesignerSerializerAttribute.

若要執行用於產生元件之設定程式碼的序列化方法:To implement a serialization method for generating configuration code for a component:

  1. 在衍生自 CodeDomSerializer的類別內,覆寫基類的適當序列化或還原序列化方法。Within a class that derives from CodeDomSerializer, override an appropriate serialization or deserialization method of the base class.

  2. 如果您想要讓預設序列化程式產生執行預設元件設定的程式碼語句,您必須取得並呼叫元件的基底序列化程式。If you want the default serializer to generate code statements that perform the default component configuration, you must obtain and call the base serializer for the component. 若要取得元件的基底序列化程式,請呼叫傳遞給方法覆寫之 IDesignerSerializationManagerGetSerializer 方法。To obtain the base serializer for the component, call the GetSerializer method of the IDesignerSerializationManager passed to your method override. 將元件的類型傳遞給 GetSerializer 方法,以序列化的設定,以及您所要求之序列化程式的基底類型,也就是 CodeDomSerializerPass the GetSerializer method the type of the component to serialize the configuration of, along with the base type of serializer you are requesting, which is CodeDomSerializer. 使用傳遞給方法覆寫的 IDesignerSerializationManager 和物件,呼叫您在基底序列化程式上覆寫之相同名稱的方法。Call the method of the same name you are overriding on the base serializer, using the IDesignerSerializationManager and object passed to your method override. 如果您要執行 Serialize 方法,基底序列化程式的 Serialize 方法會傳回物件。If you are implementing the Serialize method, the Serialize method of the base serializer will return an object. 這個物件的類型取決於基底序列化程式的類型,這取決於您要序列化其值的元件類型而定。The type of this object depends on the type of base serializer which depends on the type of component you are serializing the values of. 如果您要執行 SerializeEventsSerializePropertiesSerializePropertiesToResources 方法,則必須建立新的 CodeStatementCollection 以包含所產生的程式碼語句,並將它傳遞給方法。If you are implementing the SerializeEvents, SerializeProperties, or SerializePropertiesToResources method, you must create a new CodeStatementCollection to contain the generated code statements, and pass it to the method.

  3. 如果您已經呼叫基底序列化程式方法,您將會有一個 CodeStatementCollection,其中包含要產生以初始化元件的語句。If you have called a base serializer method, you will have a CodeStatementCollection that contains the statements to generate to initialize the component. 否則,您應該建立 CodeStatementCollectionOtherwise you should create a CodeStatementCollection. 您可以新增代表語句的 CodeStatement 物件,以在元件設定程式碼中產生此集合。You can add CodeStatement objects representing statements to generate in the component configuration code to this collection.

  4. 傳回代表要產生之原始程式碼的 CodeStatementCollection,以設定元件。Return the CodeStatementCollection that represents the source code to generate to configure the component.

給實施者的注意事項

當您繼承自 CodeDomSerializer 時,您必須覆寫下列成員:Deserialize(IDesignerSerializationManager, Object)Serialize(IDesignerSerializationManager, Object)When you inherit from CodeDomSerializer, you must override the following members: Deserialize(IDesignerSerializationManager, Object) and Serialize(IDesignerSerializationManager, Object).

建構函式

CodeDomSerializer()

初始化 CodeDomSerializer 類別的新執行個體。Initializes a new instance of the CodeDomSerializer class.

方法

Deserialize(IDesignerSerializationManager, Object)

將指定序列化的 CodeDOM 物件還原序列化為物件。Deserializes the specified serialized CodeDOM object into an object.

DeserializeExpression(IDesignerSerializationManager, String, CodeExpression)

還原序列化指定的運算式。Deserializes the specified expression.

DeserializeInstance(IDesignerSerializationManager, Type, Object[], String, Boolean)

傳回指定型別的執行個體。Returns an instance of the given type.

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

如果已指定篩選且指定的物件符合指定篩選,則還原序列化該物件的屬性。Deserializes the properties of the specified object that match the specified filter, if a filter was specified.

DeserializeStatement(IDesignerSerializationManager, CodeStatement)

還原序列化指定的陳述式。Deserializes the specified statement.

DeserializeStatementToInstance(IDesignerSerializationManager, CodeStatement)

還原序列化陳述式。Deserializes a single statement.

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

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

傳回指定物件的運算式。Returns an expression for the given object.

(繼承來源 CodeDomSerializerBase)
GetHashCode()

作為預設雜湊函數。Serves as the default hash function.

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

尋找指定物件值的序列化程式。Locates a serializer for the given object value.

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

尋找指定型別的序列化程式。Locates a serializer for the given type.

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

決定指定的陳述式應隸屬於哪一個陳述式群組。Determines which statement group the given statement should belong to.

GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

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

傳回指定物件的唯一名稱。Returns a unique name for the given object.

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

傳回值,指出指定的物件是否已經序列化。Returns a value indicating whether the given object has been serialized.

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

傳回值,指出指定的物件是否已經序列化 (選擇性地考量預設運算式)。Returns a value indicating whether the given object has been serialized, optionally considering preset expressions.

(繼承來源 CodeDomSerializerBase)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

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

將指定的物件序列化為 CodeDOM 物件。Serializes the specified object into a CodeDOM object.

SerializeAbsolute(IDesignerSerializationManager, Object)

用預設值將指定的物件序列化。Serializes the given object, accounting for default values.

SerializeCreationExpression(IDesignerSerializationManager, Object, Boolean)

傳回運算式,表示指定物件的建立。Returns an expression representing the creation of the given object.

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

將指定的事件序列化至指定的陳述式集合中。Serializes the given event into the given statement collection.

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

序列化指定物件的所有事件。Serializes all events of the specified object.

SerializeMember(IDesignerSerializationManager, Object, MemberDescriptor)

在指定的物件上將指定的成員序列化。Serializes the given member on the given object.

SerializeMemberAbsolute(IDesignerSerializationManager, Object, MemberDescriptor)

將指定的成員序列化,說明預設值。Serializes the given member, accounting for default values.

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

使用所指定篩選序列化指定物件的所有屬性。Serializes all properties for the specified object, using the specified filter.

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

將指定的屬性序列化為資源。Serializes the specified properties to resources.

SerializeProperty(IDesignerSerializationManager, CodeStatementCollection, Object, PropertyDescriptor)

將指定物件上的屬性序列化。Serializes a property on the given object.

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

將指定的物件序列化為使用指定資源名稱的資源配套。Serializes the given object into a resource bundle using the given resource name.

SerializeResourceInvariant(IDesignerSerializationManager, String, Object)

使用不因文化特性而異將指定的物件序列化為使用指定資源名稱的資源套件組合。Serializes the given object into a resource bundle using the given resource name using invariant culture.

SerializeToExpression(IDesignerSerializationManager, Object)

序列化指定值為 CodeDOM 運算式。Serializes the specified value to a CodeDOM expression.

SerializeToReferenceExpression(IDesignerSerializationManager, Object)

序列化指定值為 CodeDOM 運算式。Serializes the specified value to a CodeDOM expression.

SerializeToResourceExpression(IDesignerSerializationManager, Object)

將指定的物件序列化為運算式。Serializes the given object into an expression.

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

將指定的物件序列化為適於不因文化特性而異的運算式。Serializes the given object into an expression appropriate for the invariant culture.

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

建立物件與運算式的關聯。Associates an object with an expression.

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

使物件與運算式產生關聯,也可以指定一個預設的運算式。Associates an object with an expression, optionally specifying a preset expression.

(繼承來源 CodeDomSerializerBase)
ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

適用於

另請參閱