首页 新闻 会员 周边

构造函数给静态变量赋值,内存溢出

0
[已关闭问题] 关闭于 2022-08-25 11:39

主要报错:

Stack overflow.
Repeat 13770 times:
--------------------------------
   at Simple.Repository.DataFilter..ctor()
--------------------------------
   at Simple.Repository.SimpleDbContext..ctor(Microsoft.EntityFrameworkCore.DbContextOptions`1<Simple.Repository.SimpleDbContext>, Simple.Common.Services.ICurrentUserService)

相关代码:

// SimpleDbContext
public class SimpleDbContext : DbContext
{
    public virtual DataFilter DataFilter { get; set; } = new DataFilter();
}
public class DataFilter
{
    private static DataFilter _default;

    public bool IgnoreSoftDelete { get; set; }

    static DataFilter()
    {
        // 这种方式赋值,在构造函数中无法获取到值
        //_default = new DataFilter()
        //{
        //    IgnoreSoftDelete = false
        //};
    }

    public DataFilter()
    {
        // 这种写法直接 Stack Overflow 了
        if(_default == null)
        {
            _default = new DataFilter()
            {
                IgnoreSoftDelete = false
            };
        }

        IgnoreSoftDelete = _default.IgnoreSoftDelete;
    }
}

遇到一个莫名其妙的问题,先发上来,看看有没大佬能解疑一下。
个人同步去找答案,找到再回来跟帖_

芦荟柚子茶的主页 芦荟柚子茶 | 菜鸟二级 | 园豆:202
提问于:2022-08-25 11:37
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册