C# 通用列表属性设置/添加行为

发布于 2024-12-11 05:47:47 字数 744 浏览 1 评论 0原文

我想在 ASP.NET 用户控件的视图状态中存储通用列表,因此我有以下代码:

    protected List<MoodleCourse> MoodleCoursesCreated
    {
        get
        {
            if (ViewState["MoodleCoursesCreated"] == null)
            {
                return new List<MoodleCourse>();
            }
            else
            {
                return (List<MoodleCourse>)ViewState["MoodleCoursesCreated"];
            }
        }
        set
        {
            ViewState["MoodleCoursesCreated"] = value;
        }
    }

因此,要将项目添加到列表中,我调用 MoodleCoursesCreated.Add(new MoodleCourse);

然而,似乎我必须执行 MoodleCourseCreated = MoodleCourseCreated 来激活设置器,以便列表实际上存储在视图状态中。我怀疑有一种更优雅的方法来实现这一点,有人有什么建议吗?干杯

I want to store a generic list in the viewstate in an ASP.NET user control, so I have the following code:

    protected List<MoodleCourse> MoodleCoursesCreated
    {
        get
        {
            if (ViewState["MoodleCoursesCreated"] == null)
            {
                return new List<MoodleCourse>();
            }
            else
            {
                return (List<MoodleCourse>)ViewState["MoodleCoursesCreated"];
            }
        }
        set
        {
            ViewState["MoodleCoursesCreated"] = value;
        }
    }

So to add an item to the list, I call MoodleCoursesCreated.Add(new MoodleCourse);

However it seems then I must do MoodleCourseCreated = MoodleCourseCreated to activate the setter so the list actually gets stored in the viewstate. I suspect there is a more elegant way to achieve this, does anyone have any suggestions? Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小嗲 2024-12-18 05:47:47

调用 .Add() 你永远不会触发设置器。我想,你根本不需要二传手。相反,重写 Add() 方法或创建自己的 AddMoodleCourse(MoodleCourse mudleCourse):

protected void AddMoodleCourse(MoodleCourse moodleCourse)
{
    var courses = ViewState["MoodleCoursesCreated"] as List<MoodleCourse>;
    if (courses == null)
    {
         courses = new List<MoodleCourse>();
         ViewState["MoodleCoursesCreated"] = courses;
    }
    courses.Add(moodleCourse);
}

然后调用 MoodleCoursesCreated.AddMoodleCourse(new MoodleCourse);

Calling .Add() you never trigger the setter. I guess, you don't need setter at all. Instead, override Add() method or create your own AddMoodleCourse(MoodleCourse moodleCourse):

protected void AddMoodleCourse(MoodleCourse moodleCourse)
{
    var courses = ViewState["MoodleCoursesCreated"] as List<MoodleCourse>;
    if (courses == null)
    {
         courses = new List<MoodleCourse>();
         ViewState["MoodleCoursesCreated"] = courses;
    }
    courses.Add(moodleCourse);
}

and now call MoodleCoursesCreated.AddMoodleCourse(new MoodleCourse);

那小子欠揍 2024-12-18 05:47:47
protected List<MoodleCourse> MoodleCoursesCreated
{
    get
    {
        if (ViewState["MoodleCoursesCreated"] == null)
        {
            ViewState["MoodleCoursesCreated"] = new List<MoodleCourse>();
        }
        return (List<MoodleCourse>)ViewState["MoodleCoursesCreated"];
    }
    set
    {
        ViewState["MoodleCoursesCreated"] = value;
    }
}
protected List<MoodleCourse> MoodleCoursesCreated
{
    get
    {
        if (ViewState["MoodleCoursesCreated"] == null)
        {
            ViewState["MoodleCoursesCreated"] = new List<MoodleCourse>();
        }
        return (List<MoodleCourse>)ViewState["MoodleCoursesCreated"];
    }
    set
    {
        ViewState["MoodleCoursesCreated"] = value;
    }
}
勿忘初心 2024-12-18 05:47:47

如果您希望某个属性在 getter 中产生副作用,请不要使用该属性。相反,只需使用这样的方法:

protected List<MoodleCourse> GetMoodleCourses()
{
    List<MoodleCourse> list = (List<MoodleCourse>)ViewState["MoodleCoursesCreated"];
    if (list == null)
    {
        list = new List<MoodleCourse>();
        ViewState["MoodleCoursesCreated"] = list;
    }
    return list;
}

Don't use a property if you want it to have side-effects in the getter. Instead just use a method like this:

protected List<MoodleCourse> GetMoodleCourses()
{
    List<MoodleCourse> list = (List<MoodleCourse>)ViewState["MoodleCoursesCreated"];
    if (list == null)
    {
        list = new List<MoodleCourse>();
        ViewState["MoodleCoursesCreated"] = list;
    }
    return list;
}
木有鱼丸 2024-12-18 05:47:47

你应该在 getter 中保留相同的引用:

protected List<MoodleCourse> MoodleCoursesCreated
{
    get
    {
        if (ViewState["MoodleCoursesCreated"] == null)
            ViewState["MoodleCoursesCreated"] = new List<MoodleCourse>();
        return ViewState["MoodleCoursesCreated"] as List<MoodleCourse>;
    }
    set
    {
        ViewState["MoodleCoursesCreated"] = value;
    }
}

you should hold the same reference in the getter:

protected List<MoodleCourse> MoodleCoursesCreated
{
    get
    {
        if (ViewState["MoodleCoursesCreated"] == null)
            ViewState["MoodleCoursesCreated"] = new List<MoodleCourse>();
        return ViewState["MoodleCoursesCreated"] as List<MoodleCourse>;
    }
    set
    {
        ViewState["MoodleCoursesCreated"] = value;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文