THRIFT-1694 Re-Enable serialization for WP7 Silverlight 
Patch: Jens Geyer


git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1387715 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/compiler/cpp/src/generate/t_csharp_generator.cc b/compiler/cpp/src/generate/t_csharp_generator.cc
index 02db2c1..d57e534 100644
--- a/compiler/cpp/src/generate/t_csharp_generator.cc
+++ b/compiler/cpp/src/generate/t_csharp_generator.cc
@@ -50,6 +50,12 @@
       iter = parsed_options.find("async");
       async_ctp_ = (iter != parsed_options.end());
 
+      iter = parsed_options.find("serial");
+      serialize_ = (iter != parsed_options.end());
+	  if (serialize_) {
+		  wcf_namespace_ = iter->second;  // since there can be only one namespace
+	  }
+
 	  iter = parsed_options.find("wcf");
 	  wcf_ = (iter != parsed_options.end());
 	  if (wcf_) {
@@ -142,6 +148,7 @@
     std::ofstream f_service_;
     std::string namespace_dir_;
     bool async_ctp_;
+    bool serialize_;
     bool wcf_;
     std::string wcf_namespace_;
 };
@@ -192,7 +199,7 @@
 	(async_ctp_ ? "using System.Threading.Tasks;\n" : "") +
     "using Thrift;\n" +
     "using Thrift.Collections;\n" +
-        (wcf_ ? "using System.ServiceModel;\n" : "") +
+    (wcf_ ? "//using System.ServiceModel;\n" : "") +
     "using System.Runtime.Serialization;\n";
 }
 
@@ -444,10 +451,10 @@
 
   indent(out) << "#if !SILVERLIGHT" << endl;
   indent(out) << "[Serializable]" << endl; 
-  if (wcf_ &&!is_exception) {
+  indent(out) << "#endif" << endl;
+  if ((serialize_||wcf_) &&!is_exception) {
 	  indent(out) << "[DataContract(Namespace=\"" << wcf_namespace_ << "\")]" << endl; // do not make exception classes directly WCF serializable, we provide a seperate "fault" for that
   }
-  indent(out) << "#endif" << endl;
   bool is_final = (tstruct->annotations_.find("final") != tstruct->annotations_.end());
  
   indent(out) << "public " << (is_final ? "sealed " : "") << "partial class " << tstruct->get_name() << " : ";
@@ -482,11 +489,12 @@
       indent() << "public Isset __isset;" << endl <<
       indent() << "#if !SILVERLIGHT" << endl <<
       indent() << "[Serializable]" << endl;
-      if (wcf_) {
+    out <<
+      indent() << "#endif" << endl;
+    if ((serialize_||wcf_)) {
 	  indent(out) << "[DataContract]" << endl;
       }
     out <<
-      indent() << "#endif" << endl <<
       indent() << "public struct Isset {" << endl;
     indent_up();
     for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
@@ -525,7 +533,7 @@
   out << endl;
 
   // generate a corresponding WCF fault to wrap the exception
-  if(wcf_ && is_exception) {
+  if((serialize_||wcf_) && is_exception) {
 	  generate_csharp_wcffault(out, tstruct);
   }
 
@@ -539,8 +547,8 @@
   out << endl;
   indent(out) << "#if !SILVERLIGHT" << endl;
   indent(out) << "[Serializable]" << endl;
-  indent(out) << "[DataContract]" << endl;
   indent(out) << "#endif" << endl;
+  indent(out) << "[DataContract]" << endl;
   bool is_final = (tstruct->annotations_.find("final") != tstruct->annotations_.end());
 
   indent(out) << "public " << (is_final ? "sealed " : "") << "partial class " << tstruct->get_name() << "Fault" << endl;
@@ -1761,7 +1769,7 @@
     generate_csharp_property(out, tfield, isPublic, generateIsset, "_");
 }
 void t_csharp_generator::generate_csharp_property(ofstream& out, t_field* tfield, bool isPublic, bool generateIsset, std::string fieldPrefix) {
-	if(wcf_ && isPublic) {
+	if((serialize_||wcf_) && isPublic) {
 		indent(out) << "[DataMember]" << endl;
 	}
     indent(out) << (isPublic ? "public " : "private ") << type_name(tfield->get_type())
@@ -2027,5 +2035,6 @@
 THRIFT_REGISTER_GENERATOR(csharp, "C#",
 "    async:           Adds Async CTP support.\n"
 "    wcf:             Adds bindings for WCF to generated classes.\n"
+"    serial:          Add serialization support to generated classes.\n"
 )
 
diff --git a/lib/csharp/src/Collections/THashSet.cs b/lib/csharp/src/Collections/THashSet.cs
index e2fc8b5..b71a8d2 100644
--- a/lib/csharp/src/Collections/THashSet.cs
+++ b/lib/csharp/src/Collections/THashSet.cs
@@ -21,14 +21,23 @@
 using System.Collections;
 using System.Collections.Generic;
 
+#if SILVERLIGHT
+using System.Runtime.Serialization;
+#endif
+
 namespace Thrift.Collections
 {
-#if !SILVERLIGHT
-     [Serializable]
+#if SILVERLIGHT
+    [DataContract]
+#else
+	[Serializable]
 #endif
 	public class THashSet<T> : ICollection<T>
 	{
 #if NET_2_0 || SILVERLIGHT
+#if SILVERLIGHT
+        [DataMember]
+#endif
         TDictSet<T> set = new TDictSet<T>();
 #else
 		HashSet<T> set = new HashSet<T>();
@@ -79,8 +88,14 @@
 		}
 
 #if NET_2_0 || SILVERLIGHT
+#if SILVERLIGHT
+        [DataContract]
+#endif
         private class TDictSet<V> : ICollection<V>
 		{
+#if SILVERLIGHT
+            [DataMember]
+#endif
 			Dictionary<V, TDictSet<V>> dict = new Dictionary<V, TDictSet<V>>();
 
 			public int Count