68

我得到的错误是:

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

这是我的代码:

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

 <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>

我在页面加载中设置会话变量,如下所示:

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;

我的用户类如下所示:

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
    }
}

我不明白它为什么会坏,我可以在不使其可序列化的情况下使用它吗?

我尝试调试,并且会话变量设置得很好,然后它进入 GetOrgList 并返回了正确的结果,但是页面没有加载,我得到了上面的错误。

这是我的 GetOrgList 函数的片段:

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];
    }
4

3 回答 3

175

您需要向Serializable要序列化的类添加一个属性。

[Serializable]
public class OrgPermission
于 2013-03-29T17:03:34.883 回答
17

如果将对象存储在会话状态中,则该对象必须是可序列化的。

http://www.hpenterprisesecurity.com/vulncat/en/vulncat/dotnet/asp_dotnet_bad_practices_non_serializable_object_stored_in_session.html


编辑:

为了正确序列化会话,应用程序存储为会话属性的所有对象都必须声明 [Serializable] 属性。此外,如果对象需要自定义序列化方法,它还必须实现 ISerializable 接口。

https://vulncat.hpefod.com/en/detail?id=desc.structural.dotnet.asp_dotnet_bad_practices_non_serializable_object_stored_in_session#C%23%2fVB.NET%2fASP.NET

于 2013-03-29T17:19:40.857 回答
3

为了繁荣,留下我的具体解决方案,因为它是这个问题的一个棘手版本:

Type 'System.Linq.Enumerable+WhereSelectArrayIterator[T...] was not marked as serializable

由于具有类型字段的类,IEnumerable<int>例如:

[Serializable]
class MySessionData{
    public int ID;
    public IEnumerable<int> RelatedIDs; //This can be an issue
}

最初的问题实例MySessionData是从一个不可序列化的列表中设置的:

MySessionData instance = new MySessionData(){ 
    ID = 123,
    RelatedIDs = nonSerizableList.Select<int>(item => item.ID)
};

The cause here is the concrete class that the Select<int>(...) returns, has type data that's not serializable, and you need to copy the id's to a fresh List<int> to resolve it.

RelatedIDs = nonSerizableList.Select<int>(item => item.ID).ToList();
于 2018-10-26T10:19:40.357 回答