I made a bunch of changes to code recently and then ran a unit test to find this:
NHibernate.HibernateException: Creating a proxy instance failed ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B).
This happened when I tried to serialize an object graph using WCF and my handy NHibernate data contract surrogate. Strangely, if I set breakpoints in certain places the exception would not occur but proxies elsewhere would behave strangely, e.g. lose property values.
Poking around on the Castle DynamicProxy forums, there were mentions of this being due to an abstract class being proxied. I turned off lazy initialization of classes that had abstract bases, and yet the problem persisted. Then, on a clue that DynamicProxy lacked support for proxying generic classes, I removed the "virtual" keyword from what looked like an innocuous method:
protected virtual IEnumerable<T> GetSubset<T>()
{
. . .
}
Simply removing the "virtual" keyword fixed the problem. So be warned: no generic methods on your proxy classes, at least not until DynamicCastle 2.0!
Tim