Helper method to remove all annotations of type
- Dominant language
- C#
- Stars
- 6.3k
- Forks
- 991
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 196
Description
### Is there an existing issue for this?
- [x] I have searched the existing issues
### Is your feature request related to a problem? Please describe the problem.
I somewhat commonly find myself trying to remove all annotations of a type from a resource. This requires some boiler plate code, as well as a possible footgun if you forget to materialise the annotations to remove before you strt iterating over the collection to remove them.
I can also see this pattern semi commonly in the aspire code base.
```cs
var roleAssignmentAnnotations = azureResource.Annotations.OfType().ToArray();
foreach (var annotation in roleAssignmentAnnotations)
{
azureResource.Annotations.Remove(annotation);
}
```
```cs
resource.Resource.Annotations.OfType()
.Where(w => w.Resource == existingResource.Resource)
.ToList()
.ForEach(w => resource.Resource.Annotations.Remove(w));
```
```cs
if (builder.Resource.Annotations.OfType().SingleOrDefault() is { } containerAnnotation)
{
builder.Resource.Annotations.Remove(containerAnnotation);
}
```
### Describe the solution you'd like
I'd like to see an extension method that encapsulates this pattern
```cs
public static bool RemoveAnnotationsOfType(this IResource resource) where T : IResourceAnnotation
{
var toRemove = resource.Annotations.OfType().ToArray();
foreach (var annotation in toRemove)
{
resource.Annotations.Remove(annotation);
}
return toRemove.Length > 0;
}
```
### Additional context
_No response_
Contributor guide
Assessment
This issue has not been assessed yet.