我有一个Model MobileList的ObservableCollection。
在Collection中,大多数值都保留null。
这里我想将null转换为Corresponding DataType。
对于Exmaple:
String obj = string.Empty;
int obj = new int();
DateTime obj = new DateTime();
注意:Collection是动态的,我无法检查null是否为 属于Int或浮动或bool或任何东西。请建议 通用转换功能可在所有地方使用它。请考虑一下 对应的模型属性DataType,基于我们的属性 必须将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; }
}
答案 0 :(得分:0)
我建议您使用null
在课堂上处理setter
,如下所示:
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();
}
}
}
答案 1 :(得分:0)
您可以使用反射来获取每个属性的数据类型并设置默认值。但这肯定不是最干净的解决方案。