You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Copy file name to clipboardExpand all lines: Documentation/coding-guidelines/interop-guidelines.md
+19-19Lines changed: 19 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,9 +5,9 @@ Interop Guidelines
5
5
We have the following goals related to interop code being used in CoreFX:
6
6
7
7
- Minimize code duplication for interop.
8
-
- We should only define a given interop signature in a single place. This stuff is tricky, and we shouldn’t be copy-and-pasting it.
9
-
- Minimize unnecessary IL in assemblies.
10
-
- Interop signatures should only be compiled into the assemblies that actually consume them. Having extra signatures bloats assemblies and makes it more difficult to do static analysis over assemblies to understand what they actually use. It also leads to problems when such static verification is used as a gate, e.g. if a store verifies that only certain APIs are used by apps in the store.
8
+
- We should only define a given interop signature in a single place. This stuff is tricky, and we shouldn't be copy-and-pasting it.
9
+
- Minimize unnecessary IL in assemblies.
10
+
- Interop signatures should only be compiled into the assemblies that actually consume them. Having extra signatures bloats assemblies and makes it more difficult to do static analysis over assemblies to understand what they actually use. It also leads to problems when such static verification is used as a gate, e.g. if a store verifies that only certain APIs are used by apps in the store.
11
11
- Keep interop code isolated and consolidated.
12
12
- This is both for good hygiene and to help keep platform-specific code separated from platform-neutral code, which is important for maximizing reusable code above PAL layers.
13
13
@@ -16,28 +16,28 @@ We have the following goals related to interop code being used in CoreFX:
16
16
### Interop type
17
17
- All code related to interop signatures (DllImports, interop structs used in DllImports, constants that map to native values, etc.) should live in a partial, static, and internal “Interop” class in the root namespace, e.g.
18
18
```C#
19
-
internalstaticpartialclassInterop { … }
19
+
internalstaticpartialclassInterop { ... }
20
20
```
21
21
22
-
- Declarations shouldn’t be in Interop directly, but rather within a partial, static, internal nested type named for a given library or set of libraries, e.g.
22
+
- Declarations shouldn't be in Interop directly, but rather within a partial, static, internal nested type named for a given library or set of libraries, e.g.
23
23
```C#
24
24
internalstaticpartialclassInterop
25
25
{
26
-
internalstaticpartialclasslibc { … }
26
+
internalstaticpartialclasslibc { ... }
27
27
}
28
28
...
29
29
internalstaticpartialclassInterop
30
30
{
31
-
internalstaticpartialclassmincore { … }
31
+
internalstaticpartialclassmincore { ... }
32
32
}
33
33
```
34
34
- With few exceptions, the only methods that should be defined in these interop types are DllImports.
35
-
- Exceptions are limited to times when most or every consumer of a particular DllImport will need to wrap its invocation in a helper, e.g. to provide additional marshaling support, to hide thread-safety issues in the underlying OS implementation, to do any required manipulation of safe handles, etc. In such cases, the DllImport should be private whenever possible rather than internal, with the helper code exposed to consumers rather than having the DllImport exposed directly.
35
+
- Exceptions are limited to times when most or every consumer of a particular DllImport will need to wrap its invocation in a helper, e.g. to provide additional marshaling support, to hide thread-safety issues in the underlying OS implementation, to do any required manipulation of safe handles, etc. In such cases, the DllImport should be private whenever possible rather than internal, with the helper code exposed to consumers rather than having the DllImport exposed directly.
36
36
37
37
### File organization
38
-
- The Interop partial class definitions should live in Interop.*.cs files. These Interop.*.cs files should all live under Common rather than within a given assembly’s folder.
39
-
- The only exception to this should be when an assembly P/Invokes to its own native library that isn’t available to or consumed by anyone else, e.g. System.IO.Compression P/Invoking to clrcompression.dll. In such cases, System.IO.Compression should have its own Interop folder which follows a similar scheme as outlined in this proposal, but just for these private P/Invokes.
40
-
- Under Common\src\Interop, we’ll have a folder for each target platform, and within each platform, for each library from which functionality is being consumed. The Interop.*.cs files will live within those library folders, e.g.
38
+
- The Interop partial class definitions should live in Interop.*.cs files. These Interop.*.cs files should all live under Common rather than within a given assembly's folder.
39
+
- The only exception to this should be when an assembly P/Invokes to its own native library that isn't available to or consumed by anyone else, e.g. System.IO.Compression P/Invoking to clrcompression.dll. In such cases, System.IO.Compression should have its own Interop folder which follows a similar scheme as outlined in this proposal, but just for these private P/Invokes.
40
+
- Under Common\src\Interop, we'll have a folder for each target platform, and within each platform, for each library from which functionality is being consumed. The Interop.*.cs files will live within those library folders, e.g.
41
41
```
42
42
\Common\src\Interop
43
43
\Windows
@@ -50,7 +50,7 @@ internal static partial class Interop
50
50
\libc
51
51
... interop files
52
52
```
53
-
As shown above, platforms may be additive, in that an assembly may use functionality from multiple folders, e.g. System.IO.FileSystem’s Linux build will use functionality both from Unix (common across all Unix systems) and from Linux (specific to Linux and not available across non-Linux Unix systems).
53
+
As shown above, platforms may be additive, in that an assembly may use functionality from multiple folders, e.g. System.IO.FileSystem's Linux build will use functionality both from Unix (common across all Unix systems) and from Linux (specific to Linux and not available across non-Linux Unix systems).
54
54
55
55
- Interop.*.cs files are created in a way such that every assembly consuming the file will need every DllImport it contains.
56
56
- If multiple related DllImports will all be needed by every consumer, they may be declared in the same file, named for the functionality grouping, e.g. Interop.IOErrors.cs.
@@ -66,7 +66,7 @@ As shown above, platforms may be additive, in that an assembly may use functiona
66
66
```
67
67
68
68
- If structs/constants will be used on their own without an associated DllImport, or if they may be used with multiple DllImports not in the same file, they should be declared in a separate file.
69
-
- In the case of multiple overloads of the same DllImport (e.g. some overloads taking a SafeHandle and others taking an IntPtr, or overloads taking different kinds of SafeHandles), if they can’t all be declared in the same file (because they won’t all be consumed by all consumers), the file should be qualified with the key differentiator, e.g.
69
+
- In the case of multiple overloads of the same DllImport (e.g. some overloads taking a SafeHandle and others taking an IntPtr, or overloads taking different kinds of SafeHandles), if they can't all be declared in the same file (because they won't all be consumed by all consumers), the file should be qualified with the key differentiator, e.g.
70
70
71
71
```
72
72
\Common\src\Interop
@@ -76,7 +76,7 @@ As shown above, platforms may be additive, in that an assembly may use functiona
76
76
\Interop.DuplicateHandle_IntPtr.cs
77
77
```
78
78
79
-
- The library names used per-platform are stored in internal constants in the Interop class in a private Libraries class in a per-platform file named Interop.Libraries.cs. These constants are then used for all DllImports to that library, rather than having the string duplicated each time, e.g.
79
+
- The library names used per-platform are stored in internal constants in the Interop class in a private Libraries class in a per-platform file named Interop.Libraries.cs. These constants are then used for all DllImports to that library, rather than having the string duplicated each time, e.g.
80
80
```C#
81
81
internalstaticpartialclassInterop// contents of Common\src\Interop\Windows\Interop.Libraries.cs
82
82
{
@@ -93,7 +93,7 @@ internal static partial class Interop // contents of Common\src\Interop\Windows\
93
93
}
94
94
}
95
95
```
96
-
(Note that this will likely result in some extra constants defined in each assembly that uses interop, which minimally violates one of the goals, but it’s very minimal.)
96
+
(Note that this will likely result in some extra constants defined in each assembly that uses interop, which minimally violates one of the goals, but it's very minimal.)
97
97
98
98
- .csproj project files then include the interop code they need, e.g.
99
99
```XML
@@ -127,7 +127,7 @@ You should not test the value of the OSGroup property directly, instead use one
127
127
Whenever possible, a single .csproj should be used per assembly, spanning all target platforms, e.g. System.Console.csproj includes conditional entries for when targeting Windows vs when targeting Linux. A property can be passed to msbuild to control which flavor is built, e.g. msbuild /p:OSGroup=OSX System.Console.csproj.
128
128
129
129
### Constants
130
-
- Wherever possible, constants should be defined as "const". Only if the data type doesn’t support this (e.g. IntPtr) should they instead be static readonly fields.
130
+
- Wherever possible, constants should be defined as "const". Only if the data type doesn't support this (e.g. IntPtr) should they instead be static readonly fields.
131
131
132
132
- Related constants should be grouped under a partial, static, internal type, e.g. for error codes they'd be grouped under an Errors type:
133
133
```C#
@@ -153,8 +153,8 @@ internal static partial class Interop
153
153
}
154
154
}
155
155
```
156
-
Using enums instead of partial, static classes can lead to needing lots of casts at call sites and can cause problems if such a type needs to be split across multiple files (enums can’t currently be partial). However, enums can be valuable in making it clear in a DllImport signature what values are permissible. Enums may be used in limited circumstances where these aren’t concerns: the full set of values can be represented in the enum, and the interop signature can be defined to use the enum type rather than the underlying integral type.
156
+
Using enums instead of partial, static classes can lead to needing lots of casts at call sites and can cause problems if such a type needs to be split across multiple files (enums can't currently be partial). However, enums can be valuable in making it clear in a DllImport signature what values are permissible. Enums may be used in limited circumstances where these aren't concerns: the full set of values can be represented in the enum, and the interop signature can be defined to use the enum type rather than the underlying integral type.
157
157
158
158
## Naming
159
-
- Interop signatures / structs / constants should be defined using the same name / capitalization / etc. that’s used in the corresponding native code.
160
-
- We should not rename any of these based on managed coding guidelines. The only exception to this is for the constant grouping type, which should be named with the most discoverable name possible; if that name is a concept (e.g. Errors), it can be named using managed naming guidelines.
159
+
- Interop signatures / structs / constants should be defined using the same name / capitalization / etc. that's used in the corresponding native code.
160
+
- We should not rename any of these based on managed coding guidelines. The only exception to this is for the constant grouping type, which should be named with the most discoverable name possible; if that name is a concept (e.g. Errors), it can be named using managed naming guidelines.
0 commit comments