mirror of
https://github.com/m-lamonaca/dev-notes.git
synced 2025-04-06 10:56:41 +00:00
c#: add LibraryImport
source generator notes
This commit is contained in:
parent
195a1a2f3c
commit
5dbb0669b1
1 changed files with 21 additions and 1 deletions
|
@ -2913,7 +2913,7 @@ unsafe
|
|||
}
|
||||
```
|
||||
|
||||
### DllImport & Extern
|
||||
### External Code
|
||||
|
||||
The `extern` modifier is used to declare a method that is implemented externally.
|
||||
A common use of the extern modifier is with the `DllImport` attribute when using Interop services to call into _unmanaged_ code.
|
||||
|
@ -2923,3 +2923,23 @@ In this case, the method must also be declared as `static`.
|
|||
[DllImport("avifil32.dll")]
|
||||
private static extern void AVIFileInit();
|
||||
```
|
||||
|
||||
The `LibraryImport` attribute can be used to import external code and have the source generator handle the marshalling for the annotated function.
|
||||
|
||||
> **Note**: The source generator will still use the `DllImport` attribute in the generated code.
|
||||
|
||||
```cs
|
||||
// before
|
||||
public static class Native
|
||||
{
|
||||
[DllImport(nameof(Native), CharSet = CharSet.Unicode)]
|
||||
public extern static string ToLower(string str);
|
||||
}
|
||||
|
||||
// after
|
||||
public static class Native
|
||||
{
|
||||
[LibraryImport(nameof(Native), StringMarshalling = StringMarshalling.Utf16)]
|
||||
public static partial string ToLower(string str);
|
||||
}
|
||||
```
|
||||
|
|
Loading…
Add table
Reference in a new issue