Behavior of Assembly.GetTypes() changed in Visual Studio 2015
I opened our solution in Visual Studio 2015 yesterday and a few of our unit tests (which ran fine in Visual Studio 2013) starting failing. Digger deeper I discovered it was because calling GetTypes()
on an assembly was returning different results. I've been able to create a very simple test case to illustrate it.
In both Visual Studio 2013 and 2015 I created a new console application using .NET Framework 4.5.2. I put the following code in both projects.
class Program
{ static void Main(string[] args) { var types = typeof(Program).Assembly.GetTypes() .Where(t => !t.IsAbstract && t.IsClass); foreach (var type in types) { Console.WriteLine(type.FullName); } Console.ReadKey(); } }
When I run in Visual Studio 2013 I get the following output (as expected).
VS2013Example.Program
When I run in Visual Studio 2015 I get the following output (not as expected).
VS2015Example.Program
VS2015Example.Program+<>c
So what is that VS2015Example.Program+<>c
type? Turns out it's the lambda inside the .Where()
method. Yes, that's right, somehow that local lambda is being exposed as a type. If I comment out the .Where()
in VS2015 then I no longer get that second line.
I've used Beyond Compare to compare the two .csproj files but the only differences are the VS version number, the project GUID, names of the default namespace and assembly, and the VS2015 one had a reference to System.Net.Http that the VS2013 one didn't.
Has anyone else seen this?
Does anyone have an explanation as to why a local variable would be being exposed as a type at the assembly level?
Has anyone else seen this?
Yes, this is caused by the new compiler behavior for lifting lambda expressions.
Previously, if a lambda expression didn't capture any local variables, it would be cached as a static method at the call site, which made the compiler team need to jump some hoops in order to properly align the method arguments and the this
parameter. The new behavior in Roslyn is that all lambda expressions get lifted into a display class, where the delegate is exposed as an instance method in the display class, disregarding if it captures any local variables.
If you decompile your method in Roslyn, you see this:
private static void Main(string[] args) { IEnumerable<Type> arg_33_0 = typeof(Program).Assembly.GetTypes(); Func<Type, bool> arg_33_1; if (arg_33_1 = Program.<>c.<>9__0_0 == null) { arg_33_1 = Program.<>c.<>9__0_0 = new Func<Type, bool>(Program.<>c.<>9.<Main>b__0_0); } using (IEnumerator<Type> enumerator = arg_33_0.Where(arg_33_1).GetEnumerator()) { while (enumerator.MoveNext()) { Console.WriteLine(enumerator.Current.FullName); } } Console.ReadKey(); } [CompilerGenerated] [Serializable] private sealed class <>c { public static readonly Program.<>c <>9; public static Func<Type, bool> <>9__0_0; static <>c() { // Note: this type is marked as 'beforefieldinit'. Program.<>c.<>9 = new Program.<>c(); } internal bool <Main>b__0_0(Type t) { return !t.IsAbstract && t.IsClass;