-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathidesignerserializationmanagersample.cs
More file actions
64 lines (57 loc) · 2.24 KB
/
Copy pathidesignerserializationmanagersample.cs
File metadata and controls
64 lines (57 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//<snippet1>
using System;
using System.CodeDom;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
namespace CodeDomSerializerSample
{
internal class MyCodeDomSerializer : CodeDomSerializer
{
public override object Deserialize(IDesignerSerializationManager manager, object codeObject)
{
// 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 statements)
{
// 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(commentText);
statements.Insert(0, comment);
}
return codeObject;
}
}
//<Snippet2>
[DesignerSerializer(typeof(MyCodeDomSerializer), typeof(CodeDomSerializer))]
public class MyComponent : Component
{
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public string LocalProperty { get; set; } = "Component Property Value";
}
//</Snippet2>
}
//</snippet1>
namespace CodeDomSerializerSample
{
class CodeDomSerializerStart
{
[STAThread]
static void Main()
{
}
}
}