public void Deny_Unrestricted()
        {
            PersistenceModeAttribute pma = new PersistenceModeAttribute(PersistenceMode.Attribute);

            Assert.AreEqual(PersistenceMode.Attribute, pma.Mode, "Mode");
            Assert.IsTrue(pma.Equals(pma), "Equals");
            Assert.IsTrue(pma.IsDefaultAttribute(), "IsDefaultAttribute");
        }
        private static string LocalizeControl(Control control, IServiceProvider serviceProvider, IDesignTimeResourceWriter resourceWriter, bool shouldLocalizeInnerContent)
        {
            ResourceExpressionEditor expressionEditor  = (ResourceExpressionEditor)ExpressionEditor.GetExpressionEditor("resources", serviceProvider);
            IControlBuilderAccessor  accessor          = control;
            ControlBuilder           controlBuilder    = accessor.ControlBuilder;
            ObjectPersistData        objectPersistData = controlBuilder.GetObjectPersistData();
            string resourceKey = controlBuilder.GetResourceKey();
            string b           = LocalizeObject(serviceProvider, control, objectPersistData, expressionEditor, resourceWriter, resourceKey, string.Empty, control, string.Empty, shouldLocalizeInnerContent, false, false);

            if (!string.Equals(resourceKey, b, StringComparison.OrdinalIgnoreCase))
            {
                controlBuilder.SetResourceKey(b);
            }
            if (objectPersistData != null)
            {
                foreach (PropertyEntry entry in objectPersistData.AllPropertyEntries)
                {
                    BoundPropertyEntry entry2 = entry as BoundPropertyEntry;
                    if ((entry2 != null) && !entry2.Generated)
                    {
                        string[] strArray = entry2.Name.Split(new char[] { '.' });
                        if (strArray.Length > 1)
                        {
                            object component = control;
                            foreach (string str3 in strArray)
                            {
                                PropertyDescriptor descriptor = TypeDescriptor.GetProperties(component)[str3];
                                if (descriptor == null)
                                {
                                    break;
                                }
                                PersistenceModeAttribute attribute = descriptor.Attributes[typeof(PersistenceModeAttribute)] as PersistenceModeAttribute;
                                if (attribute != PersistenceModeAttribute.Attribute)
                                {
                                    if (string.Equals(entry2.ExpressionPrefix, "resources", StringComparison.OrdinalIgnoreCase))
                                    {
                                        System.Web.Compilation.ResourceExpressionFields parsedExpressionData = entry2.ParsedExpressionData as System.Web.Compilation.ResourceExpressionFields;
                                        if ((parsedExpressionData != null) && string.IsNullOrEmpty(parsedExpressionData.ClassKey))
                                        {
                                            object obj3 = expressionEditor.EvaluateExpression(entry2.Expression, entry2.ParsedExpressionData, entry2.PropertyInfo.PropertyType, serviceProvider);
                                            if (obj3 == null)
                                            {
                                                object obj4;
                                                obj3 = ControlDesigner.GetComplexProperty(control, entry2.Name, out obj4).GetValue(obj4);
                                            }
                                            resourceWriter.AddResource(parsedExpressionData.ResourceKey, obj3);
                                        }
                                    }
                                    break;
                                }
                                component = descriptor.GetValue(component);
                            }
                        }
                    }
                }
            }
            return(b);
        }
Example #3
0
        /// <summary>
        /// Writes an attribute to an HtmlTextWriter if it needs serializing
        /// </summary>
        /// <returns>True if it does any writing</returns>
        private static bool ProcessAttribute(PropertyDescriptor prop, object o, HtmlTextWriter writer, string prefix)
        {
            //check whether we're serialising it
            if (prop.SerializationVisibility == DesignerSerializationVisibility.Hidden ||
                prop.DesignTimeOnly ||
                prop.IsReadOnly ||
                !prop.ShouldSerializeValue(o) ||
                prop.Converter == null ||
                !prop.Converter.CanConvertTo(typeof(string)))
            {
                return(false);
            }

            bool foundAttrib = false;

            //is this an attribute? If it's content, we deal with it later.
            PersistenceModeAttribute modeAttrib = prop.Attributes[typeof(PersistenceModeAttribute)] as PersistenceModeAttribute;

            if (modeAttrib == null || modeAttrib.Mode == PersistenceMode.Attribute)
            {
                if (prop.SerializationVisibility == DesignerSerializationVisibility.Visible)
                {
                    if (prefix == string.Empty)
                    {
                        writer.WriteAttribute(prop.Name, prop.Converter.ConvertToString(prop.GetValue(o)));
                    }
                    else
                    {
                        writer.WriteAttribute(prefix + "-" + prop.Name, prop.Converter.ConvertToString(prop.GetValue(o)));
                    }
                    foundAttrib = true;
                }
                //recursively handle subproperties
                else if (prop.SerializationVisibility == DesignerSerializationVisibility.Content)
                {
                    object val = prop.GetValue(o);
                    foreach (PropertyDescriptor p in prop.GetChildProperties(val))
                    {
                        if (ProcessAttribute(p, val, writer, prop.Name))
                        {
                            foundAttrib = true;
                        }
                    }
                }
            }
            return(foundAttrib);
        }
Example #4
0
        //simply checks if there are any inner properties to render so we can use self-closing tags
        private static bool HasInnerProperties(object component)
        {
            if (component == null)
            {
                throw new ArgumentNullException("component");
            }


            //Do we have child controls as inner content of control?
            PersistChildrenAttribute persAtt = TypeDescriptor.GetAttributes(component)[typeof(PersistChildrenAttribute)] as PersistChildrenAttribute;

            if (persAtt != null && persAtt.Persist && (component is Control))
            {
                return(true);
            }
            //We don't, so we're going to have to go though the properties
            else
            {
                PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(component);
                foreach (PropertyDescriptor prop in properties)
                {
                    //check whether we're serialising it
                    if (prop.SerializationVisibility == DesignerSerializationVisibility.Hidden ||
                        prop.DesignTimeOnly
                        //|| !prop.ShouldSerializeValue(component) //confused by collections....
                        || prop.Converter == null)
                    {
                        continue;
                    }

                    PersistenceModeAttribute modeAttrib = prop.Attributes[typeof(PersistenceModeAttribute)] as PersistenceModeAttribute;
                    if (modeAttrib == null || modeAttrib.Mode == PersistenceMode.Attribute)
                    {
                        continue;
                    }

                    return(true);
                }
            }

            return(false);
        }
Example #5
0
        public ServerObjectParsingObject(Type type, Hashtable attributes, string tagid, ParsingObject parent)
            : base(tagid, parent)
        {
            //create the object
            if (type.GetInterface("System.ComponentModel.IComponent") != null)
            {
                //note: this automatically adds to parent's container, as some controls
                //need to be sited e.g. if they use site dictionaries
                //TODO: should this action be passed up the tree so controls can intercept?
                obj = ((AspNetEdit.Editor.ComponentModel.DesignerHost)base.DesignerHost).CreateComponent(type, attributes["ID"] as string, false);
            }
            else
            {
                obj = Activator.CreateInstance(type);
            }

            //and populate it from the attributes
            pdc = TypeDescriptor.GetProperties(obj);
            foreach (DictionaryEntry de in attributes)
            {
                if (0 == string.Compare((string)de.Key, "runat"))
                {
                    continue;
                }
                if (0 == string.Compare((string)de.Key, "ID"))
                {
                    continue;
                }
                //use the dash subproperty syntax
                string[]           str = ((string)de.Key).Split('-');
                PropertyDescriptor pd  = pdc.Find(str[0], true);

                //if property not found, try events
                if (str.Length == 1 && pd == null && CultureInfo.InvariantCulture.CompareInfo.IsPrefix(str[0].ToLower(), "on"))
                {
                    IEventBindingService iebs = (IEventBindingService)DesignerHost.GetService(typeof(IEventBindingService));
                    if (iebs == null)
                    {
                        throw new Exception("Could not obtain IEventBindingService from host");
                    }

                    EventDescriptorCollection edc = TypeDescriptor.GetEvents(obj);
                    EventDescriptor           e   = edc.Find(str[0].Remove(0, 2), true);
                    if (e != null)
                    {
                        pd = iebs.GetEventProperty(e);
                    }
                    else
                    {
                        throw new Exception("Could not find event " + str[0].Remove(0, 2));
                    }
                }

                object loopObj = obj;

                for (int i = 0; i < str.Length; i++)
                {
                    if (pd == null)
                    {
                        throw new Exception("Could not find property " + (string)de.Key);
                    }

                    if (i == str.Length - 1)
                    {
                        pd.SetValue(obj, pd.Converter.ConvertFromString((string)de.Value));
                        break;
                    }

                    loopObj = pd.GetValue(loopObj);
                    pd      = TypeDescriptor.GetProperties(loopObj).Find(str[0], true);
                }
            }

            parseAtt = TypeDescriptor.GetAttributes(obj)[typeof(ParseChildrenAttribute)] as ParseChildrenAttribute;
            //FIXME: fix this in MCS classlib
            if (parseAtt.DefaultProperty.Length == 0)
            {
                parseAtt = null;
            }

            //work out how we're trying to parse the children
            if (parseAtt != null)
            {
                if (parseAtt.DefaultProperty != null)
                {
                    PropertyDescriptor pd = pdc[parseAtt.DefaultProperty];
                    if (pd == null)
                    {
                        throw new Exception("Default property does not exist");
                    }
                    if (pd.PropertyType.GetInterface("System.Collections.IList") == (typeof(IList)))
                    {
                        mode = ParseChildrenMode.DefaultCollectionProperty;
                    }
                    else
                    {
                        mode = ParseChildrenMode.DefaultProperty;
                    }
                }
                else if (parseAtt.ChildrenAsProperties)
                {
                    mode = ParseChildrenMode.Properties;
                }
                else
                {
                    mode = ParseChildrenMode.Controls;
                }
            }
            else
            {
                //FIXME: these are actually persistence hints, but ParseChildrenAttribute doesn't always exist.
                //FIXME: logic would be dodgy with bad input
                parseAtt = ParseChildrenAttribute.Default;
                mode     = ParseChildrenMode.Controls;
                foreach (PropertyDescriptor pd in pdc)
                {
                    PersistenceModeAttribute modeAttrib = pd.Attributes[typeof(PersistenceModeAttribute)] as PersistenceModeAttribute;
                    if (modeAttrib == null)
                    {
                        return;
                    }

                    switch (modeAttrib.Mode)
                    {
                    case PersistenceMode.Attribute:
                        continue;

                    case PersistenceMode.EncodedInnerDefaultProperty:
                        parseAtt.DefaultProperty = pd.Name;
                        mode = ParseChildrenMode.DefaultEncodedProperty;
                        break;

                    case PersistenceMode.InnerDefaultProperty:
                        parseAtt.DefaultProperty = pd.Name;
                        if (pd.PropertyType.GetInterface("System.Collections.IList") == (typeof(IList)))
                        {
                            mode = ParseChildrenMode.DefaultCollectionProperty;
                        }
                        else
                        {
                            mode = ParseChildrenMode.DefaultProperty;
                        }
                        break;

                    case PersistenceMode.InnerProperty:
                        mode = ParseChildrenMode.Properties;
                        break;
                    }
                }
            }
        }
Example #6
0
        /// <summary>
        ///    <para>
        ///       Persists the inner properties of the control.
        ///    </para>
        /// </summary>
        /// <param name='sw'>
        ///    The string writer to use.
        /// </param>
        /// <param name=' component'>
        ///    The component to persist.
        /// </param>
        /// <param name='host'>
        ///    The services interface exposed by the webforms designer.
        /// </param>
        internal static void PersistInnerProperties(TextWriter sw, object component, IDesignerHost host)
        {
            PropertyDescriptorCollection propDescs = TypeDescriptor.GetProperties(component);

            for (int i = 0; i < propDescs.Count; i++)
            {
                // Only deal with inner attributes that need to be persisted
                if (propDescs[i].SerializationVisibility == DesignerSerializationVisibility.Hidden)
                {
                    continue;
                }

                PersistenceModeAttribute persistenceMode = (PersistenceModeAttribute)propDescs[i].Attributes[typeof(PersistenceModeAttribute)];
                if (persistenceMode.Mode == PersistenceMode.Attribute)
                {
                    continue;
                }

                if (propDescs[i].PropertyType == typeof(string))
                {
                    // String based property...

                    DataBindingCollection dataBindings = null;
                    if (component is IDataBindingsAccessor)
                    {
                        dataBindings = ((IDataBindingsAccessor)component).DataBindings;
                    }
                    if (dataBindings == null || dataBindings[propDescs[i].Name] == null)
                    {
                        PersistenceMode mode = persistenceMode.Mode;
                        if ((mode == PersistenceMode.InnerDefaultProperty) ||
                            (mode == PersistenceMode.EncodedInnerDefaultProperty))
                        {
                            PersistStringProperty(sw, component, propDescs[i], mode);
                        }
                        else
                        {
                            Debug.Fail("Cannot persist inner string property marked with PersistenceMode.InnerProperty");
                        }
                    }
                }
                else if (typeof(ITemplate).IsAssignableFrom(propDescs[i].PropertyType))
                {
                    // Template based property...
                    if (persistenceMode.Mode == PersistenceMode.InnerProperty)
                    {
                        PersistTemplateProperty(sw, component, propDescs[i]);
                    }
                    else
                    {
                        Debug.Fail("Cannot persist template property " + propDescs[i].Name + " not marked with PersistenceMode.InnerProperty");
                    }
                }
/* AUI change 03/21/01 */
                else if (propDescs[i].DisplayName.Equals("Templates") &&
                         component is DeviceSpecificChoice &&
                         typeof(IDictionary).IsAssignableFrom(propDescs[i].PropertyType))
                {
                    IDictionary templateCollection = (IDictionary)propDescs[i].GetValue(component);
                    foreach (String templateName in templateCollection.Keys)
                    {
                        ITemplate template = (ITemplate)templateCollection[templateName];
                        PersistTemplateProperty(sw, templateName, template);
                    }
                }
/* End of AUI change*/
                else if (typeof(ICollection).IsAssignableFrom(propDescs[i].PropertyType))
                {
                    // Collection based property...
                    if ((persistenceMode.Mode == PersistenceMode.InnerProperty) ||
                        (persistenceMode.Mode == PersistenceMode.InnerDefaultProperty))
                    {
                        PersistCollectionProperty(sw, component, propDescs[i], persistenceMode.Mode, host);
                    }
                    else
                    {
                        Debug.Fail("Cannot persist collection property " + propDescs[i].Name + " not marked with PersistenceMode.InnerProperty or PersistenceMode.InnerDefaultProperty");
                    }
                }
                else
                {
                    // Other complex property...
                    if (persistenceMode.Mode == PersistenceMode.InnerProperty)
                    {
                        PersistComplexProperty(sw, component, propDescs[i], host);
                    }
                    else
                    {
                        Debug.Fail("Cannot persist complex property " + propDescs[i].Name + " not marked with PersistenceMode.InnerProperty");
                    }
                }
            }
        }
Example #7
0
        private static void PersistInnerProperties(HtmlTextWriter writer, object component, IDesignerHost host)
        {
            //Do we have child controls as inner content of control?
            PersistChildrenAttribute persAtt = TypeDescriptor.GetAttributes(component)[typeof(PersistChildrenAttribute)] as PersistChildrenAttribute;

            if (persAtt != null && persAtt.Persist && (component is Control))
            {
                if (((Control)component).Controls.Count > 0)
                {
                    writer.Indent++;
                    foreach (Control child in ((Control)component).Controls)
                    {
                        PersistControl(writer, child, host);
                    }
                    writer.Indent--;
                }
            }
            //We don't, so we're going to have to go though the properties
            else
            {
                PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(component);
                bool contentStarted = false;
                foreach (PropertyDescriptor prop in properties)
                {
                    //check whether we're serialising it
                    if (prop.SerializationVisibility == DesignerSerializationVisibility.Hidden ||
                        prop.DesignTimeOnly
                        //|| !prop.ShouldSerializeValue (component) //confused by collections...
                        || prop.Converter == null)
                    {
                        continue;
                    }

                    PersistenceModeAttribute modeAttrib = prop.Attributes[typeof(PersistenceModeAttribute)] as PersistenceModeAttribute;
                    if (modeAttrib == null || modeAttrib.Mode == PersistenceMode.Attribute)
                    {
                        continue;
                    }

                    //handle the different modes
                    switch (modeAttrib.Mode)
                    {
                    case PersistenceMode.EncodedInnerDefaultProperty:
                        if (contentStarted)
                        {
                            throw new Exception("The Control has inner properties in addition to a default inner property");
                        }
                        if (prop.Converter.CanConvertTo(typeof(string)))
                        {
                            writer.Write(System.Web.HttpUtility.HtmlEncode(prop.Converter.ConvertToString(prop.GetValue(component))));
                            return;
                        }
                        break;

                    case PersistenceMode.InnerDefaultProperty:
                        if (contentStarted)
                        {
                            throw new Exception("The Control has inner properties in addition to a default inner property");
                        }
                        PersistInnerProperty(prop, prop.GetValue(component), writer, host, true);
                        return;

                    case PersistenceMode.InnerProperty:
                        PersistInnerProperty(prop, prop.GetValue(component), writer, host, false);
                        contentStarted = true;
                        break;
                    }
                }
                writer.WriteLine();
            }
        }
Example #8
0
        /// <summary>
        /// Writes an attribute to an HtmlTextWriter if it needs serializing
        /// </summary>
        /// <returns>True if it does any writing</returns>
        private static bool ProcessAttribute(PropertyDescriptor prop, object o, HtmlTextWriter writer, string prefix)
        {
            //FIXME: there are several NotImplementedExceptions in Mono's ASP.NET 2.0
            //this is a hack so it doesn't break when encountering them
            try {
                prop.GetValue(o);
            } catch (Exception ex) {
                if ((ex is NotImplementedException) || (ex.InnerException is NotImplementedException))
                {
                    return(false);
                }
                else
                {
                    throw;
                }
            }

            //FIXME: Mono has started to return prop.ShouldSerializeValue = false for ID
            //workaround, because I'm not sure if this is expected behaviour, and it would still be
            //broken for some people's runtime
            if (prop.DisplayName == "ID")
            {
                writer.WriteAttribute("id", prop.GetValue(o) as string);
                return(true);
            }

            //check whether we're serialising it
            if (prop.SerializationVisibility == DesignerSerializationVisibility.Hidden ||
                prop.DesignTimeOnly ||
                prop.IsReadOnly ||
                !prop.ShouldSerializeValue(o) ||
                prop.Converter == null ||
                !prop.Converter.CanConvertTo(typeof(string)))
            {
                return(false);
            }

            bool foundAttrib = false;

            //is this an attribute? If it's content, we deal with it later.
            PersistenceModeAttribute modeAttrib = prop.Attributes[typeof(PersistenceModeAttribute)] as PersistenceModeAttribute;

            if (modeAttrib == null || modeAttrib.Mode == PersistenceMode.Attribute)
            {
                if (prop.SerializationVisibility == DesignerSerializationVisibility.Visible)
                {
                    if (prefix == string.Empty)
                    {
                        writer.WriteAttribute(prop.Name, prop.Converter.ConvertToString(prop.GetValue(o)));
                    }
                    else
                    {
                        writer.WriteAttribute(prefix + "-" + prop.Name, prop.Converter.ConvertToString(prop.GetValue(o)));
                    }
                    foundAttrib = true;
                }
                //recursively handle subproperties
                else if (prop.SerializationVisibility == DesignerSerializationVisibility.Content)
                {
                    object val = prop.GetValue(o);
                    foreach (PropertyDescriptor p in prop.GetChildProperties(val))
                    {
                        if (ProcessAttribute(p, val, writer, prop.Name))
                        {
                            foundAttrib = true;
                        }
                    }
                }
            }
            return(foundAttrib);
        }
        private static string LocalizeObject(IServiceProvider serviceProvider, object obj, ObjectPersistData persistData, ResourceExpressionEditor resEditor, IDesignTimeResourceWriter resourceWriter, string resourceKey, string objectModelName, object topLevelObject, string filter, bool shouldLocalizeInnerContent, bool isComplexProperty, bool implicitlyLocalizeComplexProperty)
        {
            bool flag;

            if (isComplexProperty)
            {
                flag = implicitlyLocalizeComplexProperty;
            }
            else
            {
                flag = (persistData == null) || persistData.Localize;
            }
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(obj);

            for (int i = 0; i < properties.Count; i++)
            {
                try
                {
                    PropertyDescriptor propertyDescriptor = properties[i];
                    if (string.Equals(propertyDescriptor.Name, "Controls", StringComparison.Ordinal))
                    {
                        Control control = obj as Control;
                        if ((control != null) && shouldLocalizeInnerContent)
                        {
                            if (!ParseChildren(control.GetType()))
                            {
                                foreach (Control control2 in control.Controls)
                                {
                                    IControlBuilderAccessor accessor       = control2;
                                    ControlBuilder          controlBuilder = accessor.ControlBuilder;
                                    if (controlBuilder != null)
                                    {
                                        string str = controlBuilder.GetResourceKey();
                                        string b   = LocalizeObject(serviceProvider, control2, controlBuilder.GetObjectPersistData(), resEditor, resourceWriter, str, string.Empty, control2, string.Empty, true, false, false);
                                        if (!string.Equals(str, b, StringComparison.OrdinalIgnoreCase))
                                        {
                                            controlBuilder.SetResourceKey(b);
                                        }
                                    }
                                }
                            }
                            continue;
                        }
                    }
                    PersistenceModeAttribute attribute = (PersistenceModeAttribute)propertyDescriptor.Attributes[typeof(PersistenceModeAttribute)];
                    string str3 = (objectModelName.Length > 0) ? (objectModelName + '.' + propertyDescriptor.Name) : propertyDescriptor.Name;
                    if ((attribute.Mode == PersistenceMode.Attribute) && (propertyDescriptor.SerializationVisibility == DesignerSerializationVisibility.Content))
                    {
                        resourceKey = LocalizeObject(serviceProvider, propertyDescriptor.GetValue(obj), persistData, resEditor, resourceWriter, resourceKey, str3, topLevelObject, filter, true, true, flag);
                    }
                    else if ((attribute.Mode == PersistenceMode.Attribute) || (propertyDescriptor.PropertyType == typeof(string)))
                    {
                        bool   flag2 = false;
                        bool   flag3 = false;
                        object obj2  = null;
                        string name  = string.Empty;
                        if (persistData != null)
                        {
                            PropertyEntry filteredProperty = persistData.GetFilteredProperty(string.Empty, str3);
                            if (filteredProperty is BoundPropertyEntry)
                            {
                                BoundPropertyEntry entry2 = (BoundPropertyEntry)filteredProperty;
                                if (!entry2.Generated)
                                {
                                    if (string.Equals(entry2.ExpressionPrefix, "resources", StringComparison.OrdinalIgnoreCase))
                                    {
                                        System.Web.Compilation.ResourceExpressionFields parsedExpressionData = entry2.ParsedExpressionData as System.Web.Compilation.ResourceExpressionFields;
                                        if ((parsedExpressionData != null) && string.IsNullOrEmpty(parsedExpressionData.ClassKey))
                                        {
                                            name = parsedExpressionData.ResourceKey;
                                            obj2 = resEditor.EvaluateExpression(entry2.Expression, entry2.ParsedExpressionData, propertyDescriptor.PropertyType, serviceProvider);
                                            if (obj2 != null)
                                            {
                                                flag3 = true;
                                            }
                                            flag2 = true;
                                        }
                                    }
                                }
                                else
                                {
                                    flag2 = true;
                                }
                            }
                            else
                            {
                                flag2 = flag && IsPropertyLocalizable(propertyDescriptor);
                            }
                        }
                        else
                        {
                            flag2 = flag && IsPropertyLocalizable(propertyDescriptor);
                        }
                        if (flag2)
                        {
                            if (!flag3)
                            {
                                obj2 = propertyDescriptor.GetValue(obj);
                            }
                            if (name.Length == 0)
                            {
                                if (string.IsNullOrEmpty(resourceKey))
                                {
                                    resourceKey = resourceWriter.CreateResourceKey(null, topLevelObject);
                                }
                                name = resourceKey + '.' + str3;
                                if (filter.Length != 0)
                                {
                                    name = filter + ':' + name;
                                }
                            }
                            resourceWriter.AddResource(name, obj2);
                        }
                        if (persistData != null)
                        {
                            foreach (PropertyEntry entry3 in persistData.GetPropertyAllFilters(str3))
                            {
                                if (entry3.Filter.Length > 0)
                                {
                                    if (entry3 is SimplePropertyEntry)
                                    {
                                        if (flag && IsPropertyLocalizable(propertyDescriptor))
                                        {
                                            if (name.Length == 0)
                                            {
                                                if (string.IsNullOrEmpty(resourceKey))
                                                {
                                                    resourceKey = resourceWriter.CreateResourceKey(null, topLevelObject);
                                                }
                                                name = resourceKey + '.' + str3;
                                            }
                                            string str5 = entry3.Filter + ':' + name;
                                            resourceWriter.AddResource(str5, ((SimplePropertyEntry)entry3).Value);
                                        }
                                    }
                                    else if (!(entry3 is ComplexPropertyEntry) && (entry3 is BoundPropertyEntry))
                                    {
                                        BoundPropertyEntry entry4 = (BoundPropertyEntry)entry3;
                                        if (!entry4.Generated && string.Equals(entry4.ExpressionPrefix, "resources", StringComparison.OrdinalIgnoreCase))
                                        {
                                            System.Web.Compilation.ResourceExpressionFields fields2 = entry4.ParsedExpressionData as System.Web.Compilation.ResourceExpressionFields;
                                            if ((fields2 != null) && string.IsNullOrEmpty(fields2.ClassKey))
                                            {
                                                object obj3 = resEditor.EvaluateExpression(entry4.Expression, entry4.ParsedExpressionData, entry3.PropertyInfo.PropertyType, serviceProvider);
                                                if (obj3 == null)
                                                {
                                                    obj3 = string.Empty;
                                                }
                                                resourceWriter.AddResource(fields2.ResourceKey, obj3);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else if (shouldLocalizeInnerContent)
                    {
                        if (typeof(ICollection).IsAssignableFrom(propertyDescriptor.PropertyType))
                        {
                            if (persistData != null)
                            {
                                foreach (ComplexPropertyEntry entry5 in persistData.GetPropertyAllFilters(propertyDescriptor.Name))
                                {
                                    foreach (ComplexPropertyEntry entry6 in entry5.Builder.GetObjectPersistData().CollectionItems)
                                    {
                                        ControlBuilder builder = entry6.Builder;
                                        object         obj4    = builder.BuildObject();
                                        string         str6    = builder.GetResourceKey();
                                        string         str7    = LocalizeObject(serviceProvider, obj4, builder.GetObjectPersistData(), resEditor, resourceWriter, str6, string.Empty, obj4, string.Empty, true, false, false);
                                        if (!string.Equals(str6, str7, StringComparison.OrdinalIgnoreCase))
                                        {
                                            builder.SetResourceKey(str7);
                                        }
                                    }
                                }
                            }
                        }
                        else if (typeof(ITemplate).IsAssignableFrom(propertyDescriptor.PropertyType))
                        {
                            if (persistData != null)
                            {
                                foreach (TemplatePropertyEntry entry7 in persistData.GetPropertyAllFilters(propertyDescriptor.Name))
                                {
                                    TemplateBuilder builder3     = (TemplateBuilder)entry7.Builder;
                                    IDesignerHost   designerHost = (IDesignerHost)serviceProvider.GetService(typeof(IDesignerHost));
                                    Control[]       controlArray = ControlParser.ParseControls(designerHost, builder3.Text);
                                    for (int j = 0; j < controlArray.Length; j++)
                                    {
                                        if (!(controlArray[j] is LiteralControl) && !(controlArray[j] is DesignerDataBoundLiteralControl))
                                        {
                                            LocalizeControl(controlArray[j], serviceProvider, resourceWriter, true);
                                        }
                                    }
                                    StringBuilder builder4 = new StringBuilder();
                                    for (int k = 0; k < controlArray.Length; k++)
                                    {
                                        if (controlArray[k] is LiteralControl)
                                        {
                                            builder4.Append(((LiteralControl)controlArray[k]).Text);
                                        }
                                        else
                                        {
                                            builder4.Append(ControlPersister.PersistControl(controlArray[k], designerHost));
                                        }
                                    }
                                    builder3.Text = builder4.ToString();
                                }
                            }
                        }
                        else if (persistData != null)
                        {
                            object               obj5 = propertyDescriptor.GetValue(obj);
                            ObjectPersistData    objectPersistData = null;
                            ComplexPropertyEntry entry8            = (ComplexPropertyEntry)persistData.GetFilteredProperty(string.Empty, propertyDescriptor.Name);
                            if (entry8 != null)
                            {
                                objectPersistData = entry8.Builder.GetObjectPersistData();
                            }
                            resourceKey = LocalizeObject(serviceProvider, obj5, objectPersistData, resEditor, resourceWriter, resourceKey, str3, topLevelObject, string.Empty, true, true, flag);
                            foreach (ComplexPropertyEntry entry9 in persistData.GetPropertyAllFilters(propertyDescriptor.Name))
                            {
                                if (entry9.Filter.Length > 0)
                                {
                                    ControlBuilder builder5 = entry9.Builder;
                                    objectPersistData = builder5.GetObjectPersistData();
                                    obj5        = builder5.BuildObject();
                                    resourceKey = LocalizeObject(serviceProvider, obj5, objectPersistData, resEditor, resourceWriter, resourceKey, str3, topLevelObject, entry9.Filter, true, true, flag);
                                }
                            }
                        }
                    }
                }
                catch (Exception exception)
                {
                    if (serviceProvider != null)
                    {
                        IComponentDesignerDebugService service = serviceProvider.GetService(typeof(IComponentDesignerDebugService)) as IComponentDesignerDebugService;
                        if (service != null)
                        {
                            service.Fail(exception.Message);
                        }
                    }
                }
            }
            return(resourceKey);
        }
Example #10
0
        public static void TestCollectionProperty(object instance, string propertyName, PersistenceModeAttribute persistenceModeAttribute, Type nonAbstractElementType)
        {
            PropertyInfo propInfo = GetPropertyInfo(instance, propertyName);

            Assert.IsTrue(typeof(IList).IsAssignableFrom(propInfo.PropertyType),
                          "Collection must implement IList");

            // NOTE: Modified to support ParameterCollection's Add method overloads.
            MethodInfo addMethodInfo = null;

            MethodInfo[] addMethodInfos = propInfo.PropertyType.GetMethods();
            foreach (MethodInfo methodInfo in addMethodInfos)
            {
                if ((methodInfo.Name.Equals("Add")) && (methodInfo.GetParameters().Length == 1))
                {
                    addMethodInfo = methodInfo;
                    break;
                }
            }
            Assert.IsNotNull(addMethodInfo, "Could not locate the collection Add method");
            Type elementType = addMethodInfo.GetParameters()[0].ParameterType;

            Assert.AreNotEqual(typeof(object), elementType, "Collection must be strongly typed");

            TestAttribute(propInfo, persistenceModeAttribute);

            // Test initial value
            IList propertyValue = (IList)propInfo.GetValue(instance, null);

            Assert.IsNotNull(propertyValue, "Initial value of collection property should not be null");
            Assert.AreEqual(0, propertyValue.Count, "Initial value of collection property should be empty");

            // Test add
            object newElement = Activator.CreateInstance(nonAbstractElementType ?? elementType);

            propertyValue.Add(newElement);
            Assert.AreEqual(newElement, propertyValue[0], "Test element not added to collection");
        }
Example #11
0
 public static void TestCollectionProperty(object instance, string propertyName, PersistenceModeAttribute persistenceModeAttribute)
 {
     TestCollectionProperty(instance, propertyName, persistenceModeAttribute, null);
 }
        /// <include file='doc\WebControlPersister.uex' path='docs/doc[@for="ControlPersister.PersistInnerProperties1"]/*' />
        /// <devdoc>
        ///    <para>
        ///       Persists the inner properties of the control.
        ///    </para>
        /// </devdoc>
        public static void PersistInnerProperties(TextWriter sw, object component, IDesignerHost host)
        {
            PropertyDescriptorCollection propDescs = TypeDescriptor.GetProperties(component);

            for (int i = 0; i < propDescs.Count; i++)
            {
                // Only deal with inner attributes that need to be persisted
                if (propDescs[i].SerializationVisibility == DesignerSerializationVisibility.Hidden)
                {
                    continue;
                }

                PersistenceModeAttribute persistenceMode = (PersistenceModeAttribute)propDescs[i].Attributes[typeof(PersistenceModeAttribute)];
                if (persistenceMode.Mode == PersistenceMode.Attribute)
                {
                    continue;
                }

                if (propDescs[i].PropertyType == typeof(string))
                {
                    // String based property...

                    DataBindingCollection dataBindings = null;
                    if (component is IDataBindingsAccessor)
                    {
                        dataBindings = ((IDataBindingsAccessor)component).DataBindings;
                    }
                    if (dataBindings == null || dataBindings[propDescs[i].Name] == null)
                    {
                        PersistenceMode mode = persistenceMode.Mode;
                        if ((mode == PersistenceMode.InnerDefaultProperty) ||
                            (mode == PersistenceMode.EncodedInnerDefaultProperty))
                        {
                            PersistStringProperty(sw, component, propDescs[i], mode);
                        }
                        else
                        {
                            Debug.Fail("Cannot persist inner string property marked with PersistenceMode.InnerProperty");
                        }
                    }
                }
                else if (typeof(ICollection).IsAssignableFrom(propDescs[i].PropertyType))
                {
                    // Collection based property...
                    if ((persistenceMode.Mode == PersistenceMode.InnerProperty) ||
                        (persistenceMode.Mode == PersistenceMode.InnerDefaultProperty))
                    {
                        PersistCollectionProperty(sw, component, propDescs[i], persistenceMode.Mode, host);
                    }
                    else
                    {
                        Debug.Fail("Cannot persist collection property " + propDescs[i].Name + " not marked with PersistenceMode.InnerProperty or PersistenceMode.InnerDefaultProperty");
                    }
                }
                else if (typeof(ITemplate).IsAssignableFrom(propDescs[i].PropertyType))
                {
                    // Template based property...
                    if (persistenceMode.Mode == PersistenceMode.InnerProperty)
                    {
                        PersistTemplateProperty(sw, component, propDescs[i]);
                    }
                    else
                    {
                        Debug.Fail("Cannot persist template property " + propDescs[i].Name + " not marked with PersistenceMode.InnerProperty");
                    }
                }
                else
                {
                    // Other complex property...
                    if (persistenceMode.Mode == PersistenceMode.InnerProperty)
                    {
                        PersistComplexProperty(sw, component, propDescs[i], host);
                    }
                    else
                    {
                        Debug.Fail("Cannot persist complex property " + propDescs[i].Name + " not marked with PersistenceMode.InnerProperty");
                    }
                }
            }
        }
        /// <include file='doc\UserControlDesigner.uex' path='docs/doc[@for="ControlDesigner.OnComponentChanged"]/*' />
        /// <devdoc>
        ///    <para>
        ///       Delegate to handle component changed event.
        ///    </para>
        /// </devdoc>
        public virtual void OnComponentChanged(object sender, ComponentChangedEventArgs ce)
        {
            if (IsIgnoringComponentChanges)
            {
                return;
            }

            IComponent component = Component;

            Debug.Assert(ce.Component == component, "ControlDesigner::OnComponentChanged - Called from an unknown/invalid source object");

            if (DesignTimeElement == null)
            {
                return;
            }

            MemberDescriptor member = ce.Member;

            if (member != null)
            {
                // CONSIDER: Figure out a better and more correct way than looking for internal types...
                Type t = Type.GetType("System.ComponentModel.ReflectPropertyDescriptor, " + AssemblyRef.System);

                if ((member.GetType() != t) ||
                    (ce.NewValue != null && ce.NewValue == ce.OldValue))
                {
                    // HACK: ideally, we would prevent the property descriptor from firing this change.
                    // This would tear large holes in the WFC architecture. Instead, we do the
                    // filtering ourselves in this evil fashion.

                    Debug.WriteLineIf(CompModSwitches.UserControlDesigner.TraceInfo, "    ...ignoring property descriptor of type: " + member.GetType().Name);
                    return;
                }

                if (((PropertyDescriptor)member).SerializationVisibility != DesignerSerializationVisibility.Hidden)
                {
                    // Set the dirty state upon changing persistable properties.
                    this.IsDirty = true;

                    PersistenceModeAttribute persistenceType = (PersistenceModeAttribute)member.Attributes[typeof(PersistenceModeAttribute)];
                    PersistenceMode          mode            = persistenceType.Mode;

                    if ((mode == PersistenceMode.Attribute) ||
                        (mode == PersistenceMode.InnerDefaultProperty) ||
                        (mode == PersistenceMode.EncodedInnerDefaultProperty))
                    {
                        string propName = member.Name;

                        // Check to see whether the property that was changed is data bound.
                        // If it is we need to remove it...
                        // For this rev, we're only doing this for the properties on the Component itself
                        // as we can't distinguish which subproperty of a complex type changed.
                        if (IsPropertyBound(propName) && (ce.Component == Component))
                        {
                            DataBindings.Remove(propName, false);
                            Behavior.RemoveAttribute(propName, true);
                        }
                    }
                    if (mode == PersistenceMode.Attribute)
                    {
                        // For tag level properties ...

                        string propName = member.Name;

                        PersistProperties(propName, ce.Component, (PropertyDescriptor)member);
                        PersistAttribute((PropertyDescriptor)member, ce.Component, propName);
                    }
                }
            }
            else
            {
                // member is null, meaning that the whole component
                // could have changed and not just a single member.
                // This happens when a component is edited through a ComponentEditor.

                // Set the dirty state if more than one property is changed.
                this.IsDirty = true;

                PersistProperties(string.Empty, ce.Component, null);
                OnBindingsCollectionChanged(null);
            }

            // Update the WYSIWYG HTML.
            UpdateDesignTimeHtml();
        }