오류 - 직렬화 가능으로 표시되지 않음
오류는 다음과 같습니다.
Type 'OrgPermission' in Assembly 'App_Code.ptjvczom, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.
내 코드는 다음과 같습니다.
다음 데이터 소스를 사용하는 그리드 보기가 있습니다.
<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];
}
다음을 추가해야 합니다.Serializable직렬화할 클래스의 속성입니다.
[Serializable]
public class OrgPermission
개체를 세션 상태로 저장하는 경우 해당 개체를 직렬화할 수 있어야 합니다.
편집:
세션을 올바르게 직렬화하려면 응용 프로그램이 세션 특성으로 저장하는 모든 개체가 [Serializable] 특성을 선언해야 합니다.또한 개체에 사용자 지정 직렬화 방법이 필요한 경우 ISerializable 인터페이스도 구현해야 합니다.
이 문제의 까다로운 버전이기 때문에 이 문제에 대한 구체적인 해결책은 번영을 위해 남겨둡니다.
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)
};
여기서의 원인은 구체적인 클래스입니다.Select<int>(...)직렬화할 수 없는 유형 데이터가 반환되고 ID를 새로 복사해야 합니다.List<int>그것을 해결하기 위해.
RelatedIDs = nonSerizableList.Select<int>(item => item.ID).ToList();
언급URL : https://stackoverflow.com/questions/15707872/error-is-not-marked-as-serializable
'programing' 카테고리의 다른 글
| Git 하위 모듈의 추적되지 않은 상태를 제거하는 방법은 무엇입니까? (0) | 2023.06.21 |
|---|---|
| C#.net 코드에서 null 변수를 SQL 저장 프로시저로 전달하는 방법 (0) | 2023.06.21 |
| SQL Server에는 패키지가 없기 때문에 프로그래머는 이를 피하기 위해 무엇을 합니까? (0) | 2023.06.16 |
| ORA-4031 "x바이트의 공유 메모리를 할당할 수 없음" 해결 (0) | 2023.06.16 |
| 보안 규칙으로 하위/필드 액세스 제한 (0) | 2023.06.16 |