Description
Describe the problem you are trying to solve
Currently as the CA2213 states it is only applied to fields that should be disposed.
When you now create an Auto-Implemented property and assign a disposable instance to it in the constructor CA2213 will not be raised. This because the field of the Auto-Implemented Property is hidden by the compiler. Unfortunately the same issue as with CA2213 is still applied.
Describe suggestions on how to achieve the rule
The rules and fixes are similar to CA2213 but should also apply to auto-implemented properties.
Instead of introducing a new rule it might also be useful to adjust CA2213 to take Auto-Implemented properties into account (optionally with an extra analyzer property so that users can configure that it should also be applied on AutoImplementedProperties). This might make the property more unclear since the title states it is only applied to fields.
Additional context
I searched the issue list to find whether this rule (adjustment) was previously suggested, but was unable to find anything about this. Since it has the same issues as the rule with the fields I am curious what the reason is that this is not (yet) implemented. Can imagine there are things that make it hard to implement (e.g. whether it is possible to detect the difference between a normal an an Auto-Implemented property).
Example
The following part is an adjustment of https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca2213
The following snippet shows a type TypeA that implements IDisposable.
public class TypeA : IDisposable
{
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
// Dispose managed resources
}
// Free native resources
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
// Disposable types implement a finalizer.
~TypeA()
{
Dispose(false);
}
}
The following snippet shows a type TypeB that violates rule CA22## by declaring an auto-implement property AnAutoImplementPropertyOfADisposableType as a disposable type (TypeA) and not calling Dispose on the auto-implemented property.
public class TypeB : IDisposable
{
private bool disposed = false;
// Assume this type has some unmanaged resources.
TypeA AnAutoImplementPropertyOfADisposableType { get; } = new TypeA();
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
// Dispose of resources held by this instance.
// Violates rule: DisposableAutoImplementPropertiesShouldBeDisposed.
// Should call AnAutoImplementPropertyOfADisposableType.Dispose();
disposed = true;
// Suppress finalization of this disposed instance.
if (disposing)
{
GC.SuppressFinalize(this);
}
}
}
public void Dispose()
{
if (!disposed)
{
// Dispose of resources held by this instance.
Dispose(true);
}
}
// Disposable types implement a finalizer.
~TypeB()
{
Dispose(false);
}
}
To fix the violation, call Dispose() on the disposable auto-implemented property:
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
// Dispose of resources held by this instance.
AnAutoImplementPropertyOfADisposableType.Dispose();
disposed = true;
// Suppress finalization of this disposed instance.
if (disposing)
{
GC.SuppressFinalize(this);
}
}
}