[INFO] Exception Thrown When Trying To Deserialize An Object Whose Type Is Defined In An Assembly Which Is Loaded By Assembly.Lo

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Chronology. In the year 2000 I was given two courses to teach: analysis and topology. The Analysis course was assigned to me due to an emergency redistribution; Topology was thrown in as an award for accepting to teach Analysis on such short notice. Algebraic topology, which covered only homotopy theory in its curriculum, was (re)introduced into the undergraduate-graduate program of the Mathematics Department (University of Manitoba) in 2004. I have taught these two courses almost continuously ever since. This book is primarily an upshot of my teaching lifestyle over these years. The level of the book, and the target audience. The book has two clearly differentiated parts: Part 1 is topology and Part 2 is homotopy. As indicated by the title of the book, we do not have any pretense to go very deeply into these subjects. On the other hand, neither could the content be described as too breezy, for we include most of the important theorems of the basic theory. The material covered in this book is on the fuzzy boundary between the undergraduate and the graduate level. One may tentatively state that Part 1 is mostly at the advanced undergraduate level, Part 2 is mostly at the early graduate level. Indeed, the Algebraic topology (Homotopy theory) course that I have been teaching has always been cross-listed, and has almost always been taken by both graduate and advanced undergraduate students. It is my hope that this book will bring this beautiful theory closer to the undergraduate curriculum.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值