Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics.Contracts;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Net.Http.Formatting;
- using System.Net.Http.Headers;
- using System.Reflection;
- using System.Runtime.CompilerServices;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Web;
- using XSerializer;
- namespace Ogre
- {
- public class XmlFormatterThatCanSerializeAnonymousTypes : MediaTypeFormatter
- {
- readonly MediaTypeFormatter wrapped;
- public XmlFormatterThatCanSerializeAnonymousTypes() : this(new XmlMediaTypeFormatter()) {}
- public XmlFormatterThatCanSerializeAnonymousTypes(MediaTypeFormatter wrapped) {
- Contract.Requires(wrapped != null);
- this.wrapped = wrapped;
- }
- public override bool CanReadType(Type type) {
- return wrapped.CanReadType(type);
- }
- public override bool CanWriteType(Type type) {
- return type.IsAnonymousType() || wrapped.CanWriteType(type);
- }
- public override bool Equals(object obj) {
- return wrapped.Equals(obj);
- }
- public override int GetHashCode() {
- return wrapped.GetHashCode();
- }
- public override MediaTypeFormatter GetPerRequestFormatterInstance(Type type, HttpRequestMessage request, MediaTypeHeaderValue mediaType) {
- return wrapped.GetPerRequestFormatterInstance(type, request, mediaType);
- }
- public override Task<object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger) {
- return wrapped.ReadFromStreamAsync(type, readStream, content, formatterLogger);
- }
- public override Task<object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger,
- CancellationToken cancellationToken) {
- return wrapped.ReadFromStreamAsync(type, readStream, content, formatterLogger, cancellationToken);
- }
- public override IRequiredMemberSelector RequiredMemberSelector { get; set; }
- public override void SetDefaultContentHeaders(Type type, HttpContentHeaders headers, MediaTypeHeaderValue mediaType) {
- wrapped.SetDefaultContentHeaders(type, headers, mediaType);
- }
- public override string ToString() {
- return wrapped.ToString();
- }
- public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext) {
- return !type.IsAnonymousType() ? serializeAnonymousType(value, type, writeStream) : wrapped.WriteToStreamAsync(type, value, writeStream, content, transportContext);
- }
- public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken) {
- return !type.IsAnonymousType() ? serializeAnonymousType(value, type, writeStream) : wrapped.WriteToStreamAsync(type, value, writeStream, content, transportContext, cancellationToken);
- }
- static async Task serializeAnonymousType(object o, Type type, Stream writeStream) {
- typeof(XmlFormatterThatCanSerializeAnonymousTypes).GetMethod("serialize", BindingFlags.NonPublic | BindingFlags.Static)
- .MakeGenericMethod(type).Invoke(null, new []{o, writeStream});
- }
- static void serialize<T>(T o, Stream stream) {
- using (var sw = new StreamWriter(stream)) {
- new XmlSerializer<T>().Serialize(o);
- sw.Flush();
- }
- }
- }
- public static class TypeExtension
- {
- public static Boolean IsAnonymousType(this Type type) {
- var hasCompilerGeneratedAttribute = type.GetCustomAttribute<CompilerGeneratedAttribute>(false) != null;
- var nameContainsAnonymousType = type.FullName.Contains("AnonymousType");
- return hasCompilerGeneratedAttribute && nameContainsAnonymousType && type.BaseType == typeof(object);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement