ReadOnlyCollection C#

时间:2015-10-12 08:22:13

标签: c#

我有分类模型

public class Category : BindableBase
{
    /// <summary>
    /// Initializes a new instance of the Category class.
    /// </summary>
    public Category()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    private int _CategoryId = -1;

    public int CategoryId
    {
        get { return _CategoryId; }
        set { _CategoryId = value; }
    }

    private string _Name;

    public string Name
    {
        get { return _Name.Trim(); }
        set { _Name = value; }
    }

    private Type _DataType;

    public Type DataType
    {
        get { return _DataType; }
        set { _DataType = value; }
    }


    }

    public List<Category> Categories { get; set; }
}

在另一个主要班级

      private static ReadOnlyCollection<Category> _CategoryList;

    public static ReadOnlyCollection<Category> CategoryList
    {
        get { return _CategoryList; }
        set { _CategoryList = value; }
    }

我从数据表填充CategoryList。

private static List<Category> GenerateCategoryList(DataSet ds)
    {
        List<Category> categoryList = new List<Category>();

        foreach (DataRow row in ds.Tables["Category"].Rows)
        {
            Category category = new Category();
            category.Name = row["Name"].ToString();

            int categoryId;
            if (int.TryParse(row["Category_Id"].ToString(), out categoryId))
                category.CategoryId = categoryId;

            int MappingId;
            if (int.TryParse(row["Category_Id_0"].ToString(), out MappingId))
                category.MappingId = MappingId;

            int RootId;
            if (int.TryParse(row["Properties_Id"].ToString(), out RootId))
                category.RootId = RootId;

            categoryList.Add(category);
        }

        return categoryList;
    }

完成上述方法后,我在两种方法中使用CategoryList

var GenCatTask = await Task.Factory.StartNew(async () => { await GetCatgeoryFromAdapter(); });
        await GenCatTask.ContinueWith(async t => { MainTreeNoProperty = await GenerateCategory(CategoryList, null); });===>CAll 1
        await GenCatTask.ContinueWith(async t => { MainTree = await GenerateCategory(CategoryList, PropertyList); }); ====>CALL2

private static async Task<List<Category>> GenerateCategory(ReadOnlyCollection<Category> Categories, ReadOnlyCollection<PropertyNode> Properties)
    {
        try
        {
            List<Category> TreeList = new List<Category>();
            List<Category> categories = Categories.Where(i => i.RootId == 0).ToList();
            categories.ForEach(category =>
            {
                TreeList.Add(category);
            });

            LogEntries(LogType.Info, "Retrieving Category List ", "ModuleController", 75);
            if (categories.Count > 0)
                SetCategories(TreeList, Categories, Properties);
            LogEntries(LogType.Info, "Completed Retrieving Category List ", "ModuleController", 100);
            return TreeList;
        }
        catch (Exception ex)
        {
            LogEntries(LogType.Error, "Error- While GenerateCategory", "ModuleController", 100, ex);
            throw;
        }
    }

调用上面的代码时。呼叫中发生的变化--- 1反映了CategoryList及其影响呼叫2。如何将CategoryList设置为ready Only,我不应该更改categoryList中的集合。但是集合正在受到影响。

During the call 1 the Categories inside the CategoryList is Null

After the first call the Categories are getting added . I dont want this to happen

0 个答案:

没有答案