.NET bug?


.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))

    // path = Path.GetFullPath(path);


    Type t =
            .Where(x => x.Name == "IFoo")

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

public interface IFoo {}

This prints:

typeof (IFoo)
typeof (IFoo)

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):

typeof (IFoo)
typeof (IFoo)

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.