Skip to content

Instantly share code, notes, and snippets.

@urasandesu
Created May 11, 2014 14:26
Show Gist options
  • Save urasandesu/2a68ce98f79c120afd60 to your computer and use it in GitHub Desktop.
Save urasandesu/2a68ce98f79c120afd60 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;
namespace DataContractSerializerTest
{
class Program
{
static void Main(string[] args)
{
var dateTimeInfo = typeof(DateTime);
{
var dcs = new DataContractSerializer(Type.GetType("System.RuntimeType"));
var sb = new StringBuilder();
var sw = new StringWriter(sb);
using (var xw = new XmlTextWriter(sw) { Formatting = Formatting.Indented })
{
dcs.WriteObject(xw, dateTimeInfo);
}
Console.WriteLine(sb.ToString());
// <RuntimeType xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:x="http://www.w3.org/2001/XMLSchema" z:FactoryType="UnitySerializationHolder" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/" xmlns="http://schemas.datacontract.org/2004/07/System">
// <Data i:type="x:string" xmlns="">System.DateTime</Data>
// <UnityType i:type="x:int" xmlns="">4</UnityType>
// <AssemblyName i:type="x:string" xmlns="">mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</AssemblyName>
// </RuntimeType>
}
Console.WriteLine();
Console.WriteLine("------------------");
Console.WriteLine();
var dateTimeInfo_get_Now = dateTimeInfo.GetMethod("get_Now");
{
var dcs = new DataContractSerializer(Type.GetType("System.Reflection.RuntimeMethodInfo"));
var sb = new StringBuilder();
var sw = new StringWriter(sb);
using (var xw = new XmlTextWriter(sw) { Formatting = Formatting.Indented })
{
dcs.WriteObject(xw, dateTimeInfo_get_Now);
}
Console.WriteLine(sb.ToString());
// <RuntimeMethodInfo xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:x="http://www.w3.org/2001/XMLSchema" z:FactoryType="MemberInfoSerializationHolder" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/" xmlns="http://schemas.datacontract.org/2004/07/System.Reflection">
// <Name i:type="x:string" xmlns="">get_Now</Name>
// <AssemblyName i:type="x:string" xmlns="">mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</AssemblyName>
// <ClassName i:type="x:string" xmlns="">System.DateTime</ClassName>
// <Signature i:type="x:string" xmlns="">System.DateTime get_Now()</Signature>
// <Signature2 i:type="x:string" xmlns="">System.DateTime get_Now()</Signature2>
// <MemberType i:type="x:int" xmlns="">8</MemberType>
// <GenericArguments i:nil="true" xmlns="" />
// </RuntimeMethodInfo>
}
Console.WriteLine();
Console.WriteLine("------------------");
Console.WriteLine();
var byteInfoArray = typeof(byte[]);
{
var dcs = new DataContractSerializer(Type.GetType("System.RuntimeType"));
var sb = new StringBuilder();
var sw = new StringWriter(sb);
using (var xw = new XmlTextWriter(sw) { Formatting = Formatting.Indented })
{
dcs.WriteObject(xw, byteInfoArray);
}
Console.WriteLine(sb.ToString());
// <RuntimeType xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:x="http://www.w3.org/2001/XMLSchema" z:FactoryType="UnitySerializationHolder" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/" xmlns="http://schemas.datacontract.org/2004/07/System">
// <Data i:type="x:string" xmlns="">System.Byte[]</Data>
// <UnityType i:type="x:int" xmlns="">4</UnityType>
// <AssemblyName i:type="x:string" xmlns="">mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</AssemblyName>
// </RuntimeType>
}
Console.WriteLine();
Console.WriteLine("------------------");
Console.WriteLine();
var dictionary2Info = typeof(Dictionary<,>);
{
var dcs = new DataContractSerializer(Type.GetType("System.RuntimeType"));
var sb = new StringBuilder();
var sw = new StringWriter(sb);
using (var xw = new XmlTextWriter(sw) { Formatting = Formatting.Indented })
{
dcs.WriteObject(xw, dictionary2Info);
}
Console.WriteLine(sb.ToString());
// <RuntimeType xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:x="http://www.w3.org/2001/XMLSchema" z:FactoryType="UnitySerializationHolder" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/" xmlns="http://schemas.datacontract.org/2004/07/System">
// <Data i:type="x:string" xmlns="">System.Collections.Generic.Dictionary`2</Data>
// <UnityType i:type="x:int" xmlns="">4</UnityType>
// <AssemblyName i:type="x:string" xmlns="">mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</AssemblyName>
// </RuntimeType>
}
Console.WriteLine();
Console.WriteLine("------------------");
Console.WriteLine();
var arrayInfo = typeof(Array);
var arrayInfo_Exists_T = arrayInfo.GetMethod("Exists");
{
var dcs = new DataContractSerializer(Type.GetType("System.Reflection.RuntimeMethodInfo"));
var sb = new StringBuilder();
var sw = new StringWriter(sb);
using (var xw = new XmlTextWriter(sw) { Formatting = Formatting.Indented })
{
dcs.WriteObject(xw, arrayInfo_Exists_T);
}
Console.WriteLine(sb.ToString());
// <RuntimeMethodInfo xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:x="http://www.w3.org/2001/XMLSchema" z:FactoryType="MemberInfoSerializationHolder" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/" xmlns="http://schemas.datacontract.org/2004/07/System.Reflection">
// <Name i:type="x:string" xmlns="">Exists</Name>
// <AssemblyName i:type="x:string" xmlns="">mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</AssemblyName>
// <ClassName i:type="x:string" xmlns="">System.Array</ClassName>
// <Signature i:type="x:string" xmlns="">Boolean Exists[T](T[], System.Predicate`1[T])</Signature>
// <Signature2 i:type="x:string" xmlns="">System.Boolean Exists[T](!!T[], System.Predicate`1[T])</Signature2>
// <MemberType i:type="x:int" xmlns="">8</MemberType>
// <GenericArguments i:nil="true" xmlns="" />
// </RuntimeMethodInfo>
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment