这适用于使用.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'到 'System.Collections.ObjectModel&GT;的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;
}
答案 0 :(得分:2)
我认为你应该使用泛型;见Introduction to C# Generics
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");
答案 1 :(得分:1)
你需要这样的东西
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