1

So here is my issue. I have a complex archetecture of interfaces and abstract classes that I am trying to load up via Assembly.LoadFrom("x.dll"). When certain types that have an interface implementation where the implementation is explicit in a base class are trying to be loaded, I am getting a TypeLoadException saying:

Method 'MyMethod' in type 'MyPart2DerivedType' from assembly 'MyPart2Assembly, version...' does not have an implementation. I am trying to understand why this is as I have gone through several articles and have even attempted to delete the obj files and dlls manually. Here are the references to what I have done so far:

Solution to TypeLoadException

TypeLoadException says 'no implementation', but it is implemented

Visual Studio Forumns: TypeLoadException

Private accessors and explicit interface implementation

So here is my example code:

//This is in project 1
public interface IFooPart1
{
    void DoStuff();
}

//This is in project 2
public interface IFooPart2
{
    void DoOtherStuff();
}

//This is in project 3
public interface IFooPart3: IFooPart1, IFooPart2
{
    void DoEvenMoreStuff();
}

//This is in project 4
public abstract class MyBaseType: IFooPart1, IFooPart2
{
    void IFooPart1.DoStuff()
    {
        DoStuffInternal();
    }

    void IFooPart2.DoOtherStuff()
    {
        DoOtherStuffInternal();
    }
}

//This is in project 5
public class MyDerivedType: MyBaseType, IFooPart3
{
    public void DoEvenMoreStuff()
    {
        //Logic here...
    }
}

//Only has references to projects 1, 2, & 3 (only interfaces)
public class Program
{
    void Main(params string[] args)
    {
        //Get the path to the actual dll
        string assemblyDll = args[0];

        //Gets the class name to load (full name, eg: MyNameSpace.MyDerivedType)
        string classNameToLoad = args[1];

        //This part works...
        var fooAssembly = Assembly.LoadFrom(assemblyDll);

        //Here we throw a TypeLoadException stating
        // Method 'DoStuff' in type 'MyDerivedType' from assembly 'Project 5...' does
        //  not have an implementation.
        Type myDerivedTypeExpected = Assembly.GetType(classNameToLoad);
    }
}

Note: If I move the explicit implementation to MyDerivedType instead of MyBaseType it works... but I don't get why I would have to do that. Seems like I should be able to. This code is only an example, the actual code has a factory that returns the loaded class but only via the interface type. (eg: var myDerivedType = GetInstance();)

Community
  • 1
  • 1
iMortalitySX
  • 1,478
  • 1
  • 9
  • 23

2 Answers2

3

Okay for everyone that is interested in my stupid fix. Here was my problem:

Project6 (which was the console app) has PROJECT references to the other projects, not references to the dlls in the location that they are supposed to build to. The other projects actually were being built to a specific repository area. So, the console application was using it's own version of the dll's when it was trying to automatically load the dependancies. This evidently made some other type way down there that was being dynamically loaded to not be loaded because it was not in the same folder as the dlls that were there...

So in short, Assembly.LoadFrom might cause you to load an assembly twice, but .NET treats it like a different assembly!!! This may introduce some real odd errors when trying to dynamically load types!!!

Please learn from my frustration/mistake. Fiends don't let freinds DI alone (code review is key to catching this stupid stuff).

iMortalitySX
  • 1,478
  • 1
  • 9
  • 23
  • 2
    Good job finding that one out. Oh and please take the lesson with the test code home too: *When you can create a simple reproduction to post it on SO, you most likely will be able to fix it yourself.* I often start typing a question here and by the time I formulated it completely along with reproduction code, I figured out what the problem is. – Daniel Hilgarth Sep 10 '13 at 16:14
1

I've had a similar problem caused by one of my projects having a reference to an older version of a nuget package dependency. The older version didn't have an implementation for one of the methods.

Tom Robinson
  • 8,348
  • 9
  • 58
  • 102