未标记为可序列化的异常
Exception not marked as serializable
没有更改代码(我记得)formatter.Serialize 对象类型失败
它适用于测试的其他 5 种类型
我没有对 class(我记得)进行任何更改 - 被标记为可序列化
这是一个相当简单的 class 并且不尊重 system.Windows.Documents.FlowDocument
如何找到错误并修复它?
public static T DeepClone<T>(T obj)
{
using (var ms = new MemoryStream())
{
var formatter = new BinaryFormatter();
try
{
formatter.Serialize(ms, obj);
}
catch (Exception Ex)
{
Debug.WriteLine(Ex.Message);
Debug.WriteLine(Ex.Source);
'System.Runtime.Serialization.SerializationException' 类型的第一次机会异常发生在 mscorlib.dll
附加信息:程序集 'PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' 中的类型 'System.Windows.Documents.FlowDocument' 未标记为可序列化。
InnerExeption 为空
我有一些缩小范围的东西,在调用 属性 之前它起作用了
在调用那个 属性 之后它失败了
ThisUserGroups 是 属性
try
{
User userclone = DeepClone<User>(CurUser); // success
}
catch (Exception Ex)
{
Debug.WriteLine("Before call to CurUser.ThisUserGroups.Count()");
Debug.WriteLine(Ex.Message);
}
Debug.WriteLine("CurUser.ThisUserGroups.Count()" + " " + CurUser.ThisUserGroups.Count() + " " + CurUser.UserID);
try
{
User userclone = DeepClone<User>(CurUser); //failure
}
问题 class 和 属性
省略其他属性和方法
组是可序列化的
[Serializable()]
public class User : Object, INotifyPropertyChanged
{
[field: NonSerializedAttribute()]
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged != null) PropertyChanged(this, e);
}
protected void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
private HashSet<Group> thisUserGroups;
public HashSet<Group> ThisUserGroups
{ // after a call to this deepclone fails
get
{
if (thisUserGroups == null)
{
thisUserGroups = new HashSet<Group>();
foreach (Group g in groupsPlus) // static all groups in library 14
{
if (!thisUserGroups.Contains(g) && g.ID <= 0)
thisUserGroups.Add(g);
}
foreach (UserGroup userGroup in userGroups.OrderBy(x => x.Group.Name)) // docAdmin fail userGroups == 54 and thisUserGoups is 2
{ // docAdminNotGroup success userGroups == 54 and thisUserGoups is 2
if (!thisUserGroups.Contains(userGroup.Group) && (IsInRoleDocAdmin || userGroup.UsrID == usrID))
thisUserGroups.Add(userGroup.Group);
}
}
return thisUserGroups; // docAdmin fail thisUserGroups = 14
}
}
public User(Int16 UsrIDcon, string UserIDcon, string UserInitialsCon, string IPROuserIDcon, string IPROuserPWcon, Int16? FieldGroupID,
double WindowLeft, double WindowTop, double WindowHeight, double WindowWidth, bool WindowMaximized,
bool ViewIpro, bool ViewNative, enumSearchDetail? SearchDetail, string ExportPath)
{
usrID = UsrIDcon;
userID = UserIDcon;
userInitials = UserInitialsCon;
iPROuserID = IPROuserIDcon;
iPROuserPW = IPROuserPWcon;
fieldGroupID = FieldGroupID;
windowLeft = WindowLeft;
windowTop = WindowTop;
windowHeight = WindowHeight;
windowWidth = WindowWidth;
windowMaximixed = WindowMaximized;
viewIpro = ViewIpro;
viewNative = ViewNative;
searchDetail = SearchDetail;
if (!string.IsNullOrEmpty(ExportPath)) exportPath = ExportPath;
if (App.StaticGabeLib != null && App.StaticGabeLib.Search != null && searchDetail != null)
{
App.StaticGabeLib.Search.SearchDetail = (enumSearchDetail)searchDetail;
}
loginTime = DateTime.Now;
}
}
我能想到的是,从两个集合中添加是问题所在。
我重构为仅使用 groupsPlus(没有 userGroup.Group)并且它起作用了。
但我不知道为什么会这样。
更奇怪的是它曾经从两个集合中添加。
没有更改代码(我记得)formatter.Serialize 对象类型失败
它适用于测试的其他 5 种类型
我没有对 class(我记得)进行任何更改 - 被标记为可序列化
这是一个相当简单的 class 并且不尊重 system.Windows.Documents.FlowDocument
如何找到错误并修复它?
public static T DeepClone<T>(T obj)
{
using (var ms = new MemoryStream())
{
var formatter = new BinaryFormatter();
try
{
formatter.Serialize(ms, obj);
}
catch (Exception Ex)
{
Debug.WriteLine(Ex.Message);
Debug.WriteLine(Ex.Source);
'System.Runtime.Serialization.SerializationException' 类型的第一次机会异常发生在 mscorlib.dll
附加信息:程序集 'PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' 中的类型 'System.Windows.Documents.FlowDocument' 未标记为可序列化。
InnerExeption 为空
我有一些缩小范围的东西,在调用 属性 之前它起作用了
在调用那个 属性 之后它失败了
ThisUserGroups 是 属性
try
{
User userclone = DeepClone<User>(CurUser); // success
}
catch (Exception Ex)
{
Debug.WriteLine("Before call to CurUser.ThisUserGroups.Count()");
Debug.WriteLine(Ex.Message);
}
Debug.WriteLine("CurUser.ThisUserGroups.Count()" + " " + CurUser.ThisUserGroups.Count() + " " + CurUser.UserID);
try
{
User userclone = DeepClone<User>(CurUser); //failure
}
问题 class 和 属性
省略其他属性和方法
组是可序列化的
[Serializable()]
public class User : Object, INotifyPropertyChanged
{
[field: NonSerializedAttribute()]
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged != null) PropertyChanged(this, e);
}
protected void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
private HashSet<Group> thisUserGroups;
public HashSet<Group> ThisUserGroups
{ // after a call to this deepclone fails
get
{
if (thisUserGroups == null)
{
thisUserGroups = new HashSet<Group>();
foreach (Group g in groupsPlus) // static all groups in library 14
{
if (!thisUserGroups.Contains(g) && g.ID <= 0)
thisUserGroups.Add(g);
}
foreach (UserGroup userGroup in userGroups.OrderBy(x => x.Group.Name)) // docAdmin fail userGroups == 54 and thisUserGoups is 2
{ // docAdminNotGroup success userGroups == 54 and thisUserGoups is 2
if (!thisUserGroups.Contains(userGroup.Group) && (IsInRoleDocAdmin || userGroup.UsrID == usrID))
thisUserGroups.Add(userGroup.Group);
}
}
return thisUserGroups; // docAdmin fail thisUserGroups = 14
}
}
public User(Int16 UsrIDcon, string UserIDcon, string UserInitialsCon, string IPROuserIDcon, string IPROuserPWcon, Int16? FieldGroupID,
double WindowLeft, double WindowTop, double WindowHeight, double WindowWidth, bool WindowMaximized,
bool ViewIpro, bool ViewNative, enumSearchDetail? SearchDetail, string ExportPath)
{
usrID = UsrIDcon;
userID = UserIDcon;
userInitials = UserInitialsCon;
iPROuserID = IPROuserIDcon;
iPROuserPW = IPROuserPWcon;
fieldGroupID = FieldGroupID;
windowLeft = WindowLeft;
windowTop = WindowTop;
windowHeight = WindowHeight;
windowWidth = WindowWidth;
windowMaximixed = WindowMaximized;
viewIpro = ViewIpro;
viewNative = ViewNative;
searchDetail = SearchDetail;
if (!string.IsNullOrEmpty(ExportPath)) exportPath = ExportPath;
if (App.StaticGabeLib != null && App.StaticGabeLib.Search != null && searchDetail != null)
{
App.StaticGabeLib.Search.SearchDetail = (enumSearchDetail)searchDetail;
}
loginTime = DateTime.Now;
}
}
我能想到的是,从两个集合中添加是问题所在。
我重构为仅使用 groupsPlus(没有 userGroup.Group)并且它起作用了。
但我不知道为什么会这样。
更奇怪的是它曾经从两个集合中添加。