C # Extension Method Oddities during Unit Test

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) { } } 
+4
source share
2 answers

Typically, MissingMethodException means that you load a different version of the DLL into the one you referenced at build time, and the actual dll that you load (at run time) does not have the method that the compiler found (at compile time).

Make sure that you somehow did not get different versions of the dll referenced by different projects. Perhaps when you run it in debug mode, some other code does the correct loading of the dll first, but when launched in context, this other code does not start, so the incorrect version is loaded instead.

This will be applied twice if the rejection method has recently been added, and therefore cannot be in the old version.

If you use full version control, you can see the debug output to see exactly which assembly is loading.

+5
source

I reflect here.

Put a restriction on the method to make sure it helps Pseudocode

public static bool IsTypeOf (this T item, O other) Where T: object, O: Type {}

Also, which class is this method in?

EDIT: This class, part of the assembly under test,

0
source

All Articles