错误 - 没有标记为可序列化标记、错误、序列化

2023-09-04 01:52:52 作者:永远没有人懂我--

我收到的错误是:

 键入大会OrgPermission'APP_ code.ptjvczom,版本= 0.0.0.0,文化=中性公钥=空'未标记为可序列化。
 

这是我的code:

我有一个GridView,使用以下数据源:

 < ASP:ObjectDataSource控件ID =ObjectDataSource1=服务器
                         SelectMethod =GetOrgList类型名=组织>
                         < SelectParameters>
                             < ASP:SessionParameter名称=组织codeSSessionField =UserOrgsTYPE =对象/>
                             < ASP:参数默认值=YNAME =主动类型=字符串/>
                         < / SelectParameters>
                     < / ASP:ObjectDataSource控件>
 

我在页面加载设置会话变量,像这样:

 用户cUser =新用户(用户ID);
                //确保用户是管理员
                名单< OrgPermission>组织之参与=新的名单,其中,OrgPermission>();
                的foreach(OrgPermission组织在cUser.orgs)
                {
                    如果(org.type ==管理员)
                    {
                        orgs.Add(组织);
                    }
                }
                会话[UserOrgs] =组织之参与;
 
C 中序列化和反序列化的错误啊 正在尝试对空流进行反序列化

我的用户类看起来是这样的:

 公共类OrgPermission
{
    公共字符串组织{获得;组; }
    公开名单<字符串>键入{搞定;组; }

    公共OrgPermission()
    {}

}
公共类cUser
{

    公共字符串用户ID {获得;组; }
    公开名单< OrgPermission>组织之参与{获得;组; }

    公共clsUser(字符串的用户名)
    {
//我在这里设置的一切
}
 

我不明白为什么它打破,我可以用它没有使它序列化?

我试着调试和会话变量集就好了,它就会进入GetOrgList并返回正确的结果,但页面没有加载,我得到上述错误。

下面是我的GetOrgList功能的片断:

 公开数据表GetOrgList(名单< OrgPermission>组织codeS,串活性)
    {

        字符串orgList = NULL;

        // code设置OrgList使用参数就在这里。

        的DataSet ds为新的DataSet();
        SqlConnection的康恩=新的SqlConnection(cCon.getConn());
        的SqlCommand CMD =新的SqlCommand(sp_GetOrgList,康涅狄格州);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add(新的SqlParameter(@ orgList,orgList));
        cmd.Parameters.Add(新的SqlParameter(@主动,主动));

            conn.Open();
            SqlDataAdapter的SQLDA =新的SqlDataAdapter();

            sqlDA.SelectCommand = CMD;
            sqlDA.Fill(DS);

            conn.Close();
        返回ds.Tables [0];
    }
 

解决方案

  [Serializable接口]
公共类OrgPermission
 

The error I'm getting is:

Type 'OrgPermission' in Assembly 'App_Code.ptjvczom, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable. 

here is my code:

I have a gridview, that uses the following DataSource:

 <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
                         SelectMethod="GetOrgList" TypeName="Org">
                         <SelectParameters>
                             <asp:SessionParameter Name="orgCodes" SessionField="UserOrgs" Type="Object" />
                             <asp:Parameter DefaultValue="Y" Name="active" Type="String" />
                         </SelectParameters>
                     </asp:ObjectDataSource>

I set the session variable in my page load like so:

User cUser = new User(userid);
                //make sure the user is an Admin
                List<OrgPermission> orgs = new List<OrgPermission>();
                foreach(OrgPermission org in cUser.orgs)
                {
                    if (org.type=='admin')
                    {
                        orgs.Add(org);                       
                    }
                }
                Session["UserOrgs"] = orgs;

My user class looks like this:

public class OrgPermission
{
    public string Org { get; set; }   
    public List<string> type { get; set; }

    public OrgPermission()
    { }

}
public class cUser
{

    public string userid { get; set; }
    public List<OrgPermission> orgs { get; set; }

    public clsUser(string username)
    {
//i set everything here
}

I can't understand why it's breaking, can I use it without making it serializable?

I tried to debug, and the session variable sets just fine, it then goes into the GetOrgList and returned correct results, but the page does not load and I get the error above.

Here is a snippet of my GetOrgList function:

public DataTable GetOrgList(List<OrgPermission> orgCodes, string active)
    {

        string orgList = null;

        //code to set OrgList using the parameter is here.

        DataSet ds = new DataSet();
        SqlConnection conn = new SqlConnection(cCon.getConn());
        SqlCommand cmd = new SqlCommand("sp_GetOrgList", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add(new SqlParameter("@orgList", orgList));
        cmd.Parameters.Add(new SqlParameter("@active", active));

            conn.Open();
            SqlDataAdapter sqlDA = new SqlDataAdapter();

            sqlDA.SelectCommand = cmd;
            sqlDA.Fill(ds);

            conn.Close();
        return ds.Tables[0];
    }

解决方案

[Serializable]
public class OrgPermission