【问题标题】:How to replace null with the default Value in C# dynamic Collection?如何用 C# 动态集合中的默认值替换 null?
【发布时间】:2016-04-15 17:00:46
【问题描述】:

我有一个模型 MobileList 的 ObservableCollection。

在集合中,大多数值都为空值。

这里我想将null转换为对应的DataType。

例如:

  • String obj = string.Empty;
  • int obj = new int();
  • DateTime obj = new DateTime();

注意:Collection 是动态的,我无法检查 null 是否为 属于 Int 或 float 或 bool 或任何东西。请建议 通用转换功能在所有地方使用它。请考虑 对应的模型属性DataType,基于我们的Property 必须将null更改为相应的DataType

public class Mobile
{
    private ObservableCollection<MobileModel> _mobileList;
    public ObservableCollection<MobileModel> MobileList
    {
        get { return _mobileList; }
        set { _mobileList = value;}
    }

    public Mobile()
    {
        ObservableCollection<MobileModel> mList = new ObservableCollection<MobileModel>();
        ObservableCollection<MobileModelInfo> modList = new ObservableCollection<MobileModelInfo>();
        MobileModel mob = new MobileModel();

        modList.Clear();
        mob.Brand = "Apple";
        modList.Add(new MobileModelInfo { Name = "iPhone 4", Catagory = "Smart Phone", Year = Convert.ToDateTime("12/18/2011"), Version = null });
        modList.Add(new MobileModelInfo { Name = "iPhone 5", Catagory = null, Year = Convert.ToDateTime("07/11/2013"), Version = 1.0 });
        modList.Add(new MobileModelInfo { Name = "iPhone 6", Catagory = "Premium Smart Phone", Year = null, Version = 1.0 });
        mob.Model = new ObservableCollection<MobileModelInfo>(modList);
        mob.OS = "IOS";
        mList.Add(mob);

        mob = new MobileModel();
        modList.Clear();
        mob.Brand = "Samsung";
        modList.Add(new MobileModelInfo { Name = "S4", Catagory = "Smart Phone", Year = Convert.ToDateTime("04/05/2011"), Version = 1.0 });
        modList.Add(new MobileModelInfo { Name = "S5", Catagory = "Smart Phone", Year = null, Version = 1.0 });
        modList.Add(new MobileModelInfo { Name = "S6", Catagory = "null", Year = Convert.ToDateTime("01/05/2011"), Version = null });
        mob.Model = new ObservableCollection<MobileModelInfo>(modList);
        mob.OS = "Android";
        mList.Add(mob);

        mob = new MobileModel();
        modList.Clear();
        mob.Brand = "MicroSoft";
        modList.Add(new MobileModelInfo { Name = "Lumina 9900", Catagory = "Phone", Year = null, Version = null });
        modList.Add(new MobileModelInfo { Name = "Opera X220", Catagory = "Smart Phone", Year = Convert.ToDateTime("02/04/2013"), Version = null });
        mob.Model = new ObservableCollection<MobileModelInfo>(modList);
        mob.OS = "Windows";
        mList.Add(mob);

        mob = new MobileModel();
        modList.Clear();
        mob.Brand = "Sony Ericssion";
        modList.Add(new MobileModelInfo { Name = "S4", Catagory = "Smart Phone", Year = Convert.ToDateTime("01/05/2011"), Version = 1.0 });
        modList.Add(new MobileModelInfo { Name = "S5", Catagory = "Smart Phone", Year = Convert.ToDateTime("08/05/2013"), Version = 1.0 });
        modList.Add(new MobileModelInfo { Name = "S6", Catagory = "Ultra Smart Phone", Year = null, Version = null });
        mob.Model = new ObservableCollection<MobileModelInfo>(modList);
        mob.OS = "Android";
        mList.Add(mob);

        MobileList = new ObservableCollection<MobileModel>(mList);

        MakeDefaultforNull(MobileList);



    }


    public void MakeDefaultforNull(ObservableCollection<MobileModel> Source)
    {

    }

}

public class MobileModel
{
    private string _brand = string.Empty;
    private ObservableCollection<MobileModelInfo> _model = new ObservableCollection<MobileModelInfo>();
    private string _os = string.Empty;

    public string Brand
    {
        get { return _brand; }
        set { _brand = value; }
    }
    public ObservableCollection<MobileModelInfo> Model
    {
        get { return _model; }
        set { _model = value; }
    }

    public string OS
    {
        get { return _os; }
        set { _os = value; }
    }
}

public class MobileModelInfo
{
    public string Name { get; set; }
    public string Catagory { get; set; }
    public DateTime? Year { get; set; }
    public double? Version { get; set; }
}

【问题讨论】:

    标签: c# null type-conversion


    【解决方案1】:

    您可以使用反射来获取每个属性的数据类型并设置默认值。但这肯定不是最干净的解决方案。

    【讨论】:

      【解决方案2】:

      我建议你在课堂上使用setter 处理null,如下所示:

        public class MobileModel 
          {
              private string _brand = string.Empty;
              private List<MobileModelInfo> _model = new List<MobileModelInfo>();
              private string _os = string.Empty;
      
              public string Brand
              {
                  get { return _brand; }
                  set {
                      if (value == null) { _brand = String.Empty; } //You know the type of the variable so you can easily set its default value.
                      else { _brand = value; } OnPropertyChanged();
                  }
              }
          }
      

      【讨论】:

      • 在我的主项目中,我有 100 多个属性。所以,我无法设置 if 条件来检查值是否为空。因此,我需要一个通用函数来将 null 替换为默认值。
      猜你喜欢
      • 2012-09-04
      • 1970-01-01
      • 1970-01-01
      • 2017-01-23
      • 2014-12-26
      • 1970-01-01
      • 2017-11-01
      • 1970-01-01
      • 2021-11-08
      相关资源
      最近更新 更多