Using Visual Studio 2008 / C # / VS testing.
I have a very simple extension method that will tell me if an object has a specific type:
public static bool IsTypeOf<T, O>(this T item, O other) { if (!(item.GetType() is O)) return false; else return true; }
It will be called as:
Hashtable myHash = new Hashtable(); bool out = myHash.IsTypeOf(typeof(Hashtable));
The method works fine when I run the code in debug mode or debug my unit tests. However, the minute I simply run all the unit tests in context, I mysteriously get a MissingMethodException for this method. Oddly enough, another extension method in the same class has no problems.
I tend to the problem, being something other than the extension method itself. I tried to delete temporary files, close / open again / clear / rebuild solution, etc. So far nothing has happened.
Has anyone come across this anywhere?
Edit: This is a simplified code example. Basically, this is the smallest reproducible example that I could create without the baggage of the surrounding code. This individual method also throws the MissingMethodException isolated when typed into the unit test, as described above. This code does not fulfill the task, as John noted, it is rather the source of the exception with which I am currently concerned.
Decision. I tried many different things, agreeing with the brand of thinking that this is a reference problem. Removing links, cleaning / rebuilding, restarting Visual Studio did not help. Ultimately, I ended up looking for my hard drive for the compiled DLL and deleted it from all over the world, which made no sense. After deleting all instances, except those that were in the TestResults folder, I was able to restore and re-run unit tests.
As for the content of the method, in unit testing I found a problem and could never get this concept to work. Since O is a type of RunTimeType, I don't have much access to it, and I tried to use IsAssignableFrom () to return the function correctly. At this time, this feature was removed from my validation methods, which need to be reviewed at another time. However, before deleting this, I was still getting the original problem that started this post with a variety of other methods.
Post-solution: the actual method was not as complicated as I did. Here is the actual working method:
public static void IsTypeOf<T>(this T item, Type type) { if (!(type.IsAssignableFrom(item.GetType()))) throw new ArgumentException("Invalid object type"); }
and unit test to check this:
[TestMethod] public void IsTypeOfTest() { Hashtable myTable = new Hashtable(); myTable.IsTypeOf(typeof(Hashtable)); try { myTable.IsTypeOf(typeof(System.String)); Assert.Fail("Type comparison should fail."); } catch (ArgumentException) { } }