コード例 #1
0
        /// <summary>
        ///  Determines if we can cache the results of serializing a component.
        /// </summary>
        private bool CanCacheComponent(IDesignerSerializationManager manager, object value, PropertyDescriptorCollection props)
        {
            if (value is IComponent comp)
            {
                if (comp.Site != null)
                {
                    if (comp.Site is INestedSite nestedSite && !string.IsNullOrEmpty(nestedSite.FullName))
                    {
                        return(false);
                    }
                }

                if (props is null)
                {
                    props = TypeDescriptor.GetProperties(comp);
                }

                foreach (PropertyDescriptor property in props)
                {
                    if (typeof(IComponent).IsAssignableFrom(property.PropertyType) &&
                        !property.Attributes.Contains(DesignerSerializationVisibilityAttribute.Hidden))
                    {
                        MemberCodeDomSerializer memberSerializer = (MemberCodeDomSerializer)manager.GetSerializer(property.GetType(), typeof(MemberCodeDomSerializer));

                        if (memberSerializer != null && memberSerializer.ShouldSerialize(manager, value, property))
                        {
                            return(false);
                        }
                    }
                }
            }

            return(true);
        }
        private bool CanCacheComponent(IDesignerSerializationManager manager, object value, PropertyDescriptorCollection props)
        {
            IComponent component = value as IComponent;

            if (component != null)
            {
                if (component.Site != null)
                {
                    INestedSite site = component.Site as INestedSite;
                    if ((site != null) && !string.IsNullOrEmpty(site.FullName))
                    {
                        return(false);
                    }
                }
                if (props == null)
                {
                    props = TypeDescriptor.GetProperties(component);
                }
                foreach (PropertyDescriptor descriptor in props)
                {
                    if (typeof(IComponent).IsAssignableFrom(descriptor.PropertyType) && !descriptor.Attributes.Contains(DesignerSerializationVisibilityAttribute.Hidden))
                    {
                        MemberCodeDomSerializer serializer = (MemberCodeDomSerializer)manager.GetSerializer(descriptor.GetType(), typeof(MemberCodeDomSerializer));
                        if ((serializer != null) && serializer.ShouldSerialize(manager, value, descriptor))
                        {
                            return(false);
                        }
                    }
                }
            }
            return(true);
        }
コード例 #3
0
        /// <summary>
        ///  This method returns true if the given member descriptor should be serialized,
        ///  or false if there is no need to serialize the member.
        /// </summary>
        public override bool ShouldSerialize(IDesignerSerializationManager manager, object value, MemberDescriptor descriptor)
        {
            bool shouldSerialize = _serializer.ShouldSerialize(manager, value, descriptor);

            if (!shouldSerialize && !descriptor.Attributes.Contains(DesignOnlyAttribute.Yes))
            {
                switch (_model)
                {
                case CodeDomLocalizationModel.PropertyReflection:
                    if (!shouldSerialize)
                    {
                        // hook up the event the first time to clear out our cache at the end of the serialization
                        if (localizationLanguage is null)
                        {
                            manager.SerializationComplete += new EventHandler(OnSerializationComplete);
                        }

                        if (GetLocalizationLanguage(manager) != CultureInfo.InvariantCulture)
                        {
                            shouldSerialize = true;
                        }
                    }

                    break;

                case CodeDomLocalizationModel.PropertyAssignment:
                    // If this property contains its default value, we still want to serialize it if we are in
                    // localization mode if we are writing to the default culture, but only if the object
                    // is not inherited.
                    InheritanceAttribute inheritance = (InheritanceAttribute)manager.Context[typeof(InheritanceAttribute)];

                    if (inheritance is null)
                    {
                        inheritance = (InheritanceAttribute)TypeDescriptor.GetAttributes(value)[typeof(InheritanceAttribute)];
                        if (inheritance is null)
                        {
                            inheritance = InheritanceAttribute.NotInherited;
                        }
                    }

                    if (inheritance.InheritanceLevel != InheritanceLevel.InheritedReadOnly)
                    {
                        shouldSerialize = true;
                    }

                    break;

                default:
                    break;
                }
            }

            return(shouldSerialize);
        }