.NET bug?

04/01/11

.NET bug?

I reported a bug in the .NET 4 Framework (technically the BCL) today. I’m still not sure if it is a bug.

Here’s a LINQPad program that shows it:

void Main()
{
    string path =
      (new Uri(Assembly.GetExecutingAssembly().CodeBase))
      .AbsolutePath;

    // path = Path.GetFullPath(path);

    Console.WriteLine(path);

    Type t =
          Assembly.LoadFile(path)
            .GetTypes()
            .Where(x => x.Name == "IFoo")
            .First();

    Console.WriteLine(t);
    Console.WriteLine(typeof(IFoo));
    Console.WriteLine(t == typeof(IFoo));
}

public interface IFoo {}

This prints:

C:/TEMP/LINQPad/bbcyralv/query_mqjugi.dll
typeof (IFoo)
typeof (IFoo)
False

Note the forward-slashes in the path. When loading an assembly dynamically and reflecting over it, the types appear to be equal but actually aren’t.

Now uncomment the line path = Path.GetFullPath(path):

C:\TEMP\LINQPad\bbcyralv\query_dhjbqv.dll
typeof (IFoo)
typeof (IFoo)
True

This converts the path to use backslashes. (LINQPad generates a temporary assembly, which is why the DLL name changed.) Now the types are equal!

I was writing this screwy code to unit test code that was using reflection and dynamic assembly loading. That’s why it’s attempting to reload its own assembly.

In the end I changed the code to use Assembly.GetExecutingAssembly().Location, which worked with both ReSharper’s test runner (where the other code seemed to be picking up the “wrong” DLL) and TeamCity.

Update - April 11, 2011

Microsoft considers this By Design. Personally I’d like to see it either (a) throw or (b) work.


Your Host: webmaster@truewill.net
Copyright © 2000-2013 by William Sorensen. All rights reserved.