Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
brian-reichle opened this issue May 20, 2020 · 0 comments
Open

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

brian-reichle opened this issue May 20, 2020 · 0 comments

Comments

@brian-reichle
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant