异常未标记为可序列化

时间:2015-05-08 14:22:56

标签: .net exception serialization

没有更改代码(我记得)formatter.Serialize因对象类型而失败 它适用于其他5种类型的测试 我没有对该类进行任何更改(我记得) - 被标记为Serializable 这是一个相当简单的类,对系统没有任何敬意.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);

类型&#39; System.Runtime.Serialization.SerializationException&#39;的第一次机会异常。发生在mscorlib.dll

其他信息:键入&#39; System.Windows.Documents.FlowDocument&#39;在Assembly&#39; PresentationFramework,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35&#39;未标记为可序列化。

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 
}

问题类和属性
其他属性和方法省略了 组是可序列化的

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

1 个答案:

答案 0 :(得分:0)

我能想到的是,从两个集合中添加是问题所在。

我重构只使用groupsPlus(没有userGroup.Group)并且它有效。

但我不知道为什么会让它发挥作用。

更奇怪的是,它曾用于从两个集合中添加。