2013-06-24 116 views
0

接口我有一个接口,称为IDeviceConfig如下:序列化WCF

[KnownType(typeof(Device))] 
[KnownType(typeof(DeviceGroup))] 
[DataContract()] 
public interface IDeviceConfig 
{ 
    [DataMember()] 
    string Name { get; set; } 

    [DataMember()] 
    List<Property> Properties { get; set; } 

    ActionResult PerformAction(string ActionId); 
} 

界面将通过两个类来实现,具体如下:

public class Device : IDeviceConfig 
{ 
... 
} 

public class DeviceGroup : IDeviceConfig 
{ 
... 
} 

在我的WCF服务,我需要返回一个IDeviceConfigs列表;当我用KnownType装饰界面时,Visual Studio抱怨说属性'KnownType'在这种声明类型上是无效的。

有人可以请解释如何返回IDeviceconfigs的列表?

+0

是'Device'和'DeviceGroup'服务或数据的合同?看起来你正在混合一些东西。 – Tim

+0

设备和设备组是数据合同。 –

回答

2

如果您查看KnownTypeAttribute的定义,则不能应用于接口。

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, Inherited = true, AllowMultiple = true)] 
public sealed class KnownTypeAttribute : Attribute 

“已知类型只能与类和结构关联,而不能与接口关联”。从这里: http://msdn.microsoft.com/en-us/library/ms730167.aspx