我想知道是否有一种方法可以通过编程方式将集合类型配置为通用列表而不是客户端上的任何数组。我知道如果我们添加服务引用并转到高级设置,我们可以在那里指定。但是项目的设计方式是我们为合同分别进行组装,并由服务器和客户端引用,并且模型类型在服务和客户端使用的同一项目中定义。因此,我们不是添加服务引用,而是通过以编程方式定义绑定和端点来完成代码中的所有操作。我想知道这是否可以在代码中轻松实现而无需编写代码将数组转换为客户端的通用列表。
谢谢, 阿贾伊
答案 0 :(得分:1)
这是由于DataContractSerializer将GenericLists转换为数组。为了处理这个问题,我不得不拦截对象的反序列化过程并将它们转换回列表。我提供了我的类定义和部分类定义,使我的答案更容易理解。
班级定义:
public partial class provider
{
public provider()
{
this.provider_contracts = new HashSet<provider_contracts>();
this.provider_clinics = new HashSet<provider_clinics>();
this.provider_custom_field_validations = new HashSet<provider_custom_field_validations>();
this.provider_directory_languages = new HashSet<provider_directory_languages>();
this.provider_directory_services = new HashSet<provider_directory_services>();
this.provider_employees = new HashSet<provider_employees>();
this.provider_field_exceptions = new HashSet<provider_field_exceptions>();
this.provider_languages = new HashSet<provider_languages>();
this.provider_required_fields = new HashSet<provider_required_fields>();
this.provider_users = new HashSet<provider_users>();
this.user_privileges = new HashSet<user_privileges>();
}
public virtual ICollection<provider_contracts> provider_contracts { get; set; }
public virtual ICollection<provider_clinics> provider_clinics { get; set; }
public virtual ICollection<provider_custom_field_validations> provider_custom_field_validations { get; set; }
public virtual ICollection<provider_directory_languages> provider_directory_languages { get; set; }
public virtual ICollection<provider_directory_services> provider_directory_services { get; set; }
public virtual ICollection<provider_employees> provider_employees { get; set; }
public virtual ICollection<provider_field_exceptions> provider_field_exceptions { get; set; }
public virtual ICollection<provider_languages> provider_languages { get; set; }
public virtual provider_options provider_options { get; set; }
public virtual ICollection<provider_required_fields> provider_required_fields { get; set; }
public virtual ICollection<provider_users> provider_users { get; set; }
public virtual ICollection<user_privileges> user_privileges { get; set; }
}
我的Partial类来处理这个问题:
public partial class provider
{
[OnDeserialized()]
internal void OnDeserializedMethod(StreamingContext context)
{
if (provider_contracts != null) provider_contracts = provider_contracts.ToList();
if (provider_clinics != null) provider_clinics = provider_clinics.ToList();
if (provider_custom_field_validations != null) provider_custom_field_validations = provider_custom_field_validations.ToList();
if (provider_directory_languages != null) provider_directory_languages = provider_directory_languages.ToList();
if (provider_directory_services != null) provider_directory_services = provider_directory_services.ToList();
if (provider_employees != null) provider_employees = provider_employees.ToList();
if (provider_field_exceptions != null) provider_field_exceptions = provider_field_exceptions.ToList();
if (provider_languages != null) provider_languages = provider_languages.ToList();
if (provider_required_fields != null) provider_required_fields = provider_required_fields.ToList();
if (provider_users != null) provider_users = provider_users.ToList();
if (user_privileges != null) user_privileges = user_privileges.ToList();
}
}
有用的资源:http://yanivh.wordpress.com/2008/11/21/datacontractserializer-and-generic-lists/
我也在MSDN论坛上回答了这个问题,并在下面添加了一个链接 “http://social.msdn.microsoft.com/Forums/vstudio/en-US/5ccb534c-e310-48d4-8345-160c06568220/collection-types-when-using-channelfactory-without-servicereference?forum=wcf”