使用XmlSerializer序列化IntPtr [英] Serialize an IntPtr using XmlSerializer
问题描述
我想知道XmlSerializer实现为什么不支持IntPtr类型.当我尝试序列化包含IntPtr类型的字段的类时,序列化失败,告诉我不支持IntPtr,并忽略该成员.
要解决此问题,我将IntPtr值转换为Int64 ...,但这是一个好主意吗?就我所能想到的.具体来说,我需要序列化一个窗口句柄,该句柄在.NET Framework中键入IntPtr.我做对了吗?
IntPtr不可序列化的原因是,将其从环境中删除时,它通常根本没有任何意义.
如果序列化窗口句柄,则只有在窗口仍存在的情况下在同一位置反序列化它,才有意义.如果您在其他计算机,其他应用程序中或在删除窗口后反序列化它,则该句柄没有任何意义.
因此,您可以将其转换为可序列化的类型,但是要确保在反序列化它时仍然有意义.
I'm wondering why the IntPtr type is not supported by the XmlSerializer implementation. When I try to serialize a class including a field of IntPtr type, the serialization fails telling me that IntPtr is not supported, and ignore that member.
To workaround this, I translate the IntPtr value to a Int64... but is it a good idea? It should be, as far I can think. In concrete, I need to serialize a window handle, which is typed IntPtr in .NET framework. Am I doing correct?
The reason that an IntPtr is not serializable is that it generally doesn't make any sense at all when you remove it from it's environment.
If you serialize a window handle, it only makes sense if you deserialize it in the same spot, while the window still exists. If you deserialize it on a different computer, in a different application, or after the window is removed, the handle has no meaning.
So, you can cast it to a type that is serializable, but it's up to you to make sure that it still makes sense when you deserialize it.
这篇关于使用XmlSerializer序列化IntPtr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!