public override object EditValue(ITypeDescriptorContext context, System.IServiceProvider provider, object value)
            {
                var svc     = provider.RequireService <IWindowsFormsEditorService>();
                var pd      = DictionaryPropertyDescriptor.GetFromContext(context);
                var dlgType = Type.GetType(pd.Option.TypeName);

                if (dlgType == null)
                {
                    return(value);
                }
                var form = Activator.CreateInstance(dlgType) as Form;

                if (form == null)
                {
                    return(value);
                }
                var valueProperty = dlgType.GetProperty("Value");

                if (valueProperty == null)
                {
                    return(value);
                }

                valueProperty.SetValue(form, value);
                if (svc.ShowDialog(form) == System.Windows.Forms.DialogResult.OK)
                {
                    value = valueProperty.GetValue(form);
                }
                form.Dispose();
                return(value);
            }
Beispiel #2
0
        private void DeserializeElements(IDictionary parent, DictionaryPropertyDescriptor propDesc, XmlReader reader, XmlSerializerContext context)
        {
            //avanzo hasta la posicion del elemento
            string nodeName = reader.LocalName;

            //avanzo hasta el primer nodo
            bool        end      = !reader.Read();
            XmlNodeType typeNode = reader.MoveToContent();

            DictionaryKeyValuePropertyDescriptor keyDesc   = propDesc.GetDictionaryItemKeyPropertyDescriptor(context);
            DictionaryKeyValuePropertyDescriptor valueDesc = propDesc.GetDictionaryItemValuePropertyDescriptor(context);

            //parseo las propiedades
            while (!end)
            {
                //me fijo si la propiedad es vacia
                if (reader.NodeType != XmlNodeType.EndElement)
                {
                    //tengo que leer el item del diccionario
                    this.DeserializeDictionaryItem(parent, keyDesc, valueDesc, reader, context);
                }


                reader.Read();
                //avanzo...
                typeNode = reader.MoveToContent();
                end      = reader.NodeType == XmlNodeType.EndElement && reader.LocalName.Equals(nodeName);
            }

            reader.Read();
        }
Beispiel #3
0
        protected override object DoFromXml(object parent, PropertyDescriptor propDesc, Type entityType, XmlReader reader, XmlSerializerContext context)
        {
            long   id       = this.GetInstanceId(reader);
            string nodeName = reader.LocalName;
            DictionaryPropertyDescriptor metadata = propDesc.GetPropertyDescriptor <DictionaryPropertyDescriptor>(entityType, context);

            IDictionary items = (IDictionary)Activator.CreateInstance(entityType, true);

            //recorro todos los items
            if (reader.NodeType == XmlNodeType.EndElement || reader.IsEmptyElement)
            {
                //avanzo
                if (reader.IsEmptyElement)
                {
                    reader.Read(); //avanzo para ubicarme en el siguiente nodo.
                }
            }
            else
            {
                this.DeserializeElements(items, metadata, reader, context);
            }

            //agrego el diccionario al stack
            context.Stack.AddInstance(id, items);

            //creo un array
            return(items);
        }
        public void PropertyType_ReturnsSpecifiedType()
        {
            IDictionary<string, object> data = new Dictionary<string, object>();
            data.Add("Property1", "Value1");
            data.Add("Property2", 2);

            DictionaryPropertyDescriptor pd1 = new DictionaryPropertyDescriptor(data, "Property1", typeof(string));
            Assert.That(pd1.PropertyType, Is.EqualTo(typeof(string)));

            DictionaryPropertyDescriptor pd2 = new DictionaryPropertyDescriptor(data, "Property2", typeof(int));
            Assert.That(pd2.PropertyType, Is.EqualTo(typeof(int)));
        }
        public void GetValue_ReturnsValueFromUnderlyingDictionary()
        {
            IDictionary<string, object> data = new Dictionary<string, object>();
            data.Add("Property1", "Value1");
            data.Add("Property2", 2);

            DictionaryPropertyDescriptor pd1 = new DictionaryPropertyDescriptor(data, "Property1", typeof(string));
            Assert.That(pd1.GetValue(null), Is.EqualTo(data["Property1"]));

            DictionaryPropertyDescriptor pd2 = new DictionaryPropertyDescriptor(data, "Property2", typeof(int));
            Assert.That(pd2.GetValue(null), Is.EqualTo(data["Property2"]));
        }
        public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
        {
            var properties = new PropertyDescriptor[Count];

            int i = 0;
            foreach(DictionaryEntry entry in this)
            {
                properties[i++] = new DictionaryPropertyDescriptor(this, entry.Key);
            }

            return new PropertyDescriptorCollection(properties);
        }
Beispiel #7
0
        public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
        {
            var properties = new PropertyDescriptor[Count];

            int i = 0;

            foreach (DictionaryEntry entry in this)
            {
                properties[i++] = new DictionaryPropertyDescriptor(this, entry.Key);
            }

            return(new PropertyDescriptorCollection(properties));
        }
Beispiel #8
0
        private void SerializeDictionaryItem(IDictionary dic, DictionaryEntry entry, DictionaryPropertyDescriptor propDesc, DictionaryKeyValuePropertyDescriptor keyDesc, DictionaryKeyValuePropertyDescriptor valueDesc, XmlTextWriter writer, XmlSerializerContext context)
        {
            //escribo el item
            string        itemName = propDesc.GetElementNameForDictionaryItem(context);
            object        key      = entry.Key;
            object        value    = entry.Value;
            IXmlConverter converter;

            if (key != null)
            {
                writer.WriteStartElement(itemName);

                Type keyType = key.GetType();

                if (keyDesc.IsXmlAttribute(keyType, context))
                {
                    converter = context.GetConverter(keyType);
                    converter.ToXml(dic, keyDesc, key, writer, context);
                }

                if (value != null)
                {
                    Type valueType = value.GetType();
                    if (valueDesc.IsXmlAttribute(valueType, context))
                    {
                        converter = context.GetConverter(valueType);
                        converter.ToXml(dic, valueDesc, value, writer, context);
                    }
                }

                //escribo los elementos
                if (keyDesc.IsXmlElement(keyType, context))
                {
                    converter = context.GetConverter(keyType);
                    converter.ToXml(dic, keyDesc, key, writer, context);
                }

                if (value != null)
                {
                    Type valueType = value.GetType();
                    if (valueDesc.IsXmlElement(valueType, context))
                    {
                        converter = context.GetConverter(valueType);
                        converter.ToXml(dic, valueDesc, value, writer, context);
                    }
                }

                writer.WriteEndElement();
            }
        }
        public void GetValue_ReturnsValueFromUnderlyingDictionary()
        {
            var data = new Dictionary <string, object>();

            data.Add("Property1", "Value1");
            data.Add("Property2", 2);

            var pd1 = new DictionaryPropertyDescriptor(data, "Property1", typeof(string));

            pd1.GetValue(null).Should().Be("Value1");

            var pd2 = new DictionaryPropertyDescriptor(data, "Property2", typeof(int));

            pd2.GetValue(null).Should().Be(2);
        }
        public void PropertyType_ReturnsSpecifiedType()
        {
            var data = new Dictionary <string, object>();

            data.Add("Property1", "Value1");
            data.Add("Property2", 2);

            var pd1 = new DictionaryPropertyDescriptor(data, "Property1", typeof(string));

            pd1.PropertyType.Should().Be(typeof(string));

            var pd2 = new DictionaryPropertyDescriptor(data, "Property2", typeof(int));

            pd2.PropertyType.Should().Be(typeof(int));
        }
        public void ResetValue_ChangesValueInUnderlyingDictionaryToNull()
        {
            var data = new Dictionary <string, object>();

            data.Add("Property1", "Value1");
            data.Add("Property2", 2);

            var pd1 = new DictionaryPropertyDescriptor(data, "Property1", typeof(string));

            pd1.ResetValue(null);
            data["Property1"].Should().BeNull();

            var pd2 = new DictionaryPropertyDescriptor(data, "Property2", typeof(int));

            pd2.ResetValue(null);
            data["Property2"].Should().BeNull();
        }
        public void SetValue_ChangesValueInUnderlyingDictionaryToSpecifiedValue()
        {
            var data = new Dictionary <string, object>();

            data.Add("Property1", "Value1");
            data.Add("Property2", 2);

            var pd1 = new DictionaryPropertyDescriptor(data, "Property1", typeof(string));

            pd1.SetValue(null, "Modified");
            data["Property1"].Should().Be("Modified");

            var pd2 = new DictionaryPropertyDescriptor(data, "Property2", typeof(int));

            pd2.SetValue(null, 0);
            data["Property2"].Should().Be(0);
        }
Beispiel #13
0
        protected override void DoToXml(object parent, PropertyDescriptor propDesc, object entity, XmlTextWriter writer, XmlSerializerContext context)
        {
            Type entityType = entity.GetType();
            DictionaryPropertyDescriptor metadata = propDesc.GetPropertyDescriptor <DictionaryPropertyDescriptor>(entityType, context);

            string nodeName = metadata.GetElementNameForType(entityType, context, true);

            writer.WriteStartElement(nodeName);

            if (!context.Settings.UniqueSerializationForInstance || !context.Stack.ContainsInstance(entity))
            {
                //agrego la lista a las entidades registradas
                long id = context.Stack.AddInstance(entity);

                base.WriteTypeDefinition(metadata, entityType, context, writer);

                //escribo el id del objeto si corresponde
                if (context.Settings.UniqueSerializationForInstance)
                {
                    writer.WriteAttributeString(XmlSerializerSettings.ObjectIdAttributeName, id.ToString());
                }

                IDictionary dic = (IDictionary)entity;

                DictionaryKeyValuePropertyDescriptor keyDesc   = metadata.GetDictionaryItemKeyPropertyDescriptor(context);
                DictionaryKeyValuePropertyDescriptor valueDesc = metadata.GetDictionaryItemValuePropertyDescriptor(context);

                foreach (DictionaryEntry item in dic)
                {
                    this.SerializeDictionaryItem(dic, item, metadata, keyDesc, valueDesc, writer, context);
                }
            }
            else
            {
                //me fijo si ya existe en el context
                long id = context.Stack.GetInstanceReferenceId(entity);
                writer.WriteAttributeString(XmlSerializerSettings.ObjectReferenceAttributeName, id.ToString());
            }

            writer.WriteEndElement();
        }
        public void ResetValue_ChangesValueInUnderlyingDictionaryToNull()
        {
            IDictionary<string, object> data = new Dictionary<string, object>();
            data.Add("Property1", "Value1");
            data.Add("Property2", 2);

            DictionaryPropertyDescriptor pd1 = new DictionaryPropertyDescriptor(data, "Property1", typeof(string));
            pd1.ResetValue(null);
            Assert.That(data["Property1"], Is.Null);

            DictionaryPropertyDescriptor pd2 = new DictionaryPropertyDescriptor(data, "Property2", typeof(int));
            pd2.ResetValue(null);
            Assert.That(data["Property2"], Is.Null);
        }
        public void SetValue_ChangesValueInUnderlyingDictionaryToSpecifiedValue()
        {
            IDictionary<string, object> data = new Dictionary<string, object>();
            data.Add("Property1", "Value1");
            data.Add("Property2", 2);

            DictionaryPropertyDescriptor pd1 = new DictionaryPropertyDescriptor(data, "Property1", typeof(string));
            pd1.SetValue(null, "Modified");
            Assert.That(data["Property1"], Is.EqualTo("Modified"));

            DictionaryPropertyDescriptor pd2 = new DictionaryPropertyDescriptor(data, "Property2", typeof(int));
            pd2.SetValue(null, 0);
            Assert.That(data["Property2"], Is.EqualTo(0));
        }