Exemplo n.º 1
0
        public static void SetBaseTypeName(string typeName, IServiceProvider serviceProvider)
        {
            if (string.IsNullOrEmpty(typeName))
            {
                throw new ArgumentNullException("typeName");
            }

            IDesignerHost host = serviceProvider.GetService(typeof(IDesignerHost)) as IDesignerHost;

            if (host == null)
            {
                throw new InvalidOperationException(SR.GetString(SR.General_MissingService, typeof(IDesignerHost).FullName));
            }

            IMemberCreationService memberCreationService = serviceProvider.GetService(typeof(IMemberCreationService)) as IMemberCreationService;

            if (memberCreationService == null)
            {
                throw new InvalidOperationException(SR.GetString("General_MissingService", typeof(IMemberCreationService).FullName));
            }

            // Validate the base type (this will throw an exception if the type name isn't valid
            Type newBaseType = ValidateBaseType(typeName, serviceProvider);

            //Warn the user of the change
            Type oldBaseType = host.RootComponent.GetType();

            if (oldBaseType == newBaseType)
            {
                return;
            }

            // If we're switch to a base type that is not derived from CompositeActivity, make sure
            // we dont's support events or exceptions
            if (!TypeProvider.IsAssignable(typeof(CompositeActivity), newBaseType))
            {
                PropertyDescriptor supportsEventsPropDesc = TypeDescriptor.GetProperties(host.RootComponent)["SupportsEvents"];
                if (supportsEventsPropDesc != null && ((bool)supportsEventsPropDesc.GetValue(host.RootComponent)) == true)
                {
                    supportsEventsPropDesc.SetValue(host.RootComponent, false);
                }

                PropertyDescriptor supportsExceptionsPropDesc = TypeDescriptor.GetProperties(host.RootComponent)["SupportsExceptions"];
                if (supportsExceptionsPropDesc != null && ((bool)supportsExceptionsPropDesc.GetValue(host.RootComponent)) == true)
                {
                    supportsExceptionsPropDesc.SetValue(host.RootComponent, false);
                }
            }

            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(host.RootComponent);

            if (host.RootComponent is CompositeActivity && ((CompositeActivity)host.RootComponent).Activities.Count > 0)
            {
                // Warn user first if there are any children that can not be re-parented to the new root.
                IUIService uiService = serviceProvider.GetService(typeof(IUIService)) as IUIService;
                if (uiService != null)
                {
                    if (DialogResult.OK != uiService.ShowMessage(SR.GetString(SR.NoChildActivities_Message),
                                                                 SR.GetString(SR.NoChildActivities_Caption), MessageBoxButtons.OKCancel))
                    {
                        return;
                    }
                }

                // Remove the children first. This would cause the component removed event to be fired,
                // thus remove the generated field from the designer.cs file.
                List <Activity>           activitiesToRemove = new List <Activity>(((CompositeActivity)host.RootComponent).Activities);
                CompositeActivityDesigner rootDesigner       = host.GetDesigner(host.RootComponent) as CompositeActivityDesigner;
                if (rootDesigner != null)
                {
                    rootDesigner.RemoveActivities(activitiesToRemove.AsReadOnly());
                }
            }

            //Also, clear all properties of original base. That will allow undo to set old values back.
            foreach (PropertyDescriptor propertyDescriptor in properties)
            {
                if (!propertyDescriptor.Name.Equals("BaseActivityType", StringComparison.Ordinal) &&
                    !propertyDescriptor.Name.Equals("Name", StringComparison.Ordinal) &&
                    propertyDescriptor.CanResetValue(host.RootComponent))
                {
                    propertyDescriptor.ResetValue(host.RootComponent);
                }
            }

            PropertyDescriptor realBaseActivityTypePropertyDescriptor = properties["BaseActivityType"];
            PropertyDescriptor baseActivityTypePropertyDescriptor     = TypeDescriptor.CreateProperty(realBaseActivityTypePropertyDescriptor.ComponentType, realBaseActivityTypePropertyDescriptor, DesignerSerializationVisibilityAttribute.Visible);

            IComponentChangeService changeService = serviceProvider.GetService(typeof(IComponentChangeService)) as IComponentChangeService;

            if (changeService != null)
            {
                changeService.OnComponentChanging(host.RootComponent, baseActivityTypePropertyDescriptor);
            }

            ((Activity)host.RootComponent).UserData[UserDataKeys.NewBaseType] = newBaseType;

            memberCreationService.UpdateBaseType(host.RootComponentClassName, newBaseType);

            if (changeService != null)
            {
                changeService.OnComponentChanged(host.RootComponent, baseActivityTypePropertyDescriptor, baseActivityTypePropertyDescriptor.GetValue(host.RootComponent), typeName);
            }

            //Work around: Force update of the host by raising idle.This is to ensure undo events work on updated host.
            Application.RaiseIdle(new EventArgs());
        }
        public static void SetBaseTypeName(string typeName, IServiceProvider serviceProvider)
        {
            if (string.IsNullOrEmpty(typeName))
            {
                throw new ArgumentNullException("typeName");
            }
            IDesignerHost host = serviceProvider.GetService(typeof(IDesignerHost)) as IDesignerHost;

            if (host == null)
            {
                throw new InvalidOperationException(SR.GetString("General_MissingService", new object[] { typeof(IDesignerHost).FullName }));
            }
            IMemberCreationService service = serviceProvider.GetService(typeof(IMemberCreationService)) as IMemberCreationService;

            if (service == null)
            {
                throw new InvalidOperationException(SR.GetString("General_MissingService", new object[] { typeof(IMemberCreationService).FullName }));
            }
            System.Type fromType = ValidateBaseType(typeName, serviceProvider);
            if (host.RootComponent.GetType() != fromType)
            {
                if (!TypeProvider.IsAssignable(typeof(CompositeActivity), fromType))
                {
                    PropertyDescriptor descriptor = TypeDescriptor.GetProperties(host.RootComponent)["SupportsEvents"];
                    if ((descriptor != null) && ((bool)descriptor.GetValue(host.RootComponent)))
                    {
                        descriptor.SetValue(host.RootComponent, false);
                    }
                    PropertyDescriptor descriptor2 = TypeDescriptor.GetProperties(host.RootComponent)["SupportsExceptions"];
                    if ((descriptor2 != null) && ((bool)descriptor2.GetValue(host.RootComponent)))
                    {
                        descriptor2.SetValue(host.RootComponent, false);
                    }
                }
                PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(host.RootComponent);
                if ((host.RootComponent is CompositeActivity) && (((CompositeActivity)host.RootComponent).Activities.Count > 0))
                {
                    IUIService service2 = serviceProvider.GetService(typeof(IUIService)) as IUIService;
                    if ((service2 != null) && (DialogResult.OK != service2.ShowMessage(SR.GetString("NoChildActivities_Message"), SR.GetString("NoChildActivities_Caption"), MessageBoxButtons.OKCancel)))
                    {
                        return;
                    }
                    List <Activity>           list     = new List <Activity>(((CompositeActivity)host.RootComponent).Activities);
                    CompositeActivityDesigner designer = host.GetDesigner(host.RootComponent) as CompositeActivityDesigner;
                    if (designer != null)
                    {
                        designer.RemoveActivities(list.AsReadOnly());
                    }
                }
                foreach (PropertyDescriptor descriptor3 in properties)
                {
                    if ((!descriptor3.Name.Equals("BaseActivityType", StringComparison.Ordinal) && !descriptor3.Name.Equals("Name", StringComparison.Ordinal)) && descriptor3.CanResetValue(host.RootComponent))
                    {
                        descriptor3.ResetValue(host.RootComponent);
                    }
                }
                PropertyDescriptor      oldPropertyDescriptor = properties["BaseActivityType"];
                PropertyDescriptor      member   = TypeDescriptor.CreateProperty(oldPropertyDescriptor.ComponentType, oldPropertyDescriptor, new Attribute[] { DesignerSerializationVisibilityAttribute.Visible });
                IComponentChangeService service3 = serviceProvider.GetService(typeof(IComponentChangeService)) as IComponentChangeService;
                if (service3 != null)
                {
                    service3.OnComponentChanging(host.RootComponent, member);
                }
                ((Activity)host.RootComponent).UserData[UserDataKeys.NewBaseType] = fromType;
                service.UpdateBaseType(host.RootComponentClassName, fromType);
                if (service3 != null)
                {
                    service3.OnComponentChanged(host.RootComponent, member, member.GetValue(host.RootComponent), typeName);
                }
                Application.RaiseIdle(new EventArgs());
            }
        }