❔ Field / Property Initialized By Method
Hello, I could have sworn I read somewhere in the .NET API's that there was an attribute you could use to tell the compiler that a property was initialized by a certain method.
If anyone knows the name of the attribute I would be eternally greatful. I am creating a blazor wasm application. It is annoying to have to keep setting my component properties to { get; set; } = null! when they are being set by
OnInitialized
or OnInitializedAsync
.7 Replies
MemberNotNullAttribute Class (System.Diagnostics.CodeAnalysis)
Specifies that the method or property will ensure that the listed field and property members have values that aren't null.
I believe
the actual property is nullable, then you place that attribute on a method, which will tell the code that it is no longer null after calling it
the problem is, that analysis doesn't carry when you say, pass this class into another method
so that context that you called the OnInitialized method and the value is no longer null is lost
For blazor components parameters specifically there is a
EditorRequiredAttribute
which is meant to provide design-time warnings if the parameter was not set and supress null assignment warnsWas this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.@Hin I was refering to a more general attribute not tied to an edit context.
If a value for this parameter is not provided, editors or build tools may provide warnings indicating the user to specify a value. This attribute is only valid on properties marked with ParameterAttribute.
@windows10ce thank you, I have been messing with that attribute as well as NotNull, it hasnt worked for me.yeah the properties themselves need to be nullable
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.