MaT*_*aTi 3 c# createinstance activator targetinvocationexception
我在使用反射实例化 DLL 文件中定义的类时遇到了一些麻烦。尽管代码看起来不错,但我在 Activator.CreateInstance 上得到了 TargetInvocationException。代码如下(目前只有一个 DLL 可用):
public Comparator() 
{
    string[] filePaths = Directory.GetFiles(@".\Extensions");
    for (int i = 0; i < filePaths.Length; ++i)
    {
        var asm = Assembly.LoadFrom(Path.GetFullPath(filePaths[i]));
        if (asm != null)
        {
            foreach (Type currType in asm.ExportedTypes)
            {
                if (!currType.IsAbstract && currType.IsPublic && typeof(SorterBase).IsAssignableFrom(currType))
                {
                    Debug.WriteLine(currType); //outputs: BubbleSort.Sorter
                    var instance = Activator.CreateInstance(currType); //TargetInvocationException
                }
            }
        }
    }
}
这是我试图实例化的类:
using SortingLibrary;
using SortingFunctions;
namespace BubbleSort
{
    public class Sorter : SorterBase //SorterBase is an abstract class defined in SortingLibrary
    {
        public Sorter()
        {
            SetFunction(Sorting.BubbleSort);
            AlgorithmName = @"Bubble Sort";
        }
    }
}
我设法防止该问题发生的唯一方法是在编译之前将加载的 .dll 添加到引用中。问题是,通常在编译期间没有办法告诉需要加载哪些库。我在这里缺少什么?
整个解决方案(虽然目前有点乱)可以在这里下载:http : //nazr.in/rOD
任何帮助将不胜感激!先感谢您。