公开WCF中的特定字段

时间:2014-04-04 08:52:38

标签: visual-studio-2010 wcf c#-4.0

我是一个班级

[DataContract]
public class Sample 
{
   [DataMember]
   int a;
   [DataMember]
   int b;
   [DataMember]
   int c;
}

Sample sampleMethod1 ()
{
  return new Sample { a =5, b=6 }
}

Sample sampleMethod1 ()
{
  return new Sample { a =15, c=61 }
}

现在我希望当我的外部客户调用sampleMethod1时,样本类的可见字段是a而c不是b。类似于sampleMethod2

1 个答案:

答案 0 :(得分:3)

你需要这样的东西..

  [DataContract]
  [KnownType(typeof(SampleDerived1))]
  [KnownType(typeof(SampleDerived2))]
  public class Sample
  {
    [DataMember]
    public int a;
  }

  [DataContract]
  public class SampleDerived1 : Sample
  {
    [DataMember]
    public int b;
  }

  [DataContract]
  public class SampleDerived2 : Sample
  {
    [DataMember]
    public int c;
  }

你的方法应该是这样的..

Sample sampleMethod1 ()
{
  return new SampleDerived1{ a =5, b=6 }
}

Sample sampleMethod2 ()
{
  return new SampleDerived2{ a =15, c=61 }
}