当我使用 PropertyInfo.SetValue() 时,它会引发 stackoverflow 异常

发布于 2024-08-07 04:29:52 字数 945 浏览 4 评论 0原文

当我在 asp.net 中使用 PropertyInfo.SetValue 时,它​​会引发 stackoverflow 异常。 我写了这段代码:

    for (int i = 0; i < rivalSeriesIDList.Count; i++)
    {
        cardb_series rivalSeries = seriesBll.GetSeriesInfoByID(rivalSeriesIDList[i].ToString());
        this.GetType().GetProperty("brandid" + (i + 1)).SetValue(this, rivalSeries.brand_id, null);
        this.GetType().GetProperty("seriesid" + (i + 1)).SetValue(this, rivalSeries.series_id, null);
    }

并且brandid+number和seriesid+number是aspx_page的属性。像这样:

public int brandid1
{
    get
    {
        if (Request.Form["brandid1"] != null)
            return int.Parse(Request.Form["brandid1"]);
        if (Request["brandid1"] != null)
            return int.Parse(Request["brandid1"]);
        return 0;
    }
    set
    {
        brandid1 = value;
    }
}

当我在控制台应用程序中测试代码时,一切正常。但是当我在Web应用程序中测试它时,它会导致堆栈溢出异常。 我不知道为什么。因为网络是无状态的? 谢谢。

When I use PropertyInfo.SetValue in asp.net , it throws a stackoverflow exception.
That I write this code:

    for (int i = 0; i < rivalSeriesIDList.Count; i++)
    {
        cardb_series rivalSeries = seriesBll.GetSeriesInfoByID(rivalSeriesIDList[i].ToString());
        this.GetType().GetProperty("brandid" + (i + 1)).SetValue(this, rivalSeries.brand_id, null);
        this.GetType().GetProperty("seriesid" + (i + 1)).SetValue(this, rivalSeries.series_id, null);
    }

And brandid+number and seriesid+number is a property of aspx_page. like this:

public int brandid1
{
    get
    {
        if (Request.Form["brandid1"] != null)
            return int.Parse(Request.Form["brandid1"]);
        if (Request["brandid1"] != null)
            return int.Parse(Request["brandid1"]);
        return 0;
    }
    set
    {
        brandid1 = value;
    }
}

when I test the code in a Console Application ,It is all right . But when I test it in a Web Application ,it will cause a stack overflow exception .
I don't know why. Because of web is no-state?
Thanks.

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

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

发布评论

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

评论(1

天荒地未老 2024-08-14 04:29:52

因为你递归地调用你的属性,即使你直接调用该属性也会得到相同的异常

public int brandid1 <- this one
{
    get
    {
        if (Request.Form["brandid1"] != null)
            return int.Parse(Request.Form["brandid1"]);
        if (Request["brandid1"] != null)
            return int.Parse(Request["brandid1"]);
        return 0;
    }
    set
    {
       and this one -> brandid1 = value;
    }
}

我不知道你想做什么,但是试试这个

private int _brandid1;
 public int brandid1 <- this one
    {
        get
        {
            if (Request.Form["brandid1"] != null)
                return int.Parse(Request.Form["brandid1"]);
            if (Request["brandid1"] != null)
                return int.Parse(Request["brandid1"]);
            return 0;
        }
        set
        {
           _brandid1 = value;
        }
    }

cause you call your property recursively, and will get the same exception even if you will call the property directly

public int brandid1 <- this one
{
    get
    {
        if (Request.Form["brandid1"] != null)
            return int.Parse(Request.Form["brandid1"]);
        if (Request["brandid1"] != null)
            return int.Parse(Request["brandid1"]);
        return 0;
    }
    set
    {
       and this one -> brandid1 = value;
    }
}

I don't know what do you want to do, but try this

private int _brandid1;
 public int brandid1 <- this one
    {
        get
        {
            if (Request.Form["brandid1"] != null)
                return int.Parse(Request.Form["brandid1"]);
            if (Request["brandid1"] != null)
                return int.Parse(Request["brandid1"]);
            return 0;
        }
        set
        {
           _brandid1 = value;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文