次の方法で共有


CodeDomSerializer クラス

オブジェクト グラフを一連の CodeDOM ステートメントにシリアル化します。このクラスは、シリアライザの抽象基本クラスを提供します。

この型のすべてのメンバの一覧については、CodeDomSerializer メンバ を参照してください。

System.Object
   System.ComponentModel.Design.Serialization.CodeDomSerializer

MustInherit Public Class CodeDomSerializer
[C#]
public abstract class CodeDomSerializer
[C++]
public __gc __abstract class CodeDomSerializer
[JScript]
public abstract class CodeDomSerializer

スレッドセーフ

この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。

解説

カスタム CodeDomSerializer を実装すると、デザイン時にコンポーネントの型に対応するコンポーネント初期化コードの生成を制御できます。

ある型に対するカスタム CodeDomSerializer を実装するには、次の処理を実行する必要があります。

  1. CodeDomSerializer から派生するクラスを定義します。
  2. シリアル化や逆シリアル化の各メソッドのメソッド オーバーライドを実装します (詳細については、以下を参照してください)。
  3. DesignerSerializerAttribute を使用して、カスタム CodeDomSerializer 実装をコンポーネントの型に関連付けます。

コンポーネントの構成コードを生成するためのシリアル化メソッドを実装するには、次の手順に従います。

  1. CodeDomSerializer から派生したクラス内で、基本クラスの該当するシリアル化または逆シリアル化のメソッドをオーバーライドします。
  2. 既定のコンポーネント構成を実行するコード ステートメントを生成する、既定のシリアライザが必要な場合は、コンポーネントの基本シリアライザを取得して呼び出します。コンポーネントの基本シリアライザを取得するには、メソッド オーバーライドに渡される IDesignerSerializationManagerGetSerializer メソッドを呼び出します。要求するシリアライザの基本型 (CodeDomSerializer) と共に、構成をシリアル化するコンポーネントの型を GetSerializer メソッドに渡します。 IDesignerSerializationManager とメソッド オーバーライドに渡されるオブジェクトを使用して、オーバーライドする同じ名前のメソッドを基本シリアライザで呼び出します。 Serialize メソッドを実装する場合、基本シリアライザの Serialize メソッドはオブジェクトを返します。このオブジェクトの型は基本シリアライザの型に依存し、基本シリアライザの型は値をシリアル化するコンポーネントの型に依存します。 SerializeEvents メソッド、 SerializeProperties メソッド、または SerializePropertiesToResources メソッドを実装する場合、生成されるコード ステートメントを格納する新しい CodeStatementCollection を作成し、それをメソッドに渡す必要があります。
  3. 基本シリアライザ メソッドを呼び出すと、コンポーネントを初期化するために生成されるステートメントを格納した CodeStatementCollection が得られます。それ以外の場合は、 CodeStatementCollection を作成する必要があります。コンポーネントの構成コードに生成されるステートメントを表す CodeStatement オブジェクトをこのコレクションに追加できます。
  4. コンポーネントを構成するために生成されるソース コードを表す CodeStatementCollection を返します。

継承時の注意: CodeDomSerializer から継承する場合、 Deserialize および Serialize の両メンバをオーバーライドする必要があります。

使用例

[Visual Basic, C#, C++] CodeDomSerializer から派生したカスタム CodeDOM シリアライザを作成する方法を次の例に示します。

 
Imports System
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 'MyCodeDomSerializer

   <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 'MyComponent

End Namespace

[C#] 
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;
            }
        }
    }

}

[C++] 
#using <mscorlib.dll>
#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
{
    public __gc class MyComponent;
private __gc class MyCodeDomSerializer : public CodeDomSerializer {
public:
    Object* Deserialize(IDesignerSerializationManager* manager, Object* codeObject) {
        // This is how we associate the component with the serializer.
        CodeDomSerializer* baseClassSerializer = dynamic_cast<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);
    }

    Object* Serialize(IDesignerSerializationManager* manager, Object* value) {
        /* Associate the component with the serializer in the same manner as with
        Deserialize */
        CodeDomSerializer* baseClassSerializer = dynamic_cast<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 (dynamic_cast<CodeStatementCollection*>(codeObject)) {
            CodeStatementCollection* statements = dynamic_cast<CodeStatementCollection*>(codeObject);

            // The code statement collection is valid, so add a comment.
            String* commentText = S"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 __gc class MyComponent : public Component {
private:
    String* localProperty;
public:
    MyComponent() : localProperty(S"Component Property Value") {}
    __property String* get_LocalProperty() {
        return localProperty;
    }
    __property void set_LocalProperty( String* value ) {
        localProperty = value;
    }

};

}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

名前空間: System.ComponentModel.Design.Serialization

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

アセンブリ: System.Design (System.Design.dll 内)

参照

CodeDomSerializer メンバ | System.ComponentModel.Design.Serialization 名前空間 | DesignerSerializerAttribute