codedomdesignerserializationmanager.cs

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

CS
490
字号
			}

			Type serializerType = typeof(CodeDomSerializer);
			AttributeCollection attributes = TypeDescriptor.GetAttributes(objectType);
			foreach (Attribute att in attributes) {
				RootDesignerSerializerAttribute serAtt = att as RootDesignerSerializerAttribute;

				if (serAtt != null) {
					if (serAtt.SerializerBaseTypeName.StartsWith(serializerType.FullName)) {
						Type type = GetType(serAtt.SerializerTypeName);
						return LicenseManager.CreateWithContext(type, new DesigntimeLicenseContext()) as CodeDomSerializer;
					}
				}
			}
			return GetRootSerializer(objectType.BaseType);
		}

		/// <summary>
		/// TODO - add method description
		/// </summary>
		/// <remarks>
		/// Interface method from IDesignerSerializationManager
		///
		/// </remarks>
		/// <param name='value'>TODO - add parameter description</param>
		public virtual string GetName(object value)
		{
			//System.Console.WriteLine("Looking for name of {0}",value);
			object name = objectToName[value];
			return (string)name;
		}

		/// <summary>
		/// TODO - add method description
		/// </summary>
		/// <remarks>
		/// Interface method from IDesignerSerializationManager
		///
		/// </remarks>
		/// <param name='name'>TODO - add parameter description</param>
		public virtual object GetInstance(string name)
		{
			object o = nameToObject[name];
			if (o != null) {
				return o;
			}
			o = OnResolveName(name);
			if (o != null){
				nameToObject[name] = o;
			}
			return o;
		}

		/// <summary>
		/// TODO - add method description
		/// </summary>
		/// <remarks>
		/// Interface method from IDesignerSerializationManager
		///
		/// </remarks>
		/// <param name='type'>TODO - add parameter description</param>
		/// <param name='arguments'>TODO - add parameter description</param>
		/// <param name='name'>TODO - add parameter description</param>
		/// <param name='addToContainer'>TODO - add parameter description</param>
		public virtual object CreateInstance(Type type, ICollection arguments, string name, bool addToContainer)
		{
			object o = null;
//			Console.WriteLine("create instance :{0} with name {2} should add:{1}", type, addToContainer, name);
//			Console.WriteLine(Environment.StackTrace);
//			Console.ReadLine();
//			Console.WriteLine(host.Container.Components.Count);
			
			if (arguments == null) {
				o = LicenseManager.CreateWithContext(type, new DesigntimeLicenseContext());
				//o = Activator.CreateInstance(type);
			} else {
				
				object[] args = new object[arguments.Count];
				arguments.CopyTo(args,0);
				// fix for misbehaviour:
				// some components DO add themselve to the container
				// this is my way to listen to this event
				int oldComponentCount = host.Container.Components.Count;
				try {
					o = Activator.CreateInstance(type, args);
				} catch (MissingMethodException) {
					throw;
				} catch (Exception ex) {
					IMessageService messageService =(IMessageService)ServiceManager.Services.GetService(typeof(IMessageService));
					messageService.ShowError(ex, "Exception while creating the component for the Forms Designer (a component has thrown an exception in the constructor).\nTHIS DOES NOT AFFECT THE SOURCE CODE.");
				}
				if (oldComponentCount != host.Container.Components.Count) {
					IComponent c = o as IComponent;
					
					if (name != null) {
						// overwrite default name
						ComponentChangeService changeService = host.GetService(typeof(IComponentChangeService)) as ComponentChangeService;
						changeService.OnComponentRename(c, c.Site.Name, name);
						SetName(o, name);
					} else {
						// when they added themselves they got already a site
						// now we need to recognice their name
						objectToName[o]           = c.Site.Name;
						nameToObject[c.Site.Name] = o;
					}
					return o;
				}
			}
			
			if (o == null) {
				throw new Exception(String.Format("Can't create instance of type {0}",type));
			}
			try {
				if (name != null) {
					INameCreationService nameCreationService = (INameCreationService)GetService(typeof(INameCreationService));
					if (!nameCreationService.IsValidName(name)) {
						name = nameCreationService.CreateName(host.Container, o.GetType());
					}
					SetName(o, name);
				}
			} catch (Exception e) {
				Console.WriteLine("Got Exception : " + e);
			}
			if (addToContainer && name != null) {
				IComponent comp = o as IComponent;
				if (comp != null) {
					host.Container.Add(comp, name);
				}
			}
			return o;
		}

		/// <summary>
		/// TODO - add method description
		/// </summary>
		/// <remarks>
		/// Interface method from IDesignerSerializationManager
		///
		/// </remarks>
		/// <param name='provider'>TODO - add parameter description</param>
		public virtual void AddSerializationProvider(IDesignerSerializationProvider provider)
		{
			//System.Console.WriteLine("Adding serilization provider {0}",provider);
			providers.Add(provider);
		}

		/// <summary>
		/// TODO - add method description
		/// </summary>
		/// <remarks>
		/// Interface method from IServiceProvider
		///
		/// </remarks>
		/// <param name='serviceType'>TODO - add parameter description</param>
//		public virtual object GetService(Type serviceType)
//		{
//			object service = host.GetService(serviceType);
//			if (service == null)
//				System.Console.WriteLine("Manager service not found {0}",serviceType);
//
//			return service;
//		}

		public void Initialize()
		{
			while (context.Pop() != null) {}
			providers.Clear();
			nameToObject.Clear();
			objectToName.Clear();
			props = PropertyDescriptorCollection.Empty;
			services = new ServiceContainer(host);
			services.AddService(typeof(IDesignerSerializationManager),this);
			services.AddService(typeof(IServiceContainer),this);
		}


		protected virtual object OnResolveName(string name)
		{
			
			if (ResolveName == null) {
				return null;
			}
			ResolveNameEventArgs e = new ResolveNameEventArgs(name);
			ResolveName(this,e);
			return e.Value;
		}

		public void OnSerializationComplete()
		{
			if (SerializationComplete != null) {
				SerializationComplete(this,EventArgs.Empty);
			}
		}

#region System.IServiceProvider interface implementation
		public object GetService(System.Type serviceType)
		{
			return services.GetService(serviceType);
		}
#endregion


#region System.ComponentModel.Design.IServiceContainer interface implementation
		public void RemoveService(Type serviceType)
		{
			services.RemoveService(serviceType);
		}

		public void RemoveService(Type serviceType,bool promote)
		{
			services.RemoveService(serviceType,promote);
		}

		public void AddService(Type serviceType, object serviceInstance)
		{
			//Console.WriteLine("DesignerHost added service {0} object {1}",serviceType.Name,serviceInstance);
			services.AddService(serviceType,serviceInstance);
		}

		public void AddService(Type serviceType, ServiceCreatorCallback callback)
		{
			//Console.WriteLine("DesignerHost added callback {1} as service {0}",serviceType.Name ,callback);
			services.AddService(serviceType, callback);
		}

		public void AddService(Type serviceType, object serviceInstance,bool promote)
		{
			//Console.WriteLine("service {0} object promote added to DesignerHost",serviceType);
			services.AddService(serviceType, serviceInstance,promote);
		}

		public void AddService(Type serviceType, ServiceCreatorCallback callback, bool promote)
		{
			//Console.WriteLine("service {0} callback promote added to DesignerHost",serviceType);
			services.AddService(serviceType, callback,promote);
		}
#endregion


		public event ResolveNameEventHandler ResolveName;
		public event EventHandler SerializationComplete;

	}
}

⌨️ 快捷键说明

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