Replies: 8 comments 15 replies
-
Using a single With a var isRequired = propertyInfo.GetCustomAttribute<RequiredMemberAttribute>(true) is not null; If the information is only available via a var isRequired =
propertyInfo.DeclaringType.GetCustomAttribute<RequiredMembersAttribute>(false)
.RequiredMembers.Any(m => m.Name == propertyInfo.Name); Though, as I wrote the above code I realised that I haven't handled inheritance - so the above would need to be modified to walk up the inheritance chain too.
Might the complexity of use via reflection push the balance across to preferring multiple attributes? Bonus question: If an interface declares a required property, would the metadata for classes or structs implementing that interfaced include the required'ness of the property? Or would reflection code need to walk the interface metadata too? |
Beta Was this translation helpful? Give feedback.
-
(was meant as a reply to @theunrepentantgeek's post) I agree, having the attribute on each member has the following pros:
|
Beta Was this translation helpful? Give feedback.
-
Is the "position" of the required modifier already set in stone or will there be a discussion whether it makes sense placing the required modifier before the "set" keyword? E.x. public virtual required int Prop2 { get; set; }
// vs:
public virtual int Prop2 { get; required set; } Personal I'd strongly prefer the latter. |
Beta Was this translation helpful? Give feedback.
-
I'm eagerly waiting for this new feature that should have a huge (hopefully positive) impact on my code. It occurred to me that adding required members might have side-effects on custom deserializers. Before, when deserializing, you could do this:
Now the order has to be strictly members first, instance last, and I'm not sure one can create the instance and assign members in separate operations. |
Beta Was this translation helpful? Give feedback.
-
Hahahaha, that picture had me falling off my chair. Good one. |
Beta Was this translation helpful? Give feedback.
-
What about the inverse? Is adding a required member to a type that doesn't have one a binary breaking change? Or adding a new required member to a type that already has required members? If not, what is to prevent existing consumers from now violating the contract? |
Beta Was this translation helpful? Give feedback.
-
Also, regarding accessibility of members, I think that required members should be able to be protected or private. |
Beta Was this translation helpful? Give feedback.
-
In that case, I agree with the LDM that the original spec of the
|
Beta Was this translation helpful? Give feedback.
-
https://github.com/dotnet/csharplang/blob/main/meetings/2022/LDM-2022-01-05.md
Agenda
Beta Was this translation helpful? Give feedback.
All reactions