Bug with IQueryable and yield syntax: System.BadImageFormatException "An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)"
I find a bug if you mix IQueryable and yield syntax.
After Diego has changed a little bit my sample to reproduce it, this is the code he sent to the C# team:
class Program
{
static void Main(string[] args)
{
try
{
GetCategoryProductsUsingQueryable(() => new MyProduct(), 1).ToList();
Console.WriteLine("Queryable succeeds");
}
catch (Exception e)
{
LogException(e);
}
}
private static void LogException(Exception e)
{
Console.WriteLine(e.GetType());
Console.WriteLine(e.Message);
}
private static List<Category> Categories =
new List<Category> {
new Category {
CategoryID = 1,
Products = new List<Product> {
new Product {
ProductID = 1 } } } };
static IEnumerable<P> GetCategoryProductsUsingQueryable<P>(Func<P> newP, int categoryId) where P : IProduct
{
var categ = Categories.AsQueryable().FirstOrDefault(c => c.CategoryID == categoryId);
if (categ == null)
yield break;
foreach (var p in categ.Products)
yield return Load(newP(), p);
}
static P Load<P>(P item, Product p) where P : IProduct
{
item.ID = p.ProductID;
return item;
}
}
public interface IProduct
{
int ID { get; set; }
}
public class MyProduct : IProduct
{
public int ID { get; set; }
}
public class Category
{
public int CategoryID { get; set; }
public List<Product> Products { get; set; }
}
public class Product
{
public int ProductID { get; set; }
}
In this case, the console output is:
System.BadImageFormatException
An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)