Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
.NET code analyzer rule CA1417 is enabled, by default, starting in .NET 5. It produces a build warning for any Platform Invoke (P/Invoke) method definitions where a String parameter is passed by value and marked with OutAttribute.
Change description
Starting in .NET 5, the .NET SDK includes .NET source code analyzers. Several of these rules are enabled, by default, including CA1417. If your project contains code that violates this rule and is configured to treat warnings as errors, this change could break your build.
Rule CA1417 flags P/Invoke method definitions where a String parameter is marked with the OutAttribute attribute and is passed by value. For example:
[DllImport("MyLibrary")]
private static extern void PIMethod([Out] string s);
The .NET runtime maintains a table, called the intern pool, that contains a single reference to each unique literal string in a program. If an interned string marked with OutAttribute is passed by value to a P/Invoke method, the runtime can be destabilized. For more information about string interning, see the remarks for String.Intern(String).
Version introduced
5.0
Recommended action
If you need to marshal modified string data back to the caller, pass the string by reference instead.
[DllImport("MyLibrary")] private static extern void PIMethod(out string s);
If you don't need to marshal modified string data back to the caller, simply remove the OutAttribute.
[DllImport("MyLibrary")] private static extern void PIMethod(string s);
For more information, see CA1417.
To disable code analysis completely, set
EnableNETAnalyzers
tofalse
in your project file. For more information, see EnableNETAnalyzers.
Affected APIs
Not detectable via API analysis.