defaultserializationobject.cs

来自「全功能c#编译器」· CS 代码 · 共 913 行 · 第 1/2 页

CS
913
字号
		} 

		private void OnComponentAdded(object sender, ComponentEventArgs ce)
		{ 
			string componentName = null;
			if ((componentsAdded_ == null) || !componentsAdded_.ContainsValue(ce.Component)) {
				return; 
			}
			Attribute[] designOnlyAttributes = new Attribute[1] { DesignOnlyAttribute.Yes };
			foreach (DictionaryEntry componentEntry in componentsAdded_) {
				if (componentEntry.Value == ce.Component) {
					string oldName = ce.Component.Site.Name;
					
					PropertyDescriptorCollection designOnlyPropCollection = TypeDescriptor.GetProperties(ce.Component, designOnlyAttributes);
					componentName = (string)componentEntry.Key;
				
					foreach(DictionaryEntry designOnlyEntry in designOnlyProperties_) {
						string designOnlyPropertyName = ((string) designOnlyEntry.Key);
		
						int dotIndex = designOnlyPropertyName.IndexOf('.');
							
						if ((dotIndex != -1) && (designOnlyPropertyName.Substring(0, dotIndex) == componentName)) {
							string propertyName = designOnlyPropertyName.Substring((dotIndex + 1));
							
							if (designOnlyPropertyName == ".Name") {
								string newName = designOnlyEntry.Value.ToString();
								// check for duplicate/invalid name using name creation service.
								NameCreationService ncs = (NameCreationService)ce.Component.Site.GetService(typeof(INameCreationService));
								
								if (ncs.IsValidName(newName)) {
									ce.Component.Site.Name = newName;
									((ComponentChangeService)this.componentChangeService_).OnComponentRename(ce.Component, oldName, newName);
								}
								continue;
							}
							
							PropertyDescriptor propDescriptor = designOnlyPropCollection[propertyName];
							if (propDescriptor != null) {
								
								try {
									propDescriptor.SetValue(ce.Component, designOnlyEntry.Value); 
								} catch (Exception e) { 
									Console.WriteLine(e);
								} 
							}
						}
					}
				} 
			}
			if (componentName != null)
			  componentsAdded_.Remove(componentName);
			if ((componentsAdded_.Count == 0) && (componentChangeService_ != null))
			{
				componentChangeService_.ComponentAdded -= new ComponentEventHandler(OnComponentAdded);
				componentChangeService_ = null;
				componentsAdded_ = null;
			}
		}
		#endregion

		#region Serialize
		private Hashtable SerializeDesignOnlyProperties(IDesignerSerializationManager manager, ICollection objectList)
		{
			Hashtable arrayOfDesignTimeProperties = new Hashtable();
			Attribute[] designOnlyAttribute = new Attribute[1] { DesignOnlyAttribute.Yes };
			foreach(object myObject in objectList) {
				PropertyDescriptorCollection designOnlyProperties = TypeDescriptor.GetProperties(myObject, designOnlyAttribute);
				string objectName = manager.GetName(myObject);
				foreach(PropertyDescriptor propDescriptor in designOnlyProperties) {
					string designOnlyPropertyName = objectName + "." + propDescriptor.Name;
					object designOnlyPropertyValue = propDescriptor.GetValue(myObject);
					
					if (propDescriptor.ShouldSerializeValue(myObject) && ((designOnlyPropertyValue == null) || designOnlyPropertyValue.GetType().IsSerializable)) {
						arrayOfDesignTimeProperties[designOnlyPropertyName] = designOnlyPropertyValue;
						//Console.WriteLine("Serialize : " + designOnlyPropertyName + " == " + designOnlyPropertyValue);
					}
				}
			}
			return arrayOfDesignTimeProperties;
		}
		#endregion

		#region IResourceService
		IResourceReader System.ComponentModel.Design.IResourceService.GetResourceReader(CultureInfo info)
		{ 
			if (resourceManager_ == null)
			{
				resourceManager_ = new MyResourceManager();
			}
			return resourceManager_; 
		}

		IResourceWriter System.ComponentModel.Design.IResourceService.GetResourceWriter(CultureInfo info)
		{ 
			if (resourceManager_ == null)
			{
				resourceManager_ = new MyResourceManager();
			}
			return resourceManager_; 
		}
		#endregion

		#region IDesignerSerializationManager
		public event ResolveNameEventHandler ResolveName;
		public event EventHandler SerializationComplete;

		void System.ComponentModel.Design.Serialization.IDesignerSerializationManager.AddSerializationProvider(IDesignerSerializationProvider provider)
		{ 
			if (designerSerializationProviders_ == null)
			{
				designerSerializationProviders_ = new ArrayList();
			}
			designerSerializationProviders_.Add(provider);
		}

		object System.ComponentModel.Design.Serialization.IDesignerSerializationManager.CreateInstance(Type type, ICollection arguments, string name, bool addToContainer)
		{ 
			object        myObject = null;
			IDesignerHost host = ((IDesignerHost) manager_.GetService(typeof(IDesignerHost)));
			object[]      argumentArray = null;

			if (type == base.GetType())
			{
				myObject = host.RootComponent;
			}
			if (typeof(ResourceManager).IsAssignableFrom(type))
			{
				myObject = resourceManager_;
			}
			if (myObject == null)
			{
				if (arguments != null && arguments.Count > 0)
				{
					argumentArray = new object[arguments.Count];
					arguments.CopyTo(argumentArray, 0); 
					myObject = Activator.CreateInstance(type, argumentArray); 
				}
				else
				  myObject = Activator.CreateInstance(type); 
			}
			if (name != null)
			{
				if (namedObjects_ == null)
				{
					namedObjects_ = new Hashtable();
					objectNames_ = new Hashtable();
				}
				namedObjects_[name] = myObject;
				objectNames_[myObject] = name;
				IComponent myComponent = myObject as IComponent;
				if (addToContainer && myComponent != null)
				{
//					if (myComponent.Site == null)
//						ISite site = new MySite(this, myComponent, name);
					((IContainer)this).Add(myComponent, name);
				}
			}
			return myObject; 
		}

		private ContextStack contextStack_; 
		ContextStack System.ComponentModel.Design.Serialization.IDesignerSerializationManager.Context
		{ 
			get
			{
				if (contextStack_ == null)
				{
					contextStack_ = new ContextStack();
				}
				return contextStack_;
			}
		}

		PropertyDescriptorCollection System.ComponentModel.Design.Serialization.IDesignerSerializationManager.Properties
		{ 
			get
			{
				return TypeDescriptor.GetProperties(this); 
			}
		}

		object System.ComponentModel.Design.Serialization.IDesignerSerializationManager.GetInstance(string name)
		{ 
			object myObject = null;
			if (name == null)
			{
				throw new ArgumentNullException("name"); 
			}
			if (namedObjects_ != null)
			{
				myObject = namedObjects_[name];
			}
			if (myObject == null && name.Equals("components"))
			{
				myObject = this;
			}
			if ((myObject == null) && (ResolveName != null))
			{
				ResolveNameEventArgs args = new ResolveNameEventArgs(name);
				ResolveName(this, args);
				myObject = args.Value;
			}
			if ((myObject == null) && (manager_ != null))
			{
				myObject = manager_.GetInstance(name);
			}
			if (myObject == null)
			{
				IReferenceService referenceService = ((IReferenceService) base.GetService(typeof(IReferenceService)));
				if (referenceService != null)
				{
					myObject = referenceService.GetReference(name); 
				}
			}
			return myObject; 
		}

		string System.ComponentModel.Design.Serialization.IDesignerSerializationManager.GetName(object value)
		{ 
			string name = null;
			if (value == null)
			{
				throw new ArgumentNullException("value");
			}
			if (objectNames_ != null)
			{
				name = (string) objectNames_[value]; 
			}
			if (name == null && (value as IComponent) != null && ((IComponent) value).Site != null)
			{
				name = ((IComponent) value).Site.Name;
			}
			return name; 
		}

		object System.ComponentModel.Design.Serialization.IDesignerSerializationManager.GetSerializer(Type objectType, Type serializerType)
		{ 
			object serializer = manager_.GetSerializer(objectType, serializerType);
			if ((objectType != null && serializer != null) && (typeof(ResourceManager).IsAssignableFrom(objectType) && typeof(CodeDomSerializer).IsAssignableFrom(serializerType)))
			{
				serializer = new MyResourceManagerCodeDomSerializer(this, ((CodeDomSerializer) serializer)); 
			}
			if (objectType != null && ((serializer != null && typeof(ResourceManager).IsAssignableFrom(objectType)) && typeof(CodeDomSerializer).IsAssignableFrom(serializerType)))
			{
				serializer = new MyResourceManagerCodeDomSerializer(this, ((CodeDomSerializer) serializer)); 
			}
			if (designerSerializationProviders_ != null)
			{
				foreach (IDesignerSerializationProvider provider in designerSerializationProviders_) 
				{
					object providerSerializer = provider.GetSerializer(this, serializer, objectType, serializerType);
					if (providerSerializer != null)
						serializer = providerSerializer;
				}
			}
			return serializer; 
		}

		Type System.ComponentModel.Design.Serialization.IDesignerSerializationManager.GetType(string typeName)
		{ 
			if (typeName.Equals(typeof(DefaultSerializationObject).FullName))
				return typeof(DefaultSerializationObject); 
			return manager_.GetType(typeName); 
		}

		void System.ComponentModel.Design.Serialization.IDesignerSerializationManager.RemoveSerializationProvider(IDesignerSerializationProvider provider)
		{ 
			if (designerSerializationProviders_ != null)
				designerSerializationProviders_.Remove(provider);
		}

		void System.ComponentModel.Design.Serialization.IDesignerSerializationManager.ReportError(object errorInformation)
		{  
		}

		void System.ComponentModel.Design.Serialization.IDesignerSerializationManager.SetName(object instance, string name)
		{ 
			object[] serializedNames = null;
			if (instance == null)
			{
				throw new ArgumentNullException("instance"); 
			}
			if (name == null)
			{
				throw new ArgumentNullException("name");
			}
			if (namedObjects_ == null)
			{
				namedObjects_ = new Hashtable();
				objectNames_ = new Hashtable();
			}
			if (namedObjects_[name] != null)
			{
				serializedNames = new object[1];
				serializedNames[0] = name;
				throw new ArgumentException(string.Format("SerializerNameInUse {0}", serializedNames));
			}
			if (objectNames_[instance] != null)
			{
				serializedNames = new object[2];
				serializedNames[0] = name;
				serializedNames[1] = ((string) objectNames_[instance]);
				throw new ArgumentException(string.Format("SerializerObjectHasName {0}", serializedNames));
			}
			namedObjects_[name] = instance;
			objectNames_[instance] = name;
		}
		#endregion

		#region ISerializable
		void System.Runtime.Serialization.ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
		{ 
			int        index;
			IComponent myComponent;
  
			if ((code_ == null) && (manager_ != null))
			{
				namedObjects_ = new Hashtable(objects_.Length);
				objectNames_ = new Hashtable(objects_.Length);
				for (index = 0; index < objects_.Length; index++)
				{
					myComponent = objects_[index] as IComponent;
					if (myComponent != null && myComponent.Site != null && myComponent.Site.Name != null)
					{
						((IContainer)this).Add(myComponent, myComponent.Site.Name);
						namedObjects_[myComponent.Site.Name] = myComponent;
						objectNames_[myComponent] = myComponent.Site.Name;
					}
				}
				if ((containerComponents_ != null) && (containerComponents_.Count > 0))
				{
					((IContainer)this).Add(this, ComponentName);
					try
					{
						code_ = rootSerializer_.Serialize((IDesignerSerializationManager)this, this);
						designOnlyProperties_ = SerializeDesignOnlyProperties(manager_, objects_); 
					}
					finally
					{
						FinalizeSerialization(true);
					}
				}
				manager_ = null;
				rootSerializer_ = null;
			}
			ArrayList  nonComponentArray = new ArrayList();
			if (code_ != null)
			{
				for (index = 0; index < objects_.Length; index++)
				{
					myComponent = objects_[index] as IComponent;
					if (myComponent == null)
					{
						nonComponentArray.Add(objects_[index]); 
					}
				}
			}
			info.AddValue(serializedObjects_,              nonComponentArray);
			info.AddValue(serializedCode_,                 code_);
			info.AddValue(serializedResources_,            ((resourceManager_ != null) ? resourceManager_.Data : null));
			info.AddValue(serializedDesignOnlyProperties_, designOnlyProperties_); 
		}
		#endregion

		#region IContainer
		void System.ComponentModel.IContainer.Add(IComponent component)
		{ 
			((IContainer)this).Add(component, null);
		}

		void System.ComponentModel.IContainer.Add(IComponent component, string name)
		{ 
			if (name == null) {
				return; 
			}
			if (containerComponents_ == null) {
				containerComponents_ = new ArrayList();
				containerComponents_.Add(component);
			} else {
				bool found = false;
				foreach(object myObject in containerComponents_) {
					if (myObject == component) {
						found = true;
						break; 
					} 
				}
				if (found == false) {
					containerComponents_.Add(component);
				}
			}
 
			if (component.Site == null) {
				component.Site = new MySite(this, component, name);
			}
		}

		ComponentCollection System.ComponentModel.IContainer.Components { 
			get {
				IComponent[] components;
				if (containerComponents_ == null) {
					components = new IComponent[0]; 
				} else { 
					components = new IComponent[containerComponents_.Count];
					containerComponents_.CopyTo(components, 0);
				}
				return new ComponentCollection(components); 
			}
		}

		void System.ComponentModel.IContainer.Remove(IComponent component)
		{ 
			if (component.Site is MySite) {
				component.Site = null; 
			}
			if (containerComponents_ != null) {
				if (containerComponents_.Contains(component)) {
					containerComponents_.Remove(component); 
				}
			}
		}
		#endregion

		#region IServiceProvider
		object System.IServiceProvider.GetService(Type serviceType)
		{ 
			if (serviceType == typeof(System.ComponentModel.Design.IResourceService)) {
				return this; 
			}
			if (manager_ != null) {
				return manager_.GetService(serviceType); 
			}
			return null; 
		}
		#endregion

		#region Component
		protected override object GetService(Type t)
		{
			return ((IServiceProvider)this).GetService(t);
		}
		#endregion

		/// <summary>
		/// This is component name used for identifivcation of my object in the container
		/// Hopefully, this name is unique enough not to be really used
		/// </summary>
		private string ComponentName 
		{ 
			get
			{ 
				return "sdSerializationComponentSD";  
			} 
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?