gpt4 book ai didi

c# - 激活器.CreateInstance : Could not load type from assembly

转载 作者:太空狗 更新时间:2023-10-29 17:48:00 25 4
gpt4 key购买 nike

我正在尝试创建在我的项目中的插件 .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

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

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

这里要注意的一个重要部分是,我使用的是程序集的“短”名称,而不是“完整”名称(包括版本、文化和 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”中定义的类“Action”的实例。我在尝试创建实例的同一个 *.cs 文件的顶部将此程序集明确引用为 using 指令。

我已经尝试了以前问题/答案中的以下建议:

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

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

<强>1。确保程序集位于正确的工作目录中:

我们在这里...

It's right there!!!

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

是的...

Looks the same

<强>3。最终推荐使用 fuslogvw.exe。

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

Hmmm...

我查看了两个日志。

短名称版本似乎会产生一些警告:

=== 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.

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

还有什么想法吗?

编辑:这是 Action 类的最小定义。它纯粹是一个模型类。

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 接口(interface)和 [Key] 属性是另一个程序集的一部分。不确定这是否会产生影响?

编辑 2: 正如 Erik 在下面指出的,显然我也对这个 OTHER 程序集进行了完全相同的检查。

最佳答案

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

关于c# - 激活器.CreateInstance : Could not load type from assembly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28889117/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com