2009-07-07 102 views
10

WCF的新功能。WCF DataContract與DataContract接口

DataContact類可以從Interface繼承嗎?

如:

[DataContract(Namespace = ...........)] 
public class VesselSequence : IVesselSequence 
{ 

    [DataMember] 
    public int AllocationId { get; set; } 

    [DataMember] 
    public string ScenarioName { get; set; } 
} 

interface VesselSequence : IVesselSequence 
{ 
    public int AllocationId { get; set; } 
    public string ScenarioName { get; set; } 
} 

回答

3

肯定是可以的,但要記住,如果你正在返回你必須定義KnownTypes的反序列化引擎屬性的接口類型,因此它可能在另一端反序列化發送接口。

+1

馬丁您好..有沒有什麼好的例子在網絡的某個地方? – Sreedhar 2009-07-07 07:00:20

5

你可以這樣做:

[DataContract(Namespace = ...........)] 
public class VesselSequence : IVesselSequence 
{ 
    [DataMember] 
    public int AllocationId { get; set; } 
    [DataMember] 
    public string ScenarioName { get; set; } 
} 

interface IVesselSequence 
{ 
    int AllocationId { get; set; } 
    string ScenarioName { get; set; } 
} 

你不能做到這一點,黯然道:

public class VesselSequence : IVesselSequence 
{ 
    public int AllocationId { get; set; } 
    public string ScenarioName { get; set; } 
} 

[DataContract(Namespace = ...........)] 
interface IVesselSequence 
{ 
    [DataMember] 
    int AllocationId { get; set; } 
    [DataMember] 
    string ScenarioName { get; set; } 
}