Exercise: Content Age Restriction
Problem statement
A social platform must ensure underage users cannot view specific types of content. Different types of posts require different age minimums. Rather than hardcoding rules for every new post type, you will use custom attributes to apply age restrictions directly to the post classes.
Task requirements
We have provided boilerplate code spread across four files: AgeRestrictedAttribute.cs, NewsPost.cs, GraphicPost.cs, and Program.cs. You must implement the application logic across these files:
In
AgeRestrictedAttribute.cs: Define theAgeRestrictedAttributeclass so it accepts aminimumAgeinteger in its constructor. Restrict the attribute so it can only be applied to classes.In
GraphicPost.cs: Decorate theGraphicPostclass with a minimum age requirement of 18. (TheNewsPost.csfile is fully implemented and requires no age restriction).In
Program.cs: Implement theCanUserViewPost(object post, int userAge)method to read the attribute at runtime. Returntrueif the user is old enough, and returnfalseif they are not. If a post has no age restriction attribute, returntrue.
Constraints
Your attribute class must inherit from the
Attributebase class.Use
[AttributeUsage(AttributeTargets.Class)]to restrict the attribute.Use
GetCustomAttribute<AgeRestrictedAttribute>()on the post’sTypeto read the metadata dynamically.
Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.
Get hints
Do not forget to make the custom attribute class inherit from
Attribute.In
CanUserViewPost, you must first get theTypeof thepostparameter before you can callGetCustomAttribute.If
GetCustomAttributereturnsnull, it means the class is not restricted, and the user should be allowed to view the post.
Exercise: Content Age Restriction
Problem statement
A social platform must ensure underage users cannot view specific types of content. Different types of posts require different age minimums. Rather than hardcoding rules for every new post type, you will use custom attributes to apply age restrictions directly to the post classes.
Task requirements
We have provided boilerplate code spread across four files: AgeRestrictedAttribute.cs, NewsPost.cs, GraphicPost.cs, and Program.cs. You must implement the application logic across these files:
In
AgeRestrictedAttribute.cs: Define theAgeRestrictedAttributeclass so it accepts aminimumAgeinteger in its constructor. Restrict the attribute so it can only be applied to classes.In
GraphicPost.cs: Decorate theGraphicPostclass with a minimum age requirement of 18. (TheNewsPost.csfile is fully implemented and requires no age restriction).In
Program.cs: Implement theCanUserViewPost(object post, int userAge)method to read the attribute at runtime. Returntrueif the user is old enough, and returnfalseif they are not. If a post has no age restriction attribute, returntrue.
Constraints
Your attribute class must inherit from the
Attributebase class.Use
[AttributeUsage(AttributeTargets.Class)]to restrict the attribute.Use
GetCustomAttribute<AgeRestrictedAttribute>()on the post’sTypeto read the metadata dynamically.
Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.
Get hints
Do not forget to make the custom attribute class inherit from
Attribute.In
CanUserViewPost, you must first get theTypeof thepostparameter before you can callGetCustomAttribute.If
GetCustomAttributereturnsnull, it means the class is not restricted, and the user should be allowed to view the post.