defaultserializationobject.cs
来自「全功能c#编译器」· CS 代码 · 共 913 行 · 第 1/2 页
CS
913 行
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Drawing;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.ComponentModel.Design.Serialization;
using System.Reflection;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Xml;
using System.Resources;
using System.CodeDom;
using ICSharpCode.Core.Services;
using ICSharpCode.SharpDevelop.FormDesigner.Hosts;
using ICSharpCode.SharpDevelop.FormDesigner;
namespace ICSharpCode.SharpDevelop.FormDesigner.Services
{
internal class DefaultSerializationObjectCodeDomSerializer : CodeDomSerializer
{
public DefaultSerializationObjectCodeDomSerializer()
{
}
public override object Serialize(IDesignerSerializationManager manager, object value)
{
return new CodeStatementCollection();
}
public override object Deserialize(IDesignerSerializationManager manager, object codeObject)
{
return null;
}
}
/// <summary>
/// This class is responsible for serializing and deserializing of the object that use
/// DesignerSerializationService. It holds the information that could be divided into
/// 4 parts:
/// 1) CODE of all components
/// 2) RESOURCES (such as images, and so on)
/// 3) DESIGN ONLY Properties tha are not serialized by main serializer
/// 4) OBJECTS that are not components and are serializable
///
/// This object itself is a component that can be serialized, but it serializes only
/// empty statement collection
/// </summary>
[Serializable]
[DesignerSerializer(typeof(DefaultSerializationObjectCodeDomSerializer),typeof(CodeDomSerializer))]
public class DefaultSerializationObject : Component,
IDesignerSerializationManager,
System.ComponentModel.Design.IResourceService,
ISerializable,
IContainer
{
private IDesignerSerializationManager manager_;
private CodeDomSerializer rootSerializer_;
#region String Identifiers
/// <summary>
/// these are strings used to identify the parts of the object
/// when serialized
/// </summary>
private string serializedCode_ = "Code";
private string serializedResources_ = "Resources";
private string serializedDesignOnlyProperties_ = "DesignOnlyProperties";
private string serializedObjects_ = "Objects";
#endregion
#region SerializedInformation Holders
private object code_;
private MyResourceManager resourceManager_;
private Hashtable designOnlyProperties_;
private object[] objects_;
#endregion
private Hashtable componentsAdded_;
private IComponentChangeService componentChangeService_;
private ArrayList containerComponents_;
private ArrayList designerSerializationProviders_;
private Hashtable namedObjects_;
private Hashtable objectNames_;
#region MyResourceManager
private class MyResourceManager : ComponentResourceManager, IResourceWriter, IResourceReader
{
#region MyResourceSet
private class MyResourceSet : ResourceSet
{
public MyResourceSet(Hashtable data)
{
base.Table = data;
}
}
#endregion
private Hashtable data_;
internal IDictionary Data
{
get
{
if (data_ == null)
data_ = new Hashtable();
return data_;
}
}
public MyResourceManager(Hashtable data)
{
data_ = data;
}
public MyResourceManager()
{
}
#region IResourceWriter
public void AddResource(string name, byte[] value)
{
Data[name] = value;
}
public void AddResource(string name, object value)
{
Data[name] = value;
}
public void AddResource(string name, string value)
{
Data[name] = value;
}
public void Close()
{
}
public void Generate()
{
}
#endregion
#region IResourceReader
public IDictionaryEnumerator GetEnumerator()
{
return Data.GetEnumerator();
}
#endregion
#region ResourceSet
public void Dispose()
{
Data.Clear();
}
public override object GetObject(string name)
{
return Data[name];
}
public override string GetString(string name)
{
return (Data[name] as string);
}
public override ResourceSet GetResourceSet(CultureInfo culture, bool createIfNotExists, bool tryParents)
{
return new MyResourceSet(data_);
}
#endregion
IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
}
#endregion
#region MyResourceManagerCodeDomSerializer
private class MyResourceManagerCodeDomSerializer : CodeDomSerializer
{
private CodeDomSerializer serializer_;
private DefaultSerializationObject owner_;
public MyResourceManagerCodeDomSerializer(DefaultSerializationObject owner, CodeDomSerializer serializer)
{
owner_ = owner;
serializer_ = serializer;
}
public override object Serialize(IDesignerSerializationManager manager, object value)
{
return serializer_.Serialize(manager, value);
}
public override object Deserialize(IDesignerSerializationManager manager, object codeObject)
{
object resourceManager = null;
foreach (CodeStatement statement in ((CodeStatementCollection) codeObject))
{
CodeVariableDeclarationStatement varStatement = statement as CodeVariableDeclarationStatement;
if (varStatement != null)
{
resourceManager = owner_.resourceManager_;
manager.SetName(resourceManager, varStatement.Name);
codeObject = new CodeStatementCollection(((CodeStatementCollection) codeObject));
((CodeStatementCollection) codeObject).Remove(statement);
}
}
serializer_.Deserialize(manager, codeObject);
return resourceManager;
}
}
#endregion
#region MySite
private class MySite : ISite, ITypeDescriptorFilterService
{
private ITypeDescriptorFilterService filter_;
public MySite(DefaultSerializationObject container, IComponent component, string name)
{
container_ = container;
component_ = component;
name_ = name;
}
public object GetService(Type type)
{
if (type == typeof(ITypeDescriptorFilterService))
{
if (filter_ == null)
{
filter_ = ((ITypeDescriptorFilterService) container_.GetService(type));
if (filter_ == null)
filter_ = this;
}
return this;
}
return container_.GetService(type);
}
bool System.ComponentModel.Design.ITypeDescriptorFilterService.FilterAttributes(IComponent component, IDictionary attributes)
{
if (filter_ != this)
{
return filter_.FilterAttributes(component, attributes);
}
return true;
}
bool System.ComponentModel.Design.ITypeDescriptorFilterService.FilterEvents(IComponent component, IDictionary events)
{
if (filter_ != this)
{
return filter_.FilterEvents(component, events);
}
return true;
}
bool System.ComponentModel.Design.ITypeDescriptorFilterService.FilterProperties(IComponent component, IDictionary properties)
{
ArrayList readOnlyDescriptors = null;
Attribute[] attributes;
if (filter_ != this)
{
filter_.FilterProperties(component, properties);
}
foreach (DictionaryEntry property in properties)
{
PropertyDescriptor descriptor = ((PropertyDescriptor) property.Value);
if (descriptor.IsReadOnly)
{
if (readOnlyDescriptors == null)
readOnlyDescriptors = new ArrayList();
attributes = new Attribute[1] {ReadOnlyAttribute.No};
readOnlyDescriptors.Add(TypeDescriptor.CreateProperty(descriptor.ComponentType, descriptor, attributes));
}
}
if (readOnlyDescriptors != null)
{
foreach (PropertyDescriptor readOnlyDescriptor in readOnlyDescriptors)
{
properties[readOnlyDescriptor.Name] = readOnlyDescriptor;
}
}
return false;
}
#region ISite
#region Component
private IComponent component_;
public IComponent Component
{
get { return component_; }
}
#endregion
#region Container
private DefaultSerializationObject container_;
public IContainer Container
{
get { return container_; }
}
#endregion
#region DesignMode
public bool DesignMode
{
get
{
return true;
}
}
#endregion
#region Name
private string name_;
public string Name
{
get { return name_; }
set { name_ = value; }
}
#endregion
#endregion
}
#endregion
public DefaultSerializationObject(IDesignerSerializationManager manager, CodeDomSerializer rootSerializer, ICollection objectCollection)
{
manager_ = manager;
rootSerializer_ = rootSerializer;
code_ = null;
objects_ = new object[objectCollection.Count];
objectCollection.CopyTo(objects_, 0);
}
public DefaultSerializationObject(SerializationInfo info, StreamingContext context)
{
code_ = info.GetValue(serializedCode_, typeof(object));
ArrayList serializedObjects = ((ArrayList) info.GetValue(serializedObjects_, typeof(ArrayList)));
objects_ = serializedObjects.ToArray();
Hashtable serializedResources = ((Hashtable) info.GetValue(serializedResources_, typeof(Hashtable)));
if (serializedResources != null)
resourceManager_ = new MyResourceManager(serializedResources);
designOnlyProperties_ = ((Hashtable) info.GetValue(serializedDesignOnlyProperties_, typeof(Hashtable)));
}
protected override void Dispose(bool disposing)
{
if (componentChangeService_ != null)
{
componentChangeService_.ComponentAdded -= new ComponentEventHandler(OnComponentAdded);
componentChangeService_ = null;
}
if (disposing)
{
objects_ = null;
}
}
private ArrayList FinalizeSerialization(bool serialize)
{
if (SerializationComplete != null) {
try {
SerializationComplete(this, EventArgs.Empty);
} catch (Exception e) {
Console.WriteLine(e);
}
}
// clear all callbacks
ResolveName = null;
SerializationComplete = null;
// clear all serialization things
manager_ = null;
designerSerializationProviders_ = null;
contextStack_ = null;
// clear all hashtables
namedObjects_ = null;
objectNames_ = null;
if (serialize == false)
{
((IContainer)this).Remove(this);
}
ArrayList comps = containerComponents_;
containerComponents_ = null;
return comps;
}
#region Deserialize
public ICollection Deserialize(IDesignerSerializationManager manager, CodeDomSerializer rootSerializer)
{
manager_ = manager;
ArrayList deserializedObjects = null;
object[] arrayOfObjects;
if ((objects_ != null) && (code_ == null)) {
return objects_;
}
IDesignerHost host = ((IDesignerHost) manager.GetService(typeof(IDesignerHost)));
if (host != null) {
componentChangeService_ = ((IComponentChangeService) host.GetService(typeof(IComponentChangeService)));
if (componentChangeService_ != null) {
host.RemoveService(typeof(IComponentChangeService));
host.AddService(typeof(IComponentChangeService), new ComponentChangeService());
}
}
try {
rootSerializer.Deserialize(this, code_);
} finally {
if (componentChangeService_ != null) {
host.RemoveService(typeof(IComponentChangeService));
host.AddService(typeof(IComponentChangeService), componentChangeService_);
}
deserializedObjects = FinalizeSerialization(false);
}
if (deserializedObjects != null) {
if ((host != null) && deserializedObjects.Contains(host.RootComponent)) {
deserializedObjects.Remove(host.RootComponent);
}
arrayOfObjects = new object[objects_.Length + deserializedObjects.Count];
objects_.CopyTo(arrayOfObjects, 0);
deserializedObjects.CopyTo(arrayOfObjects, objects_.Length);
objects_ = arrayOfObjects;
if (host != null && componentChangeService_ != null) {
componentChangeService_.ComponentAdded += new ComponentEventHandler(OnComponentAdded);
componentsAdded_ = new Hashtable();
NameCreationService ncs = (NameCreationService)host.GetService(typeof(INameCreationService));
foreach (IComponent component in deserializedObjects) {
componentsAdded_.Add(component.Site.Name, component);
// System.Windows.Forms.Control control = component as System.Windows.Forms.Control;
// if (control != null && control.Parent == null) {
// component.Site = null;
// }
}
}
}
return objects_;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?