在运行时提供ServiceKnownType?

我在合同中使用了超过100个ServiceKnownType的WCF接口,如下所示:

[ServiceKnownType(typeof(RowUser))] [ServiceKnownType(typeof(RowRegion))] [ServiceKnownType(typeof(RowDocument))] [... loads more ...] [ServiceContract(SessionMode = SessionMode.Required)] public interface IServiceBrowse : IDisposable { [OperationContract] void Insert(Row satz); } 

有没有办法在运行时提供这些ServiceKnownTypes?
将所有这些ServiceKnownTypes添加到源代码中不仅容易出错且繁琐,而且我的程序集以我不喜欢的方式绑定在一起(我希望能够将这些类型提取到子组件中以将它们分离,但不能,因为服务需要列出所有已知的类型)。

就在这里。

ServiceKnownTypeAttribute允许您将方法名称指定为第一个参数,后跟包含实现该方法的System.Type的参数。

指定的方法必须是static和public,并且返回类型为IEnumerable。

 [ServiceKnownType("RegisterKnownTypes", typeof(Services))] public class Services : IServices { static public IEnumerable RegisterKnownTypes(ICustomAttributeProvider provider) { } } 

另见http://msdn.microsoft.com/en-us/library/system.servicemodel.serviceknowntypeattribute.aspx