title: WCF: How to use an abstract class as DataContract
body: <p>I want to use an abstract class to define it as DataContract for WCF. The following example shows how I define my abstract class <code>UpperClass</code>.</p>
  
  <pre><code>[DataContract(IsReference = true)]
  [KnownType(typeof(SubClass1))]
  [KnownType(typeof(SubClass2))]
  abstract public class UpperClass
  {
      abstract public void update();
  }
  </code></pre>
  
  <p><code>SubClass1</code> and <code>SubClass2</code> inherit from <code>UpperClass</code>. Actually, they only share 1 method named <code>update()</code>, which is only used internally (not a DataMember). The following code shows the implementation of <code>SubClass1</code> and <code>SubClass2</code>:</p>
  
  <pre><code>[DataContract]
  public class SubClass1 : UpperClass
  {
      private int[] _val1;
      private int[] _val2;
  
      //internal constructor:
      public SubClass1()
      {
          _val1 = new int[2];
          _val2 = new int[2];
  
          //initialize values of _val1 and _val2 ...
      }
  
      //internal update method
      public override void update()
      {
          //here comes update formulas...
      }
  
      //enable simple access from WCF client:
      [DataMember]
      public int[] val1
      {
          get { return _val1; }
          set { _val1 = value; }
      }
  
      [DataMember]
      public int[] val2
      {
          get { return _val2; }
          set { _val2 = value; }
      }
  }
  
  
  
  
  
  [DataContract]
  public class SubClass2 : UpperClass
  {
      private int[] _value1;
      private int[] _value2;
  
      //internal constructor:
      public SubClass2()
      {
          _value1 = new int[12];
          _value2 = new int[12];
      }
  
      // ...
  </code></pre>
  
  <p>On the server side, I supply my survice using the following code, which is defined as [OperationContract(IsInitiating = true, IsTerminating = false)] in its Interface-class.</p>
  
  <pre><code>public UpperClass init(string s)
  {
      if (s.Equals("SubClass1"))
          obj = new SubClass1();
      else if (s.Equals("SubClass2"))
          obj = new SubClass2();
  
      return obj;
  }
  </code></pre>
  
  <p>And on the client side, I consume (or initialize) my service using:</p>
  
  <pre><code>UpperClass DatSet = proxy.init("SubClass1");
  </code></pre>
  
  <p>When I debug to the end of this line, the content of <code>DatSet</code> looks very strange:</p>
  
  <blockquote>
    <p>{MyExternalLayer.IMyProcessingLayer_proxy.SubClass1}
        [MyExternalLayer.IMyProcessingLayer_proxy.SubClass1]: {MyExternalLayer.IMyProcessingLayer_proxy.SubClass1}
        ExtensionData: {System.Runtime.Serialization.ExtensionDataObject}
        extensionDataField: {System.Runtime.Serialization.ExtensionDataObject}
        PropertyChanged: null</p>
  </blockquote>