Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/fundamentals/code-analysis/quality-rules/ca1060.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ The following example shows an **Interaction.Beep** method that wraps the **Mess

## SafeNativeMethods example

P/Invoke methods that can be safely exposed to any application and that do not have any side effects should be put in a class that is named **SafeNativeMethods**. You do not have to demand permissions and you do not have to pay much attention to where they are called from.
P/Invoke methods that can be safely exposed to any application and that do not have any side effects should be put in a class that is named **SafeNativeMethods**. You do not have to pay much attention to where they are called from.

The following example shows an **Environment.TickCount** property that wraps the **GetTickCount** function from kernel32.dll.

Expand All @@ -83,7 +83,7 @@ The following example shows an **Environment.TickCount** property that wraps the

## UnsafeNativeMethods example

P/Invoke methods that cannot be safely called and that could cause side effects should be put in a class that is named **UnsafeNativeMethods**. These methods should be rigorously checked to make sure that they are not exposed to the user unintentionally. Alternatively, the methods should have another permission that is demanded instead of **UnmanagedCode** when they use them.
P/Invoke methods that cannot be safely called and that could cause side effects should be put in a class that is named **UnsafeNativeMethods**. These methods should be rigorously checked to make sure that they are not exposed to the user unintentionally.

The following example shows a **Cursor.Hide** method that wraps the **ShowCursor** function from user32.dll.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# All files
[*]

# CS0659: Type overrides Object.Equals(object o) but does not override Object.GetHashCode()
dotnet_diagnostic.CS0659.severity = suggestion

# CS0108: Member hides inherited member; missing new keyword
dotnet_diagnostic.CS0108.severity = suggestion

# CA1822: Mark members as static
dotnet_diagnostic.CA1822.severity = none
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@
<PackageReference Include="System.Security.Permissions" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
<None Include=".editorconfig" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,8 @@ internal static class SafeNativeMethods
//<snippet4>
public static class Cursor
{
// Callers do not require UnmanagedCode permission, however,
// they do require UIPermissionWindow.AllWindows.
public static void Hide()
{
// Need to demand an appropriate permission
// in place of UnmanagedCode permission as
// ShowCursor is not considered a safe method.
new UIPermission(UIPermissionWindow.AllWindows).Demand();
UnsafeNativeMethods.ShowCursor(false);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace ca1802
using System;

namespace ca1802
{
//<snippet1>
// This class violates the rule.
Expand All @@ -7,6 +9,11 @@ public class UseReadOnly
static readonly int x = 3;
static readonly double y = x + 2.1;
static readonly string s = "readonly";

public void Print()
{
Console.WriteLine(s);
}
}

// This class satisfies the rule.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Reflection;
using System;
using System.Reflection;
using System.Resources;

namespace ca1810
Expand All @@ -16,6 +17,11 @@ static StaticConstructor()
new ResourceManager("strings", Assembly.GetExecutingAssembly());
resourceString = stringManager.GetString("string");
}

public void Print()
{
Console.WriteLine(someInteger);
}
}

public class NoStaticConstructor
Expand All @@ -29,6 +35,11 @@ static string InitializeResourceString()
new ResourceManager("strings", Assembly.GetExecutingAssembly());
return stringManager.GetString("string");
}

public void Print()
{
Console.WriteLine(someInteger);
}
}
//</snippet1>
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace ca2109
{
#pragma warning disable SYSLIB0003
//<snippet1>
public class HandleEvents
{
Expand All @@ -21,4 +22,5 @@ public static void SomeActionHappened(Object sender, EventArgs e)
}
}
//</snippet1>
#pragma warning restore SYSLIB0003
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ protected SerializationConstructorsRequired(
}

// The following method serializes the instance.
[SecurityPermission(SecurityAction.LinkDemand,
Flags = SecurityPermissionFlag.SerializationFormatter)]
void ISerializable.GetObjectData(SerializationInfo info,
StreamingContext context)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ protected BaseType(
baseValue = info.GetInt32("baseValue");
}

[SecurityPermissionAttribute(SecurityAction.Demand,
SerializationFormatter = true)]
public virtual void GetObjectData(
SerializationInfo info, StreamingContext context)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace ca2242
//<snippet1>
class NaNTests
{
static double zero;
static double zero = 0;

static void Main()
{
Expand Down