簡體   English   中英

如何用C#動態集合中的默認值替換null?

[英]How to replace null with the default Value in C# dynamic Collection?

我有一個模型MobileList的ObservableCollection。

在集合中,大多數值都為空。

在這里,我想將null轉換為對應的DataType。

例如:

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

注意:Collection是動態的,我無法檢查null是否屬於Int或float或bool或任何東西。 請建議使用通用轉換功能,以便在所有地方使用它。 請考慮對應的模型Property 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; }
}

我建議您使用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();
            }
        }
    }

您可以使用反射來獲取每個屬性的數據類型並設置默認值。 但這肯定不是最干凈的解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM