From 548244f0495898492616f520fec7b08c267574d9 Mon Sep 17 00:00:00 2001 From: Carl Yeksigian Date: Thu, 6 Jun 2013 07:52:42 -0400 Subject: [PATCH] THRIFT-1959: Add Union TMemoryBuffer support Client: csharp Patch: carl --- lib/csharp/src/Transport/TMemoryBuffer.cs | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/lib/csharp/src/Transport/TMemoryBuffer.cs b/lib/csharp/src/Transport/TMemoryBuffer.cs index c6e72f1c..ca31fee3 100644 --- a/lib/csharp/src/Transport/TMemoryBuffer.cs +++ b/lib/csharp/src/Transport/TMemoryBuffer.cs @@ -17,6 +17,7 @@ * under the License. */ +using System; using System.IO; using System.Reflection; using Thrift.Protocol; @@ -59,7 +60,7 @@ namespace Thrift.Transport { get { return true; } } - public static byte[] Serialize(TBase s) { + public static byte[] Serialize(TAbstractBase s) { var t = new TMemoryBuffer(); var p = new TBinaryProtocol(t); @@ -68,12 +69,18 @@ namespace Thrift.Transport { return t.GetBuffer(); } - public static T DeSerialize(byte[] buf) where T : TBase, new() { - var t = new T(); - var trans = new TMemoryBuffer(buf); - var p = new TBinaryProtocol(trans); - t.Read(p); - return t; + public static T DeSerialize(byte[] buf) where T : TAbstractBase { + var trans = new TMemoryBuffer(buf); + var p = new TBinaryProtocol(trans); + if (typeof (TBase).IsAssignableFrom(typeof (T))) { + var method = typeof (T).GetMethod("Read", BindingFlags.Instance | BindingFlags.Public); + var t = Activator.CreateInstance(); + method.Invoke(t, new object[] {p}); + return t; + } else { + var method = typeof (T).GetMethod("Read", BindingFlags.Static | BindingFlags.Public); + return (T) method.Invoke(null, new object[] {p}); + } } private bool _IsDisposed; -- 2.17.1