2
votes

I'm using System.Xaml.XamlServices.Save method to serialize an object which has properties with public getters/private setters and by design these properties are ignored. I tried to implement advice of how to override default XAML bindings and get private properties serialized, but it doesn't work for some reason - those properties are still ignored. Could anyone point out what's wrong:

public class CustomXamlSchemaContext : XamlSchemaContext
{
    protected override XamlType GetXamlType(string xamlNamespace, string name, params XamlType[] typeArguments)
    {
        var type = base.GetXamlType(xamlNamespace, name, typeArguments);
        return new CustomXamlType(type.UnderlyingType, type.SchemaContext, type.Invoker);
    }
}

public class CustomXamlType : XamlType
{
    public CustomXamlType(Type underlyingType, XamlSchemaContext schemaContext, XamlTypeInvoker invoker) : base(underlyingType, schemaContext, invoker)
    {
    }

    protected override bool LookupIsConstructible()
    {
        return true;
    }

    protected override XamlMember LookupMember(string name, bool skipReadOnlyCheck)
    {
        var member = base.LookupMember(name, skipReadOnlyCheck);
        return new CustomXamlMember(member.Name, member.DeclaringType, member.IsAttachable);
    }
}

public class CustomXamlMember : XamlMember
{
    public CustomXamlMember(string name, XamlType declaringType, bool isAttachable) : base(name, declaringType, isAttachable)
    {
    }

    protected override bool LookupIsReadOnly()
    {
        return false;
    }
}


    public static string Save(object instance)
    {
        var stringWriter1 = new StringWriter(CultureInfo.CurrentCulture);
        var stringWriter2 = stringWriter1;
        var settings = new XmlWriterSettings { Indent = true, OmitXmlDeclaration = true };
        using (var writer = XmlWriter.Create(stringWriter2, settings))
        {
            Save(writer, instance);
        }
        return stringWriter1.ToString();
    }

    public static void Save(XmlWriter writer, object instance)
    {
        if (writer == null)
            throw new ArgumentNullException("writer");
        using (var xamlXmlWriter = new XamlXmlWriter(writer, new CustomXamlSchemaContext()))
        {
            XamlServices.Save(xamlXmlWriter, instance);
        }
    }

Having above infrastructure code and a class

public class Class1
{
    public string Property1 { get; private set; }
    public string Property2 { get; set; }
    public DateTime AddedProperty { get; set; }
}

and serializing an instance of this class with

var obj = new Class1 { Property1 = "value1", Property2 = "value2" };
var objString = Save(obj);

I get the result

<Class1 AddedProperty="0001-01-01" Property2="value2" xmlns="clr-namespace:TestNamespace;assembly=Tests" />

where there is no entry for Property1.

What's even more interesting, that none of the overloads are called during serialization.

3
I see no properties for serialization in your code example on methods. - ChrisBD
string Save(object instance) takes instance of a class with private properties - andriys
Can you give an example of a class that you're passing here? - ChrisBD

3 Answers

2
votes

It turned out couple tweaks to my initial code solves the problem. Here's final solution:

private class CustomXamlSchemaContext : XamlSchemaContext
{
    public override XamlType GetXamlType(Type type)
    {
        var xamlType = base.GetXamlType(type);
        return new CustomXamlType(xamlType.UnderlyingType, xamlType.SchemaContext, xamlType.Invoker);
    }
}

private class CustomXamlType : XamlType
{
    public CustomXamlType(Type underlyingType, XamlSchemaContext schemaContext, XamlTypeInvoker invoker)
        : base(underlyingType, schemaContext, invoker)
    {
    }

    protected override bool LookupIsConstructible()
    {
        return true;
    }

    protected override XamlMember LookupMember(string name, bool skipReadOnlyCheck)
    {
        var member = base.LookupMember(name, skipReadOnlyCheck);
        return member == null ? null : new CustomXamlMember((PropertyInfo)member.UnderlyingMember, SchemaContext, member.Invoker);
    }

    protected override IEnumerable<XamlMember> LookupAllMembers()
    {
        foreach (var member in base.LookupAllMembers())
        {
            var value = new CustomXamlMember((PropertyInfo)member.UnderlyingMember, SchemaContext, member.Invoker);
            yield return value;
        }
    }

    protected override bool LookupIsPublic()
    {
        return true;
    }
}

private class CustomXamlMember : XamlMember
{
    public CustomXamlMember(PropertyInfo propertyInfo, XamlSchemaContext schemaContext, XamlMemberInvoker invoker)
        : base(propertyInfo, schemaContext, invoker)
    {
    }

    protected override bool LookupIsReadOnly()
    {
        return false;
    }

    protected override bool LookupIsWritePublic()
    {
        return true;
    }
}

This customization allows to serialize/deserialize properties with public getter and public/internal/protected/private setters. It ignores all the other properties. It also serializes instances of internal classes.

1
votes

The problem here is that you are attempting to write readonly and private properties.

According to the XAML standard the only readonly properties that are syntactically correct are for List, Dictionary and static members:

3.3.1.6. Only List, Dictionary, or Static Members may be Read-only If neither [value type][is list] nor [value type][is dictionary], nor [is static] is True, [is read only] MUST be False.

Have a look here for MSDN syntax detail.

And the standard itself can be downloaded here.

You'll also note that only public properties have any relevance here (from msdn linked above):

In order to be set through attribute syntax, a property must be public and must be writeable. The value of the property in the backing type system must be a value type, or must be a reference type that can be instantiated or referenced by a XAML processor when accessing the relevant backing type.

For WPF XAML events, the event that is referenced as the attribute name must be public and have a public delegate.

The property or event must be a member of the class or structure that is instantiated by the containing object element.

and if you think about it you can see why.

The whole C# standard is really built around using classes that interact by using public properties and methods. By doing so other classes don't need to know what resides within a class beyond them. Each class can be treated as a black box where the public properties and methods are the class's interface to other code.

Here's an informative blog regarding XAML serialization.

Personally I would ask myself why I need to serialize/deserialize private member properties.

0
votes

I'm not quite sure what is wrong with the above code, but if you like I can provide an alternative.

This is kind of a hacky way to do it, but it works (tested it). First, you can throw away all that custom XAML stuff. Then, just change your Class1 to be:

public class Class1
{
    private string _Property1;

    public string Property2 { get; set; }
    public DateTime AddedProperty { get; set; }

    public Class1()
    {

    }

    public Class1(string prop1, string prop2)
    {
        _Property1 = prop1;
        Property2 = prop2;
    }

    public string Property1 
    { 
        get { return _Property1; }
        set { }
    }
}

While the set accessor is accessible, it doesn't do anything so in effect this is the same as a public getter/private setter setup. Proper documentation will also help if someone else needs to use your Class1 and is wondering why the 'set' isn't working for Property1.

This could be a plan B in case no one posts a fix for your above code.

Update: If you need to deserialize the object as well, you could create another object that acts as a go-between for your Class1 and the serialization process. The whole setup would look like this:

public class Class1
{
    public string Property1 { get; private set; }
    public string Property2 { get; set; }
    public DateTime AddedProperty { get; set; }

    public Class1()
    {
    }

    public Class1(string prop1, string prop2) : this()
    {
        Property1 = prop1;
        Property2 = prop2;
    }

    public Class1(Class1DTO dto)
    {
        Property1 = dto.Property1;
    }

    public Class1DTO CreateDTO()
    {
        return new Class1DTO 
        { 
            AddedProperty = AddedProperty,
            Property1 = Property1,
            Property2 = Property2
        };
    }
}

public class Class1DTO
{
    public string Property1 { get; set; }
    public string Property2 { get; set; }
    public DateTime AddedProperty { get; set; }
}

The whole serialization/deserialization process would be like this:

var obj = new Class1("value1", "value2");

var dto = obj.CreateDTO();

var objString = Save(dto);

using (var stringReader = new StringReader(objString))
{
    using (var reader = new XamlXmlReader(stringReader))
    {
        var deserializedDTO = XamlServices.Load(reader);
        var originalObj = new Class1(dto);
    }
}

You can then change access modifiers to fine tune the amount of access other people would have on your whole setup (you could create static Serialize/Deserialize methods on your Class1 type and push the Class1DTO type into a private nested class so people can't access it etc.).