Skip to content

Instantly share code, notes, and snippets.

@urasandesu
Created May 12, 2014 11:57
Show Gist options
  • Save urasandesu/a663891006ecde5aff5e to your computer and use it in GitHub Desktop.
Save urasandesu/a663891006ecde5aff5e to your computer and use it in GitHub Desktop.
using System;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;
namespace NetDataContractSerializerTest
{
class Program
{
static void Main(string[] args)
{
// DataContractSerializer
{
var dateTimeInfo_get_Now = typeof(DateTime).GetMethod("get_Now");
var strDateTimeInfo_get_Now = default(string);
{
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());
strDateTimeInfo_get_Now = sb.ToString();
}
Console.WriteLine();
Console.WriteLine("------------------");
Console.WriteLine();
var result = default(MethodInfo);
{
var dcs = new DataContractSerializer(Type.GetType("System.Reflection.RuntimeMethodInfo"));
var sr = new StringReader(strDateTimeInfo_get_Now);
using (var xr = new XmlTextReader(sr))
{
try
{
result = (MethodInfo)dcs.ReadObject(xr);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
Console.WriteLine(result);
}
}
// <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>
//
// ------------------
//
// ISerializable 型 'System.Reflection.RuntimeMethodInfo' 内に、パラメーター (SerializationInfo、StreamingContext) を持つコンストラクターがありません。
//
// NetDataContractSerializer
{
var dateTimeInfo_get_Now = typeof(DateTime).GetMethod("get_Now");
var strDateTimeInfo_get_Now = default(string);
{
var ndcs = new NetDataContractSerializer();
var sb = new StringBuilder();
var sw = new StringWriter(sb);
using (var xw = new XmlTextWriter(sw) { Formatting = Formatting.Indented })
{
ndcs.WriteObject(xw, dateTimeInfo_get_Now);
}
Console.WriteLine(sb.ToString());
strDateTimeInfo_get_Now = sb.ToString();
}
Console.WriteLine();
Console.WriteLine("------------------");
Console.WriteLine();
var result = default(MethodInfo);
{
var ndcs = new NetDataContractSerializer();
var sr = new StringReader(strDateTimeInfo_get_Now);
using (var xr = new XmlTextReader(sr))
{
result = (MethodInfo)ndcs.ReadObject(xr);
}
Console.WriteLine(result);
}
}
// <RuntimeMethodInfo xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:x="http://www.w3.org/2001/XMLSchema" z:Id="1" z:FactoryType="MemberInfoSerializationHolder" z:Type="System.Reflection.MemberInfoSerializationHolder" z:Assembly="0" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/" xmlns="http://schemas.datacontract.org/2004/07/System.Reflection">
// <Name z:Id="2" z:Type="System.String" z:Assembly="0" xmlns="">get_Now</Name>
// <AssemblyName z:Id="3" z:Type="System.String" z:Assembly="0" xmlns="">mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</AssemblyName>
// <ClassName z:Id="4" z:Type="System.String" z:Assembly="0" xmlns="">System.DateTime</ClassName>
// <Signature z:Id="5" z:Type="System.String" z:Assembly="0" xmlns="">System.DateTime get_Now()</Signature>
// <Signature2 z:Id="6" z:Type="System.String" z:Assembly="0" xmlns="">System.DateTime get_Now()</Signature2>
// <MemberType z:Id="7" z:Type="System.Int32" z:Assembly="0" xmlns="">8</MemberType>
// <GenericArguments i:nil="true" xmlns="" />
// </RuntimeMethodInfo>
//
// ------------------
//
// System.DateTime get_Now()
//
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment