C# 错误 - 未标记为可序列化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15707872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 17:41:04  来源:igfitidea点击:

Error - is not marked as serializable

c#asp.net.netserialization

提问by Madam Zu Zu

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:

我有一个 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>

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.

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

Here is a snippet of my GetOrgList function:

这是我的 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];
    }

采纳答案by burning_LEGION

You need to add a Serializableattribute to the class which you want to serialize.

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

[Serializable]
public class OrgPermission

回答by nimeshjm

If you store an object in session state, that object must be serializable.

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

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

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



edit:

编辑:

In order for the session to be serialized correctly, all objects the application stores as session attributes must declare the [Serializable] attribute. Additionally, if the object requires custom serialization methods, it must also implement the ISerializable interface.

为了正确序列化会话,应用程序存储为会话属性的所有对象都必须声明 [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

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

回答by Jono

Leaving my specific solution of this for prosperity, as it's a tricky version of this problem:

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

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

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

Due to a class with an attribute IEnumerable<int>eg:

由于具有属性的类,IEnumerable<int>例如:

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

Originally the problem instance of MySessionDatawas set from a non-serializable list:

最初的问题实例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.

这里的原因是Select<int>(...)返回的具体类,具有不可序列化的类型数据,您需要将 id 复制到一个新的List<int>来解决它。

RelatedIDs = nonSerizableList.Select<int>(item => item.ID).ToList();