rspec/rules/S4052/csharp/rule.adoc

62 lines
2.0 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2024-08-09 16:15:11 +02:00
With the advent of .NET Framework 2.0, certain practices and types have become obsolete.
2021-04-28 16:49:39 +02:00
2024-08-09 16:15:11 +02:00
In particular, exceptions should now extend `System.Exception` instead of `System.ApplicationException`. Similarly, generic collections should be used instead of the older, non-generic, ones. Finally when creating an XML view, you should not extend `System.Xml.XmlDocument`.
2021-04-28 16:49:39 +02:00
This rule raises an issue when an externally visible type extends one of these types:
2024-08-09 16:15:11 +02:00
* https://learn.microsoft.com/en-us/dotnet/api/system.applicationexception[System.ApplicationException]
* https://learn.microsoft.com/en-us/dotnet/api/system.xml.xmldocument[System.Xml.XmlDocument]
* https://learn.microsoft.com/en-us/dotnet/api/system.collections.collectionbase[System.Collections.CollectionBase]
* https://learn.microsoft.com/en-us/dotnet/api/system.collections.dictionarybase[System.Collections.DictionaryBase]
* https://learn.microsoft.com/en-us/dotnet/api/system.collections.queue[System.Collections.Queue]
* https://learn.microsoft.com/en-us/dotnet/api/system.collections.readonlycollectionbase[System.Collections.ReadOnlyCollectionBase]
* https://learn.microsoft.com/en-us/dotnet/api/system.collections.sortedlist[System.Collections.SortedList]
* https://learn.microsoft.com/en-us/dotnet/api/system.collections.stack[System.Collections.Stack]
== How to fix it
2021-04-28 16:49:39 +02:00
2024-08-09 16:15:11 +02:00
=== Code examples
2024-08-09 16:15:11 +02:00
==== Noncompliant code example
2021-04-28 16:49:39 +02:00
2024-08-09 16:15:11 +02:00
[source,csharp,diff-id=1,diff-type=noncompliant]
2021-04-28 16:49:39 +02:00
----
using System;
using System.Collections;
namespace MyLibrary
{
public class MyCollection : CollectionBase // Noncompliant
{
}
}
----
2024-08-09 16:15:11 +02:00
==== Compliant solution
2024-08-09 16:15:11 +02:00
[source,csharp,diff-id=1,diff-type=compliant]
2021-04-28 16:49:39 +02:00
----
using System;
2024-08-09 16:15:11 +02:00
using System.Collections.ObjectModel;
2021-04-28 16:49:39 +02:00
namespace MyLibrary
{
public class MyCollection : Collection<T>
{
}
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Refactor this type not to derive from an outdated type '{0}'.
endif::env-github,rspecator-view[]