Skip to content

Don't look for an attribute where it's not supported. #233

Open
@brian-reichle

Description

@brian-reichle

Another idea for an analyzer, warn when looking for an attribute on something where the AttributeUsage says it can't be.

eg.
given

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface)]
sealed class FooAttribute : Attribute
{
}
MethodInfo methodInfo = ...
Type type = ...
var x = info.IsDefined(typeof(FooAttribute)); // this should warn because FooAttribute cannot be applied to a method.
var y = type.IsDefined(typeof(FooAttribute)); // this is fine because FooAttribute can be applied to one or more of the type targets (Class, Delegate, Enum, Interface, GenericParameter, Struct).

Should also apply to GetCustomAttributes(Type, Boolean) as well as the GetCustomAttribute<>() and GetCustomAttributes<>() extension methods.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions