【发布时间】:2013-09-14 07:52:29
【问题描述】:
所以这是我的问题。我有一个复杂的接口和抽象类架构,我试图通过 Assembly.LoadFrom("x.dll") 加载它们。当某些具有接口实现的类型尝试加载基类中的显式实现时,我收到一个 TypeLoadException 说:
程序集“MyPart2Assembly,版本...”中的“MyPart2DerivedType”类型中的方法“MyMethod”没有实现。我试图理解为什么会这样,因为我已经阅读了几篇文章,甚至试图手动删除 obj 文件和 dll。以下是我迄今为止所做的参考:
TypeLoadException says 'no implementation', but it is implemented
Visual Studio Forumns: TypeLoadException
Private accessors and explicit interface implementation
这是我的示例代码:
//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);
}
}
注意:如果我将显式实现移动到 MyDerivedType 而不是 MyBaseType 它可以工作......但我不明白为什么我必须这样做。好像我应该可以。这段代码只是一个例子,实际的代码有一个工厂,它返回加载的类,但只能通过接口类型。 (例如:var myDerivedType = GetInstance();)
【问题讨论】:
-
你用什么参数调用这个程序?项目 4 和 5 中的程序集是否与 exe 位于同一文件夹中?
-
是的,但是 dll 是我传入的完整路径
-
请告诉我参数。
-
"c:\myassemblyfolder\Project5.dll" "MyNameSpace.MyDerivedType"
-
我会尝试在 Project 5 之前加载 Project 4 程序集
标签: c# interface dependency-injection .net-3.5