我是一个班级
[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
答案 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 }
}