【问题标题】:Exception not marked as serializable未标记为可序列化的异常
【发布时间】:2015-07-19 12:10:54
【问题描述】:

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

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

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

【问题讨论】:

  • 您能显示未能序列化的类定义吗?
  • @Andy_Vulhop 代码示例和更多细节添加
  • Group 或其子项是否包含System.Windows.Documents.FlowDocument 类型的属性?
  • @Andy_Vulhop NO - 这让我很困惑。有趣的是,如果我只做 groupsPlus 而不是 userGroups,那么它就可以工作。所以它适用于某些组,但不适用于其他组。

标签: .net exception serialization


【解决方案1】:

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

我重构为仅使用 groupsPlus(没有 userGroup.Group)并且它起作用了。

但我不知道为什么它会起作用。

更奇怪的是它曾经可以从两个集合中添加。

【讨论】:

    猜你喜欢
    • 2011-02-11
    • 2011-02-06
    • 2017-02-21
    • 2015-11-10
    • 2016-01-18
    • 2011-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多