CodeDomSerializer 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
序列化物件 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的自定義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 ,您必須:
定義衍生自 CodeDomSerializer 的類別。
實作串行化或還原串行化方法的方法覆寫。 (如需詳細資訊,請參閱下列資訊。)
使用 DesignerSerializerAttribute將自定義CodeDomSerializer實作與元件類型產生關聯。
若要實作串行化方法,以產生元件的組態程序代碼:
在衍生自 CodeDomSerializer的類別內,覆寫基類的適當串行化或還原串行化方法。
如果您想要讓預設串行化程式產生執行預設元件元件組態的程式代碼語句,您必須取得並呼叫元件的基底串行化程式。 若要取得元件的基底串行化程式,請呼叫 GetSerializer 傳遞至方法覆寫的 IDesignerSerializationManager 方法。 GetSerializer傳遞元件的型別以串行化 組態的方法,以及您所要求的串行化程式基底類型,也就是 CodeDomSerializer。 使用 IDesignerSerializationManager 傳遞至方法覆寫的 和 物件,呼叫您在基底串行化程式上覆寫相同名稱的方法。 如果您要實作 Serialize 方法, Serialize 基底串行化程式的方法會傳回 物件。 此物件的類型取決於基底串行化程序的類型,這取決於您要串行化值的元件類型。 如果您要實作 SerializeEvents、 SerializeProperties或 SerializePropertiesToResources 方法,則必須建立新的 CodeStatementCollection ,以包含產生的程式代碼語句,並將它傳遞至 方法。
如果您已呼叫基底串行化程式方法,您將會有 , CodeStatementCollection 其中包含要產生以初始化元件的 語句。 否則,您應該建立 CodeStatementCollection。 您可以將代表 語句的物件新增 CodeStatement 至這個集合,以在元件組態程式代碼中產生。
傳回 , CodeStatementCollection 表示要產生以設定元件的原始程式碼。
給實施者的注意事項
當您繼承自 CodeDomSerializer 時,您必須覆寫下列成員:Deserialize(IDesignerSerializationManager, Object) 和 Serialize(IDesignerSerializationManager, Object)。
建構函式
CodeDomSerializer() |
初始化 CodeDomSerializer 類別的新執行個體。 |