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 c6e72f1..ca31fee 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 @@
 			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 @@
 			return t.GetBuffer();
 		}
 
-		public static T DeSerialize<T>(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<T>(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<T>();
+				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;