Error - is not marked as serializable

后端 未结 3 1271
借酒劲吻你
借酒劲吻你 2020-12-01 09:55

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 s         


        
相关标签:
3条回答
  • 2020-12-01 10:39

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

    [Serializable]
    public class OrgPermission
    
    0 讨论(0)
  • 2020-12-01 10:42

    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

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

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

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

    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();
    
    0 讨论(0)
  • 2020-12-01 10:46

    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


    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.

    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

    0 讨论(0)
提交回复
热议问题