For example I wanted to do the following:
namespace Test
{
static bool PerformTest()
{
bool result = false;
// Todo: do something
return result;
}
}
..and then call that function from another module:
Test::PerformTest();
..I get an error saying that PerformTest is not part of the namespace. If it were a class I’d put this down to a missing ‘public’ statement but as far as I can tell you can only make ref classes and structs public?
The static keyword means different things in different contexts. Here it means “no external linkage” which it the exact opposite you want. You’ll have to remove it.
It isn’t clear whether or not /clr is in effect when this code gets compiled. Let’s assume it is. The CLR has no support for free functions like this, it only supports class methods. The C++/CLI compiler deals with this by creating a dummy class named
<Module>in the global namespace, making a free function a method of that class that is static and has internal accessibility. Both the class name and the accessibility specifier keeps it well out of reach from a C# program. Reflection should work but I’ve never tried it.There is one back-door, you can export a free function just like you can in a native DLL project. Same syntax:
The C++/CLI compiler exports a stub with the same name as the function that any native code can call. The stub loads the CLR, if required, switches to managed code execution and calls the actual PerformTest() function. Any C# code can now pinvoke the function. The overhead is a bit silly but that should not matter much in a test scenario.