[INFO] Exception Thrown When Trying To Deserialize An Object Whose Type Is Defined In An Assembly Which Is Loaded By Assembly.Lo
来源:互联网 发布:mmd怎么导出动作数据 编辑:程序博客网 时间:2024/05/20 06:52
SYMPTOMS
Suppose you call Assembly.LoadFrom(...) to load an assembly named SimpleAsm in which type SimpleType is defined. Then you try to serialize a SimpleType ojbect, it works fine. But as soon as you deserialize some SimpleType object, you encounter following exception:
System.Runtime.Serialization.SerializationException: Cannot find the assembly SimpleAsm, Version=xxxx, Culture=xxxx, PublicKeyToken=xxxx.
Even the call to deserialization is in SimpleAsm itselft, the exception still exists. Although it looks a little bit ridiculous, it's the proper behavior by design.
CAUSE
"Some extensible applications use Assembly.LoadFrom to load an assembly and then construct objects from types defined in the loaded assembly. These objects can be serialized to a stream without any trouble. However, when deserializing this stream, the formatter attempts to load the assembly by calling Assembly's Load or LoadWithPartialName method instead of calling the LoadFrom method. In most cases, the Common Language Runtime (CLR) will not be able to locate the assembly file, causing a SerializationException exception to be thrown. "
RESOLUTION
Implement and register a System.ResolveEventHandler to help CLR class loader redirect to the properly position of the assembly and reload it into memory. The handler may look like this:
public static Assembly MyResolveEventHandler(object sender, ResolveEventArgs args)
{
string[] strProps = args.Name.Split(new char[] {','});
String strPath = m_codebase + strProps[0] + ".dll";
return Assembly.LoadFrom(strPath);
}
m_codebase is a relative directory that your assembly resides in. Before deserialize any SimpleType objects, register your event handler to the application domain:
Thread.GetDomain().AssemblyResolve += resolveHandler;
Also remember to unregister the handler when finish deserializing SimpleType objects to avoid any side-effects that may be brought by custom event handler:
Thread.GetDomain().AssemblyResolve -= resolveHandler;
MORE INFORMATION
.NET 247: SerializationException on microsoft.public.vsnet.ide
Run-time Serialization
- [INFO] Exception Thrown When Trying To Deserialize An Object Whose Type Is Defined In An Assembly Which Is Loaded By Assembly.Lo
- 错误 the type "xxxx" is defined in an assembly that is not refernced.You must add a reference to assem
- The type 'System.Data.Objects.DataClasses.EntityObject' is defined in an assembly that is not refere
- C#解决类似System.Data.SQLite.SQLiteParameter' is defined in an assembly that is not referenced. You must
- Finding all elements(which are integer) in an array whose sum is equals to the element exists within
- Deserialize causes exception "Unable to find assembly"
- How to embed a manifest in an assembly
- Deployment failed due to an error in FastDev assembly synchronization.
- Caused by: The Result type [json] which is defined in the Result annotation on the class
- Which is better, a return code or an exception?
- Message BT123 - Target host or target server & is not defined in an operation type
- The final local variable xxx cannot be assigned, since it is defined in an enclosing type
- The final local variable xxx cannot be assigned, since it is defined in an enclosing type
- The final local variable xxx cannot be assigned, since it is defined in an enclosing type“
- The final local variable xxx cannot be assigned, since it is defined in an enclosing type“,
- The final local variable xxx cannot be assigned, since it is defined in an enclosing type
- The final local variable xxx cannot be assigned, since it is defined in an enclosing type
- The final local variable cannot be assigned, since it is defined in an enclosing type
- 我第一次BLOG,难忘学子生涯,就把自己5年前的离开学校时的告别宣言copy一下上来,缅怀一下!
- Building-a-WYSIWYG-HTML-Editor
- 中国软件(CSDN)灌水乐园2004年第一季度人权报告
- 2004年6月22日
- My Blog :)
- [INFO] Exception Thrown When Trying To Deserialize An Object Whose Type Is Defined In An Assembly Which Is Loaded By Assembly.Lo
- 网络游戏中的数据包设计与定义(初论)
- 从BBS项目的开发谈起,回顾毕业后这一年
- 沟通的成本
- PHP 面向对象程序设计资源
- 游戏设计师需要那些能力?
- 这个BLOG真不错
- CSDN终于开放Blog了
- Record Management System从入门到精通系列之一