An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)

19 במרץ 2007

4 תגובות

The informative error message:

An unhandled exception of type 'System.BadImageFormatException' occurred in .dll

Additional information: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)

The detailed inner exception:

The code that caused the compiler to produce bad IL code:

    public class ActiveRecordForApp<T> : ActiveRecordBase<T>

    where T : class

    {

    }

Here's the Fix:

    public class ActiveRecordForApp<T> : ActiveRecordBase<T>

    where T : class

    {

    }

Yes, it's 100% true. If you put a generic constraint "T: class" then your code will generate bad IL which is completely undebugable. So why use a generic constraint "T : class" you ask? Because IT'S WRITTEN ON MSDN WITH NO WARNING!!!

http://msdn2.microsoft.com/en-us/library/d5x73970.aspx

Can someone please change this MSDN documentation to not encourage programmers to produce faulty IL?

The official fix & "workaround" can be found on the microsoft connect Website:

https://connect.microsoft.com/feedback/viewfeedback.aspx?FeedbackID=93389&wa=wsignin1.0&siteid=210 

הוסף תגובה
facebook linkedin twitter email

כתיבת תגובה

האימייל לא יוצג באתר. (*) שדות חובה מסומנים

4 תגובות

  1. Ayende Rahien19 במרץ 2007 ב 19:43

    You are making too much of it, you should wait to get ExecutionEngineException, those are fun to track down.
    I had the chance to do a lot of really generics internal works lately, and I am impressed that it works in the first place

    להגיב
  2. tikotal22 במרץ 2007 ב 8:43

    I'm not sure if I understood you correctly.
    The example provided by MSDN does not direct you to use the a constraint of class but to
    use a specific constaint such as 'string'.
    However, I do agree that MSDN does not warn
    about using a 'class' constraint, But why should you?

    http://tikotal.blogspot.com/2007/03/attempt-was-made-to-load-program-with.html

    להגיב
  3. Justin-Josef Angel22 במרץ 2007 ב 16:55

    public T myMethodThatNeedsToReturnNullInSomeGenericParameter()
    {
    return null;
    }
    will not compile unless you specify T is a reference type (so it could return null). this will compile:
    public T myMethodThatNeedsToReturnNullInSomeGenericParameter
    ()
    where T : class
    {
    return null;
    }

    however, if T is an int and the return type for the method is an int you cannot return a null value and the conversion will not compile.

    this is how to make the code work in the current state of affairs:
    public T myMethodThatNeedsToReturnNullInSomeGenericParameter()
    {
    return (T)((object)null);
    }

    להגיב
  4. Dmitry11 ביוני 2007 ב 11:49

    There's no need to use boxing here:

    public T myMethodThatNeedsToReturnNullInSomeGenericParameter()

    {

    return (T)((object)null);

    }

    Type defauld works better:
    public T myMethodThatNeedsToReturnNullInSomeGenericParameter()

    {

    return default(T);

    }

    להגיב