Shorthand version of EventArgs primary constructor #8462
Replies: 3 comments 5 replies
-
What you're asking has nothing to do with What you can do is use my source generator package (or write your own) and just make the type public partial class AlertEventArgs([Property]string message, [Property]string notificationid) : EventArgs; Here is what will be generated at the end for you automatically : public partial class AlertEventArgs : EventArgs
{
public string Message { get; }
public string Notificationid { get; }
AlertEventArgs(string message, string notificationid)
{
Message = message;
Notificaionid = notificationid;
}
} |
Beta Was this translation helpful? Give feedback.
-
It's basically inheriting record-like type from non-record class. |
Beta Was this translation helpful? Give feedback.
-
A source-generator could add these members for you. You'd just need to make the type partial. |
Beta Was this translation helpful? Give feedback.
-
When creating new EventArgs overloads I must write a code block like this.
public class AlertEventArgs(string message, string notificationid) : EventArgs { public string Message { get; } = message; public string NotificationId { get; } = notificationid; }
When I try to use the style like record types use it is nice and short but there where no generated properties in the lowered code.
public class AlertEventArgs(string Message, string Notificationid) : EventArgs;
Wouldn't it be nice if the second version creates the same experience much more compact? Maybe an extra Attribute to make it happen. Nice and clean.
https://sharplab.io/#gist:04c00f32a6c37fca899afb724076356d
Beta Was this translation helpful? Give feedback.
All reactions