WCF中的通用列表初始化为Null [英] Generic List in WCF intialized to Null

查看:105
本文介绍了WCF中的通用列表初始化为Null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,



这非常重要!!



我正在开发网络服务到WCF应用程序,其中我有许多以下列模式定义的类。



Guys,

This is very Important !!

I am working on the web service to WCF application in which I have many classes defined in the following pattern.

public class Foo
{
        private string _foo1;
        private string _foo2;

       public string Property1
       {
           get { return _foo1; }
           set { _foo1= value; }
       }

       private string _chkSubsidiaryCompany;
       public string Property2
       {
           get { return _foo2; }
           set { _foo2= value; }
       }

       private List<string> _foo3=new List<string>();
       public List<string> Perperty3
       {
           get { return _foo3; }
           set { _foo3= value; }
       }      
}





因此,根据WCF 4.0,我们不需要设置每个属性/类DataMember / DataContract分别。



当我在客户端使用Foo对象时,列表没有使用空容器初始化,但是'Null'



由于我有庞大的代码库,我不想用DataContract / DataMember来装饰实体。



关注:如何将客户端的null初始化集合转换为空容器?

我只想删除分配给客户端任何集合的Null序列化后的一面。






我已经尝试过的东西。



1.在实体构造函数中初始化colelction。

2.在客户端添加Web引用时将服务配置更改为System.Collection.Generic List。



在时间的兴趣,请帮助



提前致谢。



快乐编码

推荐答案

大家好,

在属性中添加空检查对我有用。我不明白同样的代码以前没有达到我的目的,但它最终最终完成。



无论如何都要回复。



Hi All,
Adding a null check in the property worked for me. I don't understand somehow the same code was not serving my purpose before but it did eventually in the end.

Thanks anyways for the responses.

private List<string> _foo1 = new List<string>();
public List<string> MyProperty
{
    get { return _foo1; }
    set { if (value != null) _foo1= value; }
}





快乐编码


为什么不指定数据合同和数据成员?这不是什么麻烦。



您还可以删除在合同实现文件中返回null的任何try catch。



我还要将_foo1和foo2初始化为某个值或string.Empty
Why don't you just specify the data contracts and data members? It ain't much of a hassle.

You could also remove any try catches that return null in the contract implementation file.

I'd also initialize the _foo1 and foo2 to some value or string.Empty


这篇关于WCF中的通用列表初始化为Null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆