Actually CSharpCodeProvider
does not support DllExport
. And neither does the C# compiler Visual Studio uses.
The reason your code works in Visual Studio is because of the UnmanagedExports
NuGet package:
A set of compile-time libraries (nothing to deploy) and a build task that enable you to export functions from managed code to native applications.
The second part is important: for it to work, it requires a build task to run. So, if you install the package in Visual Studio, it will run that build task after normal compilation.
But if you just reference the DLL (you do that with CSharpCodeProvider
, but it would behave the same if you did just that with VS), it's not going to work.
So, if you want UnmanagedExports
to work with CSharpCodeProvider
, you will need to figure out some way to run the build task there too. Probably the simplest way would be to just copy the commands from the build task and run them using Process.Start()
.