Monthly Archive for September, 2010

MSTest: Unit Test Adapter threw exception: Type is not resolved for member XXX

This was not an easy one… I was trying to run a unit test with MSTest but I was always getting the following error:
Unit Test Adapter threw exception: Type is not resolved for member ‘XXX,XXX Version=1.0.0.0, Culture=neutral, PublicKeyToken=null’

As usual in such case – a message which does not really say what’s wrong. I googled the problem but there was not much about it on the web. The best resource I found was post titled VSTS Unit Test ‘Type is not resolved’ exception. It describes how VSTestHost process runs the test and explains what the possible problem might be in this case.

The author suggests that data required for test (e.g. a dll file) is not found in base directory for AppDomain (i.e. unit test ‘Out’ directory) because it’s already switched back to directory that holds VSTestHost.exe. There are two links to MSDN given where Microsoft admits this is a known bug and provides a hack to work around the problem – supply VSTestHost with copies of required artifacts (again, this is described in details in above mentioned post).

Unfortunately that didn’t work with my case. I’ve found the root cause though…

Continue reading ‘MSTest: Unit Test Adapter threw exception: Type is not resolved for member XXX’

C#: GetHashCode() might cause OverflowException

Microsoft recommends if you overload Equals method you should also overload GetHashCode. Now, how to properly implement GetHashCode? There are many resources on the web that describe it. A good starting point might be this article on Stack Overflow.

Following MSDN guidlines GetHashCode must fulfill these requirements:

  • If two objects of the same type represent the same value, the hash function must return the same constant value for either object.
  • For the best performance, a hash function must generate a random distribution for all input.
  • The hash function must return exactly the same value regardless of any changes that are made to the object.

Sticking to first bullet, you (probably?) should consider the same fields in Equals and GetHashCode methods. Let’s have a look at the example in which I did so:

public class Contact
{
    public int ID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public override bool Equals(object obj)
    {
        // If parameter is null return false.
        if (obj == null)
        {
            return false;
        }

        // If parameter cannot be cast to Contact return false.
        Contact c = obj as Contact;
        if c == null)
        {
            return false;
        }

        // Return true if the fields match:
        return ID == c.ID
            && FirstName == c.FirstName
            && LastName == c.LastName;;
    }

    public override int GetHashCode()
    {
        return ID.GetHashCode()
            + FirstName.GetHashCode()
            + LastName.GetHashCode();
    }
}

Now, what is wrong with this example of GetHashCode? There’s one drawback here. The hash is calculated as a sum of three integer values, which might give a value that is greater than int.MaxValue and that will result in OverflowException.

Continue reading ‘C#: GetHashCode() might cause OverflowException’

It’s possible to support this blog

A few days ago I decided to add ‘Donate’ button on my blog (see the sidebar to the right). This is a feature provided by PayPal.

If you want to prove you are really delighted with my help or simply you have too much spare money icon smile Its possible to support this blog , it’s possible to pay me a kind of tip. That can be a dollar or two, that’s not really important; every little helps. All in all that could help pay for the hosting.

Of course I’m still blogging for fun so treat this as a feature – use it only if you really want to icon smile Its possible to support this blog