-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Proposal] Allow C# to export functions (and constants) to native code #11771
Comments
Personally, I think that rather than a new keyword that I'd rather see another pseudo-attribute, [DllExport(ExportAs = "MyAdd")]
public static int Add(int x, int y) {
return x + y;
} |
@HaloFour: Yes, you are probably right with an (pseudo-)attribute usage to keep consistency with the Come to think of it, it would be easier to port code, which used the |
Similar feature request already exists: #1013. And yes, would be nice to have such feature. |
@tomrus88: Thank you very much Sir for mentioning it... I will look into the linked reference |
Attributes like this are why I was hoping Roslyn would have given us a compiler pipeline. Injecting extra extensions in such a case would be childs play |
Quote from page 386:
Well, just
That's all I have wanted to point out here. I hope one day C#/VB will have this. |
@OmegaExtern : I always use the IL Support extension for VS, but that is a bit tedious. I also hope that VB/C#/F#/J#/ ..... will have this feature |
FYI, #1013 is also a duplicate of this. I'd love to see this. |
There is something, which I have forgotten to mention while creating this issue: In C++, you can do something like this: extern "C" __declspec(dllexport) static DWORD MyConstant = 0x00000042; Is this possible to do in C#? [DllExport]
public const int MyConstant = 0x00000042; I think the required CIL code is something along these lines: .field public static int32 MyConstant at data_01
...
.data data_01 = int32(0x00000042) This could help me a lot by writing [DllExport]
public const int NvOptimusEnablement = 1; instead of .data data_01 = int32(1)
.class public auto ansi '<Module>'
{
.field public static int32 NvOptimusEnablement at data_01
} every time I need NVidia-Optimus-support for my .NET executable |
Moved to dotnet/csharplang#308 |
(The title nearly says it all)
I would like to propose the ability to export a function written in a C#-project to native code (C++ etc.) -- quasi the opposite of a P/Invoke-call.
As the CLR has this already implemented (See Serge Lindin's book about the .NET 2.0 Assembler, chapter 18, pages 380 and following) since v2.0, no CLR changes have to be made for this feature.
An "exported" function must be marked as
static
and should be non-private. A possible example could be:which would compile into something like this:
which could be called from other (native) asseblies.
If one would like to export the function under a different name (e.g. as
MyAdd
instead ofAdd
), it could be done as follows:Which would result in the following CIL-code:
Of course, all parameters and return values must be value types (native arrays, primitives, native structures or pointer).
For exporting constants, see this post
I know, that the the project
Unmanaged Exports
by Robert Giesecke exists, however I would like to see this feature implemented into the C#-language without the need of external references or extensions.The text was updated successfully, but these errors were encountered: