6

Goal: To remove the compiler warning 'CS0649' (field is never assigned to) when I use my custom attribute.

I have a custom attribute (code below is just examples):

[AttributeUsage(AttributeTargets.Field)]
public class MyCustomAttribute : Attribute { }

I then use that attribute on a field:

[MyCustom]
private readonly SomeType someType;

My application will auto fill someType with a value so we don't need to worry about initializing it.

I will still get a squiggly line in Visual Studio under someType and the warning message "Field "someType" is never assigned to, and will always have it's default value null."

Is there an attribute or other means that I can add to MyCustomAttribute that will remove this compiler warning?

NOTE: I do not want to have to modify the field or type the field is within further. I simply want to add the attribute and the warning go away.

Shog9
  • 156,901
  • 35
  • 231
  • 235
Michael Puckett II
  • 6,586
  • 5
  • 26
  • 46
  • Possible duplicate of [Disable/suppress warning CS0649 in C# for a specific field of class](https://stackoverflow.com/questions/13726743/disable-suppress-warning-cs0649-in-c-sharp-for-a-specific-field-of-class) (although you can only suppress it in your own code - if you are distributing your Attribute in a library, there is no way to suppress it for others) – Gabriel Luci Dec 11 '18 at 19:07
  • 1
    @GabrielLuci I don't think that's a duplicate, the OP doesn't want to modify the field or class the field is within. That said I don't think the attribute itself can suppress the warning. – JSteward Dec 11 '18 at 19:21
  • @JSteward Right. – Gabriel Luci Dec 11 '18 at 19:24
  • Where will the value of `someType` be set? If you set the value in the constructor, the warning should go away. – Chris Dunaway Dec 11 '18 at 22:10
  • @ChrisDunaway There is a method of setting it in the constructor as well and it does. It mimicks the features of Springboot in JAVA where you can use attributes to inject. This is for a DI service and it works well but I need to remove those warnings for some instances. – Michael Puckett II Dec 12 '18 at 00:59
  • 1
    Do you already find a way to deal with it? i got the same problem. – MinRam Mar 08 '22 at 02:28
  • Sort of... the work arounds are not good enough but for me this was due to injection and attributes. The 'sort of' work around was for me to stick to pure constructor injection, which honestly is probably the best way to go anyway, but it's not a fix for this issue. MS is getting this warning in their own Blazor code if you use the [Inject] attribute instead of constructor injection. :/ – Michael Puckett II Mar 10 '22 at 22:50

2 Answers2

1

There are only two ways to get rid of a warning:

  1. Use #pragma warning disable/restore, or
  2. Suppress a warning for your entire project in the project Properties -> Build -> and put the warning number in 'Suppress warnings'.

But that's it.

But that's why it's a warning and not an error. Warnings are there to flag things that are fishy, but could very well be legitimate. A field that is never set within the class itself is fishy, but not necessarily an error.

The #pragma directives are the best way to get rid of them since it's explicitly shows that you've acknowledged the warning and deemed it unfounded.

Gabriel Luci
  • 38,328
  • 4
  • 55
  • 84
  • Thank you @GabrielLuci I'm aware of these solutions and was hoping to manage it in a way to suppress for the clients. I know Spring in Java does this and I was thinking there must be a way. I'll keep digging and if I come up with nothing I'll mark this as answered although something tells me we must be able to. I'm developing a framework that does DI using attributes and it works well l, but those warnings are something clients will not appreciate. – Michael Puckett II Dec 11 '18 at 19:52
  • It's also a good idea to add a comment as to why you needed a `#pragma warning disable` here so future maintainers know why you were suppressing a warning. – Chris Akridge Dec 11 '18 at 20:00
  • With your current implementation, there is no way. You can change you implementation though. For example, you could put your Attribute on the class and use an extension method to have a property-like object to the class. There's an example of that [here](https://stackoverflow.com/a/8331915/1202807). – Gabriel Luci Dec 11 '18 at 20:01
  • 1
    @GabrielLuci How does that help me suppress warnings? I don't want to add custom ones. I simply want to auto inject a field; therefore when the attribute is used the clients should not receive a warning saying it's not used. The DI container also does constructor injection, which removes the error, but for a just a field this shouldn't happen. It also works with Properties which removes the warning. I don't want to reduce the ability of the DI to remove the warning but for now I may have to or add proper documentation. – Michael Puckett II Dec 11 '18 at 20:10
  • It helps you suppress warnings by removing the reason for the warning :) But I realize it may not fit your other requirements. – Gabriel Luci Dec 11 '18 at 20:15
1

In case you are using Rider or Resharper, you can use MeansImplicitUseAttribute on your custom attribute:

[MeansImplicitUse(ImplicitUseKindFlags.Assign)]
[AttributeUsage(AttributeTargets.Field)]
public class MyCustomAttribute : Attribute { }
MHGameWork
  • 616
  • 5
  • 14