Inheritance: System.Attribute
 public override IEnumerable <IPropertyDescriptor> GetProperties(Type type, object container)
 {
     return(from p in (from p in innerTypeDescriptor.GetProperties(type, container)
                       where p.GetCustomAttribute <YamlIgnoreAttribute>() == null
                       select p).Select((Func <IPropertyDescriptor, IPropertyDescriptor>) delegate(IPropertyDescriptor p)
     {
         PropertyDescriptor propertyDescriptor = new PropertyDescriptor(p);
         YamlMemberAttribute customAttribute = p.GetCustomAttribute <YamlMemberAttribute>();
         if (customAttribute != null)
         {
             if (customAttribute.SerializeAs != null)
             {
                 propertyDescriptor.TypeOverride = customAttribute.SerializeAs;
             }
             propertyDescriptor.Order = customAttribute.Order;
             propertyDescriptor.ScalarStyle = customAttribute.ScalarStyle;
             if (customAttribute.Alias != null)
             {
                 propertyDescriptor.Name = customAttribute.Alias;
             }
         }
         return propertyDescriptor;
     })
            orderby p.Order
            select p);
 }
Ejemplo n.º 2
0
        public void SerializationRespectsScalarStyleOverride()
        {
            var writer = new StringWriter();
            var obj = new ScalarStyleExample();

            var overrides = new YamlAttributeOverrides();
            var style1 = new YamlMemberAttribute();
            style1.ScalarStyle = ScalarStyle.DoubleQuoted;
            var style2 = new YamlMemberAttribute();
            style2.ScalarStyle = ScalarStyle.Literal;
            overrides.Add(typeof(ScalarStyleExample), "LiteralString", style1);
            overrides.Add(typeof(ScalarStyleExample), "DoubleQuotedString", style2);
            
            var serializer = new Serializer(overrides: overrides);
            
            serializer.Serialize(writer, obj);
            var serialized = writer.ToString();
            Dump.WriteLine(serialized);

            serialized.Should()
                .Be("LiteralString: \"Test\"\r\nDoubleQuotedString: |-\r\n  Test\r\n", "the properties should be specifically styled");
        }
Ejemplo n.º 3
0
        public void RoundtripAliasOverride()
        {
            var writer = new StringWriter();
            var input = new NameConvention { AliasTest = "Fourth" };

            var overrides = new YamlAttributeOverrides();
            var attribute = new YamlMemberAttribute();
            attribute.Alias = "fourthOverride";
            overrides.Add(typeof(NameConvention), "AliasTest", attribute);
            var serializer = new Serializer(overrides: overrides);
            
            serializer.Serialize(writer, input, input.GetType());
            var text = writer.ToString();

            // Todo: use RegEx once FluentAssertions 2.2 is released
            text.TrimEnd('\r', '\n').Should().Be("fourthOverride: Fourth");

            var deserializer = new Deserializer(overrides: overrides);
            var output = deserializer.Deserialize<NameConvention>(UsingReaderFor(text));

            output.AliasTest.Should().Be(input.AliasTest);
        }