だからここに私の問題があります。Assembly.LoadFrom("x.dll") を介してロードしようとしているインターフェイスと抽象クラスの複雑なアーキテクチャがあります。基本クラスで実装が明示的であるインターフェイス実装を持つ特定の型をロードしようとすると、次のように TypeLoadException が発生します。
アセンブリ 'MyPart2Assembly、バージョン...' のタイプ 'MyPart2DerivedType' のメソッド 'MyMethod' には実装がありません。いくつかの記事を読み、obj ファイルと dll を手動で削除しようとしたこともあるため、これがなぜなのかを理解しようとしています。これまでに私が行ったことへの参照は次のとおりです。
TypeLoadException は「実装なし」と言っていますが、実装されています
Visual Studio フォーラム: TypeLoadException
だからここに私のコード例があります:
//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);
}
}
注:明示的な実装を MyBaseType ではなく MyDerivedType に移動すると動作します...しかし、なぜそうしなければならないのかわかりません。できるはずのようです。このコードは単なる例です。実際のコードには、ロードされたクラスを返すファクトリがありますが、インターフェイス タイプを介してのみです。(例: var myDerivedType = GetInstance();)