简体   繁体   English

IsSerializable为标记为Serializable的对象返回“false”

[英]IsSerializable returning “false” for an object marked Serializable

I'm attempting to serialize a DenseMatrix object which is an object provided within the MathNET Numerics project. 我正在尝试序列化DenseMatrix对象,这是MathNET Numerics项目中提供的对象。

DenseMatrix.cs DenseMatrix.cs

Below I have provided some useful information. 下面我提供了一些有用的信息。

  • I can see from both the object explorer as well as .NET reflector that this class (and everything this class inherits from) is marked as [Serializable]. 我可以从对象资源管理器和.NET反射器中看到,这个类(以及这个类继承的所有东西)被标记为[Serializable]。

  • Attribute.IsDefined(typeof(MathNet.Numerics.LinearAlgebra.Double.DenseMatrix), typeof(SerializedAttribute)) returns true . Attribute.IsDefined(typeof(MathNet.Numerics.LinearAlgebra.Double.DenseMatrix),typeof(SerializedAttribute))返回true

  • typeof(MathNet.Numerics.LinearAlgebra.Double.DenseMatrix).IsSerializable returns false typeof(MathNet.Numerics.LinearAlgebra.Double.DenseMatrix).IsSerializable返回false

  • It seems everything in the Mathnet.Numerics Matrix family is marked [Serializable], though they all show the same non-serializable behavior. 似乎Mathnet.Numerics Matrix系列中的所有内容都标记为[Serializable],尽管它们都显示相同的非序列化行为。

Now, when I attempt to run my object through a BinaryFormatter, I get a SerializationException "Type MathNet.Numerics.LinearAlgebra.Double.DenseMatrix is not marked as Serializable". 现在,当我尝试通过BinaryFormatter运行我的对象时,我得到一个SerializationException“Type MathNet.Numerics.LinearAlgebra.Double.DenseMatrix未标记为Serializable”。

What is going on here? 这里发生了什么? I know that the BinaryFormatter is looking directly at the IsSerializable property, so that's why it's coughing up. 我知道BinaryFormatter正在直接查看IsSerializable属性,所以这就是它咳嗽的原因。 So why is IsSerializable returning false? 那么为什么IsSerializable会返回false?

EDIT 编辑

The question is directed more toward the .NET implementation of IsSerializable in general, and only uses The Mathnet example as context. 问题更多地针对IsSerializable的.NET实现,并且仅使用Mathnet示例作为上下文。

2nd EDIT 第二次编辑

I think adding to the confusion was the fact that I was using MathNet's "portable" library version. 我认为增加混乱是因为我使用的是MathNet的“便携式”库版本。 In order to make the portable library compile, they had created a custom SerializableAttribute that would be used since System.SerializableAttribute is not available for a portable library. 为了使可移植库编译,他们创建了一个自定义的SerializableAttribute,因为System.SerializableAttribute不适用于可移植库,所以会使用它。 When running with Mono, System.Type would look for System.SerializableAttribute, but only find MathNet.SerializableAttribute. 使用Mono运行时,System.Type将查找System.SerializableAttribute,但只能找到MathNet.SerializableAttribute。

.NET recursively checks all members of objects marked [Serializable], and IsSerializable returns false if the types of any members of a [Serializable] class are not marked [Serializable] as well. .NET递归检查标记为[Serializable]的对象的所有成员,如果[Serializable]类的任何成员的类型也未标记为[Serializable],则IsSerializable返回false。

The solution to the above case is to simply mark the classes that are members in DenseMatrix as [Serializable]. 上述情况的解决方案是简单地将DenseMatrix中成员的类标记为[Serializable]。

ie DenseColumnMatrixStorage 即DenseColumnMatrixStorage

MatrixStorage MatrixStorage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 保留未标记为可序列化的对象 - Persist an object that is not marked as serializable 从标记为可序列化的对象获取XMlNode内容 - Get the XMlNode content from object marked as serializable C#无法序列化对象,因为MainViewModel没有标记为Serializable - C# Cannot serialize object because MainViewModel is not marked Serializable 如何执行未标记为可序列化的对象的深层副本(在C#中)? - How to perform a deep copy of an object not marked as serializable (in C#)? 为什么BinaryFormatter会尝试将标记为[Serializable]的类型的对象强制转换为IConvertible? - Why would BinaryFormatter attempt to cast an object of type marked [Serializable] to IConvertible? 当一个对象派生自MarshalByRefObject并且还标记为[Serializable]时会发生什么? - what happens when an object derives from MarshalByRefObject and is also marked [Serializable]? 为什么二进制序列化要求将对象标记为可序列化? - Why does Binary Serialization require the object to be marked as serializable? LuisResult未标记为可序列化 - LuisResult is not marked as serializable SerializationException类型“未标记为可序列化” - 但它确实如此 - SerializationException Type “is not marked as serializable” - But it is 错误 - 未标记为可序列化 - Error - is not marked as serializable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM