rspec/rules/S4214/rule.adoc

44 lines
999 B
Plaintext
Raw Normal View History

== Why is this an issue?
2021-01-27 13:42:22 +01:00
Methods marked with the ``++System.Runtime.InteropServices.DllImportAttribute++`` attribute use Platform Invocation Services to access unmanaged code and should not be exposed. Keeping them private or internal makes sure that their access is controlled and properly managed.
2020-06-30 12:49:37 +02:00
2021-02-02 15:02:10 +01:00
2021-01-27 13:42:22 +01:00
This rule raises an issue when a method declared with ``++DllImport++`` is public or protected.
2020-06-30 12:49:37 +02:00
=== Noncompliant code example
2020-06-30 12:49:37 +02:00
2022-02-04 17:28:24 +01:00
[source,text]
2020-06-30 12:49:37 +02:00
----
using System;
using System.Runtime.InteropServices;
namespace MyLibrary
{
public class Foo
{
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
public static extern bool RemoveDirectory(string name); // Noncompliant
}
}
----
=== Compliant solution
2020-06-30 12:49:37 +02:00
2022-02-04 17:28:24 +01:00
[source,text]
2020-06-30 12:49:37 +02:00
----
using System;
using System.Runtime.InteropServices;
namespace MyLibrary
{
public class Foo
{
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
private static extern bool RemoveDirectory(string name);
}
}
----