Exercise: Content Age Restriction

Create and read custom attributes to enforce age restrictions on social media posts.

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 the AgeRestrictedAttribute class so it accepts a minimumAge integer in its constructor. Restrict the attribute so it can only be applied to classes.

  • In GraphicPost.cs: Decorate the GraphicPost class with a minimum age requirement of 18. (The NewsPost.cs file is fully implemented and requires no age restriction).

  • In Program.cs: Implement the CanUserViewPost(object post, int userAge) method to read the attribute at runtime. Return true if the user is old enough, and return false if they are not. If a post has no age restriction attribute, return true.

Constraints

  • Your attribute class must inherit from the Attribute base class.

  • Use [AttributeUsage(AttributeTargets.Class)] to restrict the attribute.

  • Use GetCustomAttribute<AgeRestrictedAttribute>() on the post’s Type to 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 the Type of the post parameter before you can call GetCustomAttribute.

  • If GetCustomAttribute returns null, it means the class is not restricted, and the user should be allowed to view the post.

Exercise: Content Age Restriction

Create and read custom attributes to enforce age restrictions on social media posts.

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 the AgeRestrictedAttribute class so it accepts a minimumAge integer in its constructor. Restrict the attribute so it can only be applied to classes.

  • In GraphicPost.cs: Decorate the GraphicPost class with a minimum age requirement of 18. (The NewsPost.cs file is fully implemented and requires no age restriction).

  • In Program.cs: Implement the CanUserViewPost(object post, int userAge) method to read the attribute at runtime. Return true if the user is old enough, and return false if they are not. If a post has no age restriction attribute, return true.

Constraints

  • Your attribute class must inherit from the Attribute base class.

  • Use [AttributeUsage(AttributeTargets.Class)] to restrict the attribute.

  • Use GetCustomAttribute<AgeRestrictedAttribute>() on the post’s Type to 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 the Type of the post parameter before you can call GetCustomAttribute.

  • If GetCustomAttribute returns null, it means the class is not restricted, and the user should be allowed to view the post.

C# 14.0
namespace SocialPlatform;
// 4. Decorate this class to require a minimum age of 18
public class GraphicPost
{
public string Content { get; set; } = string.Empty;
}