加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 分配Session变量/值时出现NullReferenceException

发布时间:2020-12-15 21:18:08 所属栏目:百科 来源:网络整理
导读:我在ASP.NET项目的很多地方使用Session变量来存储数据.我经常写这样的东西: public uint MyPropery { get { object o = Session["MyProperty"]; if (o != null) return (uint)o; else return 0; } set { Session["MyProperty"] = value; }} 但是,这次我在se
我在ASP.NET项目的很多地方使用Session变量来存储数据.我经常写这样的东西:

public uint MyPropery 
{
    get 
    {
        object o = Session["MyProperty"];
        if (o != null)
            return (uint)o;
        else
            return 0;
    }
    set 
    {
        Session["MyProperty"] = value;
    }
}

但是,这次我在setter中得到了NullReferenceException.据我所知,以上述方式分配Session变量是有效的.此外,Session不为null,也不是值.

有什么想法吗?

编辑:

添加属性所在的UserControl的代码.我正在使用ext.net,但这不应该与此有任何关系.我想到了一个想法:

UserControl(如下所示)是在页面的代码隐藏中动态添加的.这与它有什么关系吗?

我正在添加像这样的UserControls(在页面上):

foreach(CoreCommons.System.Comment c in cg.Reply_Comments)
{
    WebApplicationExtNetTest.Secure.UserControls.CoreComment cc = new UserControls.CoreComment();
    cc._Comment = c; // here is where i get the NullRef
    this.Panel1.ContentControls.Add(cc);
}

标记:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="CoreComment.ascx.cs" Inherits="WebApplicationExtNetTest.Secure.UserControls.CoreComment" %>
<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>

<ext:Panel runat="server" ID="CoreCommentOuterPanel" BodyStyle="background: #FFFDDE">
    <Items>
        <ext:ColumnLayout runat="server">
            <Columns>
                <ext:LayoutColumn ColumnWidth="0.8">
                    <ext:Image runat="server" ImageUrl="/Resources/bullet_triangle_green_16x16.png" Align="AbsMiddle"></ext:Image> 
                    <ext:Label runat="server" ID="lblCommentInfo"></ext:Label>
                </ext:LayoutColumn>
                <ext:LayoutColumn ColumnWidth="0.2"><ext:Button runat="server" ID="btnDelete" Icon="Delete"></ext:Button></ext:LayoutColumn>
            </Columns>
        </ext:ColumnLayout>
        <ext:Label runat="server" ID="lblComment"></ext:Label>
    </Items>
</ext:Panel>

代码隐藏:

namespace WebApplicationExtNetTest.Secure.UserControls
{
    public partial class CoreComment : System.Web.UI.UserControl
    {
        public CoreCommons.System.Comment _Comment
        {
            get
            {
                object o = Session["CoreComment_ObjectId"];
                if (o != null)
                    return (tWorks.Core.CoreCommons.System.Comment)o;
                else
                    return null;
            }
            set
            {
                Session["CoreComment_ObjectId"] = value;
                SetComment();
            }
        }            

        protected void Page_Load(object sender,EventArgs e)
        {    
        }

        private void SetComment()
        {
            if (_Comment == null)
            {
                lblCommentInfo.Text = "";
                lblComment.Text = "";
            }
            else
            {
                lblCommentInfo.Text = _Comment.Author + "," + _Comment.TimeStamp.ToString("g");
                lblComment.Text = _Comment.Text;
            }
        }
    }
}

解决方法

我几乎完全确定在SetComment()中抛出了NullReferenceException,因为在设置_Comment属性时,没有任何CoreComment的子控件(lblComment,lblCommentInfo)被正确实例化.

这些子控件未实例化的原因确实是您当前添加CoreComment控件的方式.要动态添加UserControl,必须使用Page.LoadControl()(参见:here)来创建控件的新实例,因为它会执行一些幕后魔术以确保它被正确初始化,其中包括实例化儿童控制.

在旁注中,我个人将SetComment()更改为SetComment(CoreCommons.System.Comment注释)并使用参数而不是重复调用getter,或者,如果保留原始,至少只调用一次getter并存储结果是局部变量.我假设可能是InProc会话存储,它不会产生太大的影响,但在任何其他存储模式下,你都会无缘无故地重复反序列化Comment对象.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读