Activator.CreateInstance: 无法从程序集中加载类型

Activator.CreateInstance: Could not load type from assembly

我正在尝试创建一个 class 的实例,该实例在我的项目中的插件 .dll 中实现,以进行类型发现。我收到此异常:

Could not load type 'Action' from assembly 'SquidReports.DataCollector.Plugin.BES, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

这正是我正在使用的方法签名: https://msdn.microsoft.com/en-us/library/d133hta4(v=vs.110).aspx

换句话说,我试图根据程序集名称和 class 名称生成对象,如下所示:

object modelObject = Activator.CreateInstance((string)modelInfo.AssemblyName, (string)modelInfo.ModelName);

这里要注意的一个重要部分是,我使用的是程序集的 'Short' 名称,而不是 'full' 名称(包括 Version、Culture 和 PublicToken)。 但是,MSDN 明确指出:

'assemblyName' can be either of the following: The simple name of an assembly, without its path or file extension.

  • For example, you would specify TypeExtensions for an assembly whose path and name are .\bin\TypeExtensions.dll.

  • The full name of a signed assembly, which consists of its simple name, version, culture, and public key token; for example, "TypeExtensions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=181869f2f7435b51".

具体来说,我正在尝试创建在程序集 'SquidReports.DataCollector.Plugin.BES' 中定义的 class 'Action' 的实例。 我在试图创建实例的完全相同的 *.cs 文件的顶部将此程序集明确引用为 using 指令。

我尝试了之前 questions/answers 的以下建议:

清理你的解决方案,重建并重试 这似乎在某些 ASP.NET 项目中有效,但这是普通的旧控制台应用程序。

检查配置文件中引用的程序集 同样,这是一个简单的控制台应用程序,仅在同一解决方案的不同项目中使用 GAC 和库

1.确保程序集位于正确的工作目录中:

我们到了...

2。确保程序集与磁盘上的版本相同

是的...

3。最后的建议是使用 fuslogvw.exe.

我没有使用该工具的经验,但有一件事我确实觉得很奇怪。在 运行 调试会话中,出现了我的程序集的长名称和短名称版本:

我查看了两个日志。

短名称版本似乎确实产生了一些警告:

=== Pre-bind state information ===

LOG: DisplayName = SquidReports.DataCollector.Plugin.BES Partial) WRN: Partial binding information was supplied for an assembly:

WRN: Assembly Name: SquidReports.DataCollector.Plugin.BES | Domain ID: 1

WRN: A partial bind occurs when only part of the assembly display name is provided.

WRN: This might result in the binder loading an incorrect assembly.

WRN: It is recommended to provide a fully specified textual identity for the assembly,

WRN: that consists of the simple name, version, culture, and public key token.

WRN: See whitepaper http://go.microsoft.com/fwlink/?LinkId=109270 for more information and common solutions to this issue.

...但它以加载成功结束,并且在正确的位置清楚地提到了我的程序集:

LOG: Attempting download of new URL file:///C:/Source/C#/SquidReports/SquidReports.DataCollector/bin/x86/Debug/SquidReports.DataCollector.Plugin.BES.DLL.

长名称版本的日志不包含任何可疑消息。

还有什么想法吗?

编辑:这是操作的最小定义class。纯属模型-class.

public class Action : ICollectible
{
    public Action()
    {
        // Empty constructor
    }

    public Action(int actionID, string siteID, string name)
    {
        this.ActionID = actionID;
        this.SiteID = siteID;
        this.Name = name;
    }

    public int ID           { get; set; }   // Identity ID assigned by DB
    [Key]
    public int ActionID     { get; set; }   // Identity ID assigned by API
    public string SiteID    { get; set; }
    public string Name      { get; set; }
}

ICollectible 接口和 [Key] 属性是另一个程序集的一部分。不确定这是否会产生影响?

编辑 2: 正如埃里克在下面指出的那样,显然我也对这个其他程序集进行了完全相同的检查。

如您所知,正在加载程序集,只是找不到类型 (Action)。您必须指定类型的全名,Namespace.Action 以便 .NET 找到它。