Bugzilla – Bug 517028
Strange LINQ Expression behaviour
Last modified: 2011-09-17 18:01:07 UTC
The simple code that follows shows two different problems detailed below. using System; using System.Linq.Expressions; class Test { public static void Main() { int? i = 0; Expression<Func<String>> e3 = Expression.Lambda<Func<String>>( Expression.Convert(Expression.Constant(i), typeof(String), typeof(Convert).GetMethod("ToString", new Type[1] { typeof(int?) }))); Console.WriteLine("SHOULD NOT WHIS BE NULL? " + typeof(Convert).GetMethod("ToString", new Type[1] { typeof(int?) })); Console.WriteLine(e3.Compile()()); } } Problems: While Convert does not have a ToString(Int32?) method, GetMethod returns a method with ToString(Object) as signature instead of null. The program fails during Compile() with a "System.InvalidProgramException: Invalid IL code" exception.
The problem is that our LINQ implementation should throw an exception because there is a parameter mismatch. .NET reports: InvalidOperationException: The operatnds for operator 'Convert' do not match the parameter of method 'ToString'. We instead silently compile incorrect code