YAXElementFor for complex nested types

Jun 7, 2012 at 7:17 PM
Edited Jun 7, 2012 at 7:21 PM

I would like to use a generic class, which contains payload property of any type. But i want them to seriliaze, as if the payload would be part of the generic class. The example below throws an exception on serialization.

I know, i should use the YAXElemenentFor attribute on Year and Author. But these classes should be freed from any YAX attribute. Is it possible to extend the YAX library for this kind of behaviour?


public class SamplePayload { public string Year { get; set; } public string Author { get; set; } } [YAXSerializeAs("resource")] public class Resource<T> { [YAXElementFor("..")] public T Data { get; set; } [YAXAttributeForClass] public string Name { get; set; } [YAXAttributeForClass] public string Href { get; set; } }

var sample = new Resource<SamplePayload>
            {
                Name = "Sample",
                Href = @"http://example.com/sample",
                Data = new SamplePayload
                        {
                            Author = "Jakob", Year = "2012"
                        }
            };
            var serializer = new YAXSerializer(typeof (Resource<SamplePayload>));
            var result = serializer.Serialize(sample);

Should generate:
<resource Name="Sample" Href="http://example.com/sample">

    <Year>2012</Year>
    <Author>Jakob</Author>

</resource>
   
Coordinator
Jun 9, 2012 at 8:56 AM

I regard this discussion, as some sort of feature request for assigning attributes to fields that we don't have access to, or we don't want to alter, their source code. I also believe this could be useful for example for customized serialization of parts of the .NET framework or other class libraries which we don't have access to their source code.

Thanks for the feature request, I'll see how could this possibly be carried out.