From: Carl Yeksigian Date: Thu, 6 Jun 2013 11:52:42 +0000 (-0400) Subject: THRIFT-1959: Add Union TMemoryBuffer support X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=548244f0495898492616f520fec7b08c267574d9;p=common%2Fthrift.git THRIFT-1959: Add Union TMemoryBuffer support Client: csharp Patch: carl --- 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;