简体   繁体   English

二进制格式化程序-类型UserControl未标记为可序列化

[英]Binary Formatter - Type UserControl is not marked as serializable

I'm trying to serialize my object using BinaryFormatter : 我正在尝试使用BinaryFormatter序列化我的对象:

private UserSettings _userSettings;
var serializer = new BinaryFormatter();
using (var file = new FileStream(@"D:\test.bin", FileMode.Create))
{
    serializer.Serialize(file, _userSettings);
}

But I'm getting a strange exception: 但是我得到一个奇怪的例外:

Type 'MyCustomUserControl' in Assembly 'xxx' is not marked as serializable. 程序集“ xxx”中的类型“ MyCustomUserControl”未标记为可序列化。

Class UserSettings doesn't have any reference to MyCustomUserControl but it's referenced by MyCustomUserControl . UserSettings没有对MyCustomUserControl任何引用,但由MyCustomUserControl引用。

The reference to the control can be hidden in an event. 控件的引用可以在事件中隐藏。 If your UserSettings class implements an event to which the control is subscribed, add the [field:NonSerialized] attribute to the event: 如果您的UserSettings类实现了订阅了控件的事件,则将[field:NonSerialized]属性添加到该事件:

[field:NonSerialized]
public event PropertyChangedEventHandler PropertyChanged;

For using BinaryFormatter serializer, your class needs to be marked as Serializable . 为了使用BinaryFormatter序列化程序,您的类需要标记为Serializable Here is an example: 这是一个例子:

[Serializable]
public class MyCustomUserControl: Control
{
    // 
}

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

相关问题 UserControl 集合未标记为可序列化 - UserControl collection not marked as serializable SerializationException类型“未标记为可序列化” - 但它确实如此 - SerializationException Type “is not marked as serializable” - But it is 程序集中的类型未标记为可序列化 - Type in assembly is not marked as serializable AppDomain CreateInstanceAndUnwrap:类型未标记为可序列化 - AppDomain CreateInstanceAndUnwrap: Type is not marked as serializable 汇编中的类型未标记为可序列化的 Unity - Type in assembly is not marked as serializable Unity 调试器Visualizer和“类型未标记为可序列化” - Debugger Visualizer and “Type is not marked as serializable” &lt;&lt; SerializationException: Type 'UnityEngine.MonoBehaviour' ...未标记为可序列化&gt;&gt;? - << SerializationException : Type 'UnityEngine.MonoBehaviour' … is not marked as serializable >>? 类序列化期间发生错误:类型未标记为可序列化 - Error during class serialization: type is not marked serializable SerializationException:类型UnityEngine.GameObject未标记为Serializable - SerializationException: Type UnityEngine.GameObject is not marked as Serializable 未能创建组件.. 类型未标记为可序列化 - Failed to Create Component .. Type is not Marked as Serializable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM