猿问

Net Core:解决“添加一种方法来打破此属性访问器的递归”

我正在尝试创建一个简单的类。ColumnSort 成员是逗号分隔文本“Car,Book,Food”中的项目列表。

ColumnSortList 创建一个列表

  1. 食物

C# 和 SonarQube 提到了诸如 Error 之类的项目

Get:添加一种方法来打破此属性访问器的递归。

Set:在此属性集访问器声明中使用“value”参数

我将如何解决这些问题以使警告/错误(在 SonarQube 中)消失?也愿意让代码更高效。

注意:columnSortList 纯粹应该是 ColumnSort 字符串的只读计算字段。

public class PageModel

{

    public int Page { get; set; }

    public int Limit { get; set; }

    public string ColumnSort { get; set; }


    public IEnumerable<string> columnSortList

    {

        get

        {

            return columnSortList;

        }

        set

        {

            if (ColumnSort == null)

            {

                columnSortList = null;

            }

            else

            {

                columnSortList = ColumnSort.Split(',')

                             .Select(x => x.Trim())

                             .Where(x => !string.IsNullOrWhiteSpace(x))

                             .AsEnumerable();

            }

        }

    }


米琪卡哇伊
浏览 110回答 2
2回答

慕盖茨4494581

如果columnSortList旨在纯粹只读,从 计算ColumnSort,那么您set根本不应该有方法。所有的逻辑都应该get像这样:public IEnumerable<string> columnSortList{&nbsp; &nbsp; get&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (ColumnSort == null)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Enumerable.Empty<string>();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return ColumnSort.Split(',')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.Select(x => x.Trim())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.Where(x => !string.IsNullOrWhiteSpace(x))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.AsEnumerable();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

Smart猫小萌

你的 getter 正在返回自身,这是你无法做到的,而你的 setter 正在设置自身,这是你也无法做到的。这似乎就是你想要的:public IEnumerable<string> columnSortList{&nbsp; &nbsp; get&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (ColumSort == null)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new List<string>();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return ColumnSort.Split(',')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.Select(x => x.Trim())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.Where(x => !string.IsNullOrWhiteSpace(x))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.AsEnumerable();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
随时随地看视频慕课网APP
我要回答