C# - 代码分析2227困惑 [英] C# - Code Analysis 2227 Confusion

查看:196
本文介绍了C# - 代码分析2227困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类属性,如下所示:

I have a class property that looks as follows:

public List<Recipe> RecipeList
{
    get { return this._recipeList; }

    set
    {
        this._recipeList = value;
        OnPropertyChanged("RecipeList");
    }
}

在另一种方法我有一个引用的财产以下以上。

In another method I have the following which references the property above.

private void RecipeSearch()
{
            this.RecipeList = RecipeManagerService.SearchByUnit(SearchCriteria)
                               .Where(recipe => recipe.IsApproved == true && !recipe.IsHidden).ToList();
}



代码分析是发出CA 2227警告:更改RecipeList被只读通过删除二传手。谁能告诉我,为什么

Code Analysis is issuing a CA 2227 warning: Change RecipeList to be read-only by removing the setter. Could anyone tell me why?

推荐答案

添加一个公共setter方法上的列表< T> 对象是危险的。您可以消除通过这个警告你的二传手私人:

Adding a public setter on a List<T> object is dangerous. You can eliminate this warning by making your setter private:

public List<Recipe> RecipeList
{
    get { return this._recipeList; }

    private set
    {
        this._recipeList = value;
        OnPropertyChanged("RecipeList");
    }
}

这仍然可以让你的类来更改这个方法,但没有外部源。

This will still allow your class to change this method, but no external source.

这篇关于C# - 代码分析2227困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆