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.
There are only two ways to get rid of a warning:
#pragma warning disable
/restore
, orBut 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With