C
C#2y ago
Ninshu

❔ 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
Aaron
Aaron2y ago
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.
Aaron
Aaron2y ago
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
mindhardt
mindhardt2y ago
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 warns
Accord
Accord2y ago
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.
Ninshu
NinshuOP2y ago
@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.
Aaron
Aaron2y ago
yeah the properties themselves need to be nullable
Accord
Accord2y ago
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.
Want results from more Discord servers?
Add your server