Advertisement
Guest User

XmlFormatterThatCanSerializeAnonymousTypes

a guest
Sep 2nd, 2014
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.74 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics.Contracts;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Net.Http;
  8. using System.Net.Http.Formatting;
  9. using System.Net.Http.Headers;
  10. using System.Reflection;
  11. using System.Runtime.CompilerServices;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. using System.Web;
  15. using XSerializer;
  16.  
  17. namespace Ogre
  18. {
  19.     public class XmlFormatterThatCanSerializeAnonymousTypes : MediaTypeFormatter
  20.     {
  21.         readonly MediaTypeFormatter wrapped;
  22.         public XmlFormatterThatCanSerializeAnonymousTypes() : this(new XmlMediaTypeFormatter()) {}
  23.         public XmlFormatterThatCanSerializeAnonymousTypes(MediaTypeFormatter wrapped) {
  24.             Contract.Requires(wrapped != null);
  25.             this.wrapped = wrapped;
  26.         }
  27.  
  28.         public override bool CanReadType(Type type) {
  29.             return wrapped.CanReadType(type);
  30.         }
  31.         public override bool CanWriteType(Type type) {
  32.             return type.IsAnonymousType() || wrapped.CanWriteType(type);
  33.         }
  34.  
  35.         public override bool Equals(object obj) {
  36.             return wrapped.Equals(obj);
  37.         }
  38.         public override int GetHashCode() {
  39.             return wrapped.GetHashCode();
  40.         }
  41.  
  42.         public override MediaTypeFormatter GetPerRequestFormatterInstance(Type type, HttpRequestMessage request, MediaTypeHeaderValue mediaType) {
  43.             return wrapped.GetPerRequestFormatterInstance(type, request, mediaType);
  44.         }
  45.  
  46.         public override Task<object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger) {
  47.             return wrapped.ReadFromStreamAsync(type, readStream, content, formatterLogger);
  48.         }
  49.  
  50.         public override Task<object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger,
  51.             CancellationToken cancellationToken) {
  52.             return wrapped.ReadFromStreamAsync(type, readStream, content, formatterLogger, cancellationToken);
  53.         }
  54.  
  55.         public override IRequiredMemberSelector RequiredMemberSelector { get; set; }
  56.         public override void SetDefaultContentHeaders(Type type, HttpContentHeaders headers, MediaTypeHeaderValue mediaType) {
  57.             wrapped.SetDefaultContentHeaders(type, headers, mediaType);
  58.         }
  59.  
  60.         public override string ToString() {
  61.             return wrapped.ToString();
  62.         }
  63.  
  64.         public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext) {
  65.             return !type.IsAnonymousType() ? serializeAnonymousType(value, type, writeStream) : wrapped.WriteToStreamAsync(type, value, writeStream, content, transportContext);
  66.         }
  67.  
  68.         public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken) {
  69.             return !type.IsAnonymousType() ? serializeAnonymousType(value, type, writeStream) : wrapped.WriteToStreamAsync(type, value, writeStream, content, transportContext, cancellationToken);
  70.         }
  71.  
  72.         static async Task serializeAnonymousType(object o, Type type, Stream writeStream) {
  73.             typeof(XmlFormatterThatCanSerializeAnonymousTypes).GetMethod("serialize", BindingFlags.NonPublic | BindingFlags.Static)
  74.                         .MakeGenericMethod(type).Invoke(null, new []{o, writeStream});
  75.         }
  76.  
  77.         static void serialize<T>(T o, Stream stream) {
  78.             using (var sw = new StreamWriter(stream)) {
  79.                 new XmlSerializer<T>().Serialize(o);
  80.                 sw.Flush();
  81.             }
  82.         }
  83.     }
  84.  
  85.     public static class TypeExtension
  86.     {
  87.         public static Boolean IsAnonymousType(this Type type) {
  88.             var hasCompilerGeneratedAttribute = type.GetCustomAttribute<CompilerGeneratedAttribute>(false) != null;
  89.             var nameContainsAnonymousType = type.FullName.Contains("AnonymousType");
  90.             return hasCompilerGeneratedAttribute && nameContainsAnonymousType && type.BaseType == typeof(object);
  91.         }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement