0

这适用于使用 .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;
    }
4

2 回答 2

2

我认为您应该为此使用泛型;请参阅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");
于 2013-09-29T15:41:40.030 回答
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
于 2013-09-29T16:16:49.003 回答