Microsoft Knowledge Base Email Alertz

KBAlertz.com: BUG: A BadImageFormatException exception may occur when you use a recursively defined constraint of a generic type in a .NET Framework 2.0-based application

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com

ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks




Article ID: 940164 - Last Review: July 30, 2007 - Revision: 1.0

BUG: A BadImageFormatException exception may occur when you use a recursively defined constraint of a generic type in a .NET Framework 2.0-based application

On This Page

SYMPTOMS

In a Microsoft .NET Framework 2.0-based application, you use a recursively defined constraint of a generic type. When you do this, a BadImageFormatException exception may occur at run time. This problem occurs only in certain scenarios.

CAUSE

This problem occurs because of an issue in the Microsoft Visual Studio 2005 common language runtime (CLR 2.0) components.

WORKAROUND

To work around this problem, remove the class constraint from the public static E GetClass<E> method.

Note Constraints are limits that are put on generic-type parameters.

To remove the class constraint from the public static E GetClass<E> method, change the method signature as follows.

Existing code

public static E GetClass<E>() where E : class, new()

Replacement code

public static E GetClass<E>() where E : new()

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the "Applies to" section.

MORE INFORMATION

For more information about generics in the CLR, visit the following Microsoft Developer Network (MSDN) Web site:
http://msdn.microsoft.com/msdnmag/issues/06/00/NET/default.aspx (http://msdn.microsoft.com/msdnmag/issues/06/00/NET/default.aspx)

Steps to reproduce the problem

  1. On a computer that has Visual Studio 2005 installed, create the following C# program.
    using System;
    
    public abstract class AbstractClass1<E> where E : AbstractClass1<E>, new()
    { }
    
    public class Class1 : AbstractClass1<Class1>
    { }
    
    public class Class2<E> where E : AbstractClass1<E>, new()
    {
        public E GetClass1()
        {
            return Factory.GetClass<E>();
        }
    }
    
    public class Factory
    {
        public static E GetClass<E>() where E : class, new()
        {
            return new E();
        }
    }
    
    public class Program
    {
        static void Main()
        {
            Console.WriteLine("Hit enter to continue..");
            Console.ReadLine();
            Class2<Class1> c2 = new Class2<Class1>();
            c2.GetClass1();
            Console.WriteLine("Hi");
        }
    }
    
  2. Compile the program, and then run the program. The BadImageFormatException exception occurs at run time when the c2.GetClass1 method is called.

APPLIES TO
  • Microsoft .NET Framework 2.0
  • Microsoft Visual Studio 2005 Standard Edition
  • Microsoft Visual Studio 2005 Professional Edition
  • Microsoft Visual Studio 2005 Express Edition
Keywords: 
kbtshoot kbbug KB940164
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Be the first to leave feedback, to help others about this knowledge base article.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please