具有可观察集合的泛型方法
本文关键字:泛型方法 集合 观察 | 更新日期: 2023-09-27 18:33:10
这适用于使用 .Net 4.5 的 Windows 8.1 应用商店应用程序
我发现自己为每个"对象"复制相同的代码。 所以我正在尝试创建一个更通用的方法,以我想要的方式获取所有数据。 最初一切都<Typed>
到我的不同对象,但现在我正在尝试使用通用<Object>
。
public async Task<ObservableCollection<object>>selectAll()
{
var Items = await Win8StorageHelper.LoadData("person.dat", typeof(ObservableCollection<object>));
ObservableCollection<object> ItemsList = new ObservableCollection<object>();
if (typeof(Int32) == Items.GetType())
{
//Not Needed anymore
}
else
{
ItemsList = (ObservableCollection<object>)Items;
}
_list.Clear();
foreach (object item in ItemsList)
{
_list.Add(item);
}
return _list;
}
我有我的原始集合_frequencyList,现在一个新的通用集合_list:
private ObservableCollection<win8Chores.Model.databaseTables.Frequency> _frequencyList;
public ObservableCollection<Object> _list = new ObservableCollection<Object>();
我正在尝试像这样调用我的新方法:
_frequencyList = await selectAll();
但是,我收到一个错误:
无法隐式转换类型 'System.Collections.ObjectModel.ObservableCollection' to 'System.Collections.ObjectModel>observableCollection'
我需要做什么才能使此代码正常工作? 我甚至走在正确的轨道上吗? 任何帮助,不胜感激。
更新:这些是我尝试为通用示例重构的原始方法:
public async Task<ObservableCollection<Person>> selectAllPersons()
{
var personItems = await Win8StorageHelper.LoadData("person.dat", typeof(ObservableCollection<Person>));
ObservableCollection<Person> personItemsList = new ObservableCollection<Person>();
if (typeof(Int32) == personItems.GetType())
{
//Not Needed anymore
}
else
{
personItemsList = (ObservableCollection<Person>)personItems;
}
_personList.Clear();
foreach (Person item in personItemsList)
{
_personList.Add(item);
}
return _personList;
}
public async Task<ObservableCollection<Frequency>> selectAllFrequencies()
{
var frequencyItems = await Win8StorageHelper.LoadData("frequency.dat", typeof(ObservableCollection<Frequency>));
ObservableCollection<Frequency> frequencyItemsList = new ObservableCollection<Frequency>();
if (typeof(Int32) == frequencyItems.GetType())
{
//Not Needed anymore
}
else
{
frequencyItemsList = (ObservableCollection<Frequency>)frequencyItems;
}
_frequencyList.Clear();
foreach (Frequency item in frequencyItemsList)
{
_frequencyList.Add(item);
}
return _frequencyList;
}
public async Task<ObservableCollection<Chore>> selectAllChores()
{
var choreItems = await Win8StorageHelper.LoadData("chore.dat", typeof(ObservableCollection<Chore>));
ObservableCollection<Chore> choreItemsList = new ObservableCollection<Chore>();
if (typeof(Int32) == choreItems.GetType())
{
//Not Needed anymore
}
else
{
choreItemsList = (ObservableCollection<Chore>)choreItems;
}
_choreList.Clear();
foreach (Chore item in choreItemsList)
{
_choreList.Add(item);
}
return _choreList;
}
我认为您应该为此使用泛型;请参阅 C# 泛型简介
public async Task<ObservableCollection<T1>> selectAll<T1>(string filename)
{
var Items = await Win8StorageHelper.LoadData(filename, typeof(ObservableCollection<T1>));
if (Items is ObservableCollection<T1>)
{
return ((ObservableCollection<T1>)Items).ToList();
}
else
{
// empty or not the right type; depending on what the storage helper gives us
return new ObservableCollection<T1>();
}
}
调用者
var personList = selectAll<Person>("person.dat");
var frequencyList = selectAll<Person>("frequency.dat");
你需要这样的东西
public async Task<ObservableCollection<T>> selectAllItems<T>(ObservableCollection<T> _list)
{
var items = await Win8StorageHelper.LoadData(string.Format("{0}.dat",typeof(T).Name.ToLower()), typeof(ObservableCollection<T>));
_list.Clear();
if(!(items is ObservableCollection<T>))
{
return _list;
}
ObservableCollection<T> itemsList = (ObservableCollection<T>)items;
foreach (T item in itemsList)
{
_list.Add(item);
}
return _list;
}
_list
是您需要添加所有项目的参数。
然后像这样称呼它
selectAllItems(_personList);
selectAllItems(_frequencyList); //etc