hunger
hunger

Reputation: 423

Reference a DLL that is created during build by a 3rd party executable

I have a project that must reference and call a DLL which is created by executing a 3rd party executable every time a build is triggered. (A proxy-DLL that is created from a JAR file to enable calling Java-Code from within C#. Done using proxygen from jni4net, but that's not important. The JAR file might change from time to time, but its calling interface stays the same). I also need to instantiate an object from a class from the DLL and call methods on the object.

I have 2 approaches in mind:

1) Run it as a pre-build step in the same project. But this means I have to include a DLL in the repository and add that DLL as a reference to the project, so that C# knows that my calls are valid. And that means that I'll have a "stale" DLL in my repo and everytime a build is triggered, the SCM detects it as changed.

2) Create a dummy DLL project, e.g. with a dummy C# file, and then execute the program in a post-build step to overwrite the dummy DLL with the actual DLL. I could then add this as a normal project dependency. I would then have to implement dummy calls in the C# for the actual calls that are later in the "real" DLL so that the other project doesn't complain about the calls. But I wouldn't have to include a stale DLL in the repository.

Is there a better, more elegant solution?

Upvotes: 1

Views: 164

Answers (2)

Pavel Anikhouski
Pavel Anikhouski

Reputation: 23258

Since your 3rd party assembly is .NET one, you can place outside your source code repo, and load dynamically using Assembly.LoadFrom method. After that you can list a types from loaded assembly using GetTypes method Activator.CreateInstance(Type) can help to create an object using loaded type.

This is called reflection. You can also invoke a method or pass arguments to constructor, have a look at MSDN for some examples

Upvotes: 1

jojasicek
jojasicek

Reputation: 11

Load it using the Assembly.LoadFrom method. That should work!

Upvotes: 1

Related Questions