【发布时间】:2015-09-17 18:56:06
【问题描述】:
我有两个类库 core 和 plugins 以及一个使用这两个库的 WPF 应用程序。在core 我动态加载plugins 如下:
try
{
Assembly assembly = Assembly.LoadFile("plugins.dll");
}
加载plugins.dll 后,我从core 库中获得了plugins 中实现Node 抽象类的类型,即core 中定义的类。这是我用来开发和可扩展应用程序的场景。
在我的core 库中的某个地方,我需要遍历从plugins 加载的Node 类的所有字段。它适用于所有字段,如 int、double 和在 plugins 库中定义的其他自定义类。
theList = assembly.GetTypes().ToList().Where(t => t.BaseType == typeof(Node)).ToList();
var fieldInfos = theList[0].GetType().GetRuntimeFields();
foreach (var item in fieldInfos)
{
Type type = item.FieldType;
// Here I get exception for fields like XYZ that defined in
// Revit API though for fields like Int and double it works charm
}
但问题是,在plugins 项目中,我也使用 Revit API,当上述循环到达来自RevitAPI.dll 的字段时,我得到以下异常(我尝试了目标平台 Any 和 x86):
An unhandled exception of type 'System.BadImageFormatException' occurred in mscorlib.dll
Additional information: Could not load file or assembly 'RevitAPI,
Version=2015.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its
dependencies. An attempt was made to load a program with an incorrect format.
当我将所有 3 个项目的构建部分中的目标平台更改为 x64 时,我得到了这个异常:
An unhandled exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
Additional information: Could not load file or assembly 'RevitAPI.dll'
or one of its dependencies. The specified module could not be found.
【问题讨论】:
-
您是否在 WPF 应用程序 (.exe) 上的加载环上加载 Revit 上的 DLL?
-
@AugustoGoncalves 在我的 WPF 应用程序中,我使用
core库来加载plugins.dll,动态使用Assembly.LoadFile()。而plugins.dll本身引用了RevitAPI.dll -
那将无法工作...您只能在 Revit 中使用 RevitAPI.dll(作为插件)
-
@AugustoGoncalves 感谢您的评论。 Revit 开发人员是否故意通过设置安全权限或类似的方式阻止他人在其他应用程序中使用该 dll?
-
RevitAPI.dll 实际上只是实际实现的一个薄层,而不是库本身,这就是为什么它不能用于 Revit 内部的独立应用程序
标签: c# dll reflection .net-assembly revit-api