这适用于使用 .Net 4.5 的 Windows 8.1 Store 应用程序
我发现自己为我拥有的每个“对象”复制了相同的代码。 所以我正在尝试创建一种更通用的方法来以我想要的方式获取所有数据。最初一切都是<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”隐式转换为“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;
}