小编Jam*_*ock的帖子

(C#.Net Core)使用反射从外部程序集实例化类

我目前正在尝试开发一种在外部项目中使用反射以编程方式运行测试类的方法。这是应该显示我的问题的简化代码块。

string pathToDLL = @"C:\Path\To\Test\Project\UnitTests.dll";
IEnumerable<Type> testClasses = assembly.GetExportedTypes();
Type testClass = testClasses.First();
object testClassInstance = assembly.CreateInstance(testClass.FullName); 
Run Code Online (Sandbox Code Playgroud)

此代码引发以下异常:

'assembly.CreateInstance(testClass.FullName)' threw an exception of type 'System.Reflection.TargetInvocationException'
    Data: {System.Collections.ListDictionaryInternal}
    HResult: -2146232828
    HelpLink: null
    InnerException: {System.IO.FileNotFoundException: Could not load file or assembly 'Project.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.
File name: 'Project.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'
   at Project.UnitTests.TestClass..ctor()}
    Message: "Exception has been thrown by the target of an invocation."
    Source: "System.Private.CoreLib"
    StackTrace: "   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, …
Run Code Online (Sandbox Code Playgroud)

.net c# reflection initialization core

4
推荐指数
1
解决办法
3850
查看次数

标签 统计

.net ×1

c# ×1

core ×1

initialization ×1

reflection ×1