Reputation: 3391
I have two class libraries core
and plugins
and a WPF application that uses these two libraries. In core
I dynamically load the plugins
as follows:
try
{
Assembly assembly = Assembly.LoadFile("plugins.dll");
}
After I load the plugins.dll
I get the types in the plugins
that has implemented Node
abstract class from core
library, that is a class defined in the core
. This is the scenario that I used to develop and extensible application.
Somewhere in my core
library I need to traverse all fields of Node
classes loaded from plugins
. It works nice for all fields like int
, double
and other custom classes that are defined inside plugins
library.
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
}
But the problem is that in plugins
project, I also use Revit API, and when the above loop reaches to a field that comes from RevitAPI.dll
I get the following exception (I tried target platform Any and 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.
When I change the target platform in build section of all 3 projects to x64 I get this exception, instead:
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.
Upvotes: 1
Views: 2507
Reputation: 13329
The first error (System.BadImageFormatException
) is because your app, compiled with AnyCPU platform, is run in x86 mode by Visual Studio. RevitAPI.dll is a x64 mixed mode assembly, so it can not be loaded in an x86 process.
The second error (System.IO.FileNotFoundException
) is because RevitAPI.dll
can not load its dependencies. You can solve this by setting the ouput or the working directory to the installation directory of Revit (C:\Program Files\Autodesk\Revit Architecture 20xx\
). May be you can also P/Invoke SetDllDirectory
to add this directory to the search path.
Of course, like Augusto says, Revit is not running, the majority of calls will therefore fail. But you can use simple classes like XYZ
or UnitUtils
.
Upvotes: 4
Reputation: 8604
Revit API DLLs (RevitAPI.dll and RevitAPIUI.dll) were not designed to be loaded on external/standalone apps (.exe). You can only use them on Class Library (.dll) and load inside Revit as a pluging.
This happens because the API DLLs are actually a thin layer for the actual implementation. Therefore, you need Revit running to use them (as a plugin).
If you need to access Revit data from outside Revit (e.g. from an external app or export to a database), you can create a plugin, load on Revit, and from that plugin, expose the data you need. There are some events that can help, such as Idling event.
Upvotes: 4