Solution: Content Age Restriction

Review the solution to understand how to define, restrict, and dynamically validate custom attributes across multiple files.

Solution: Content Age Restriction

Review the solution to understand how to define, restrict, and dynamically validate custom attributes across multiple files.
C# 14.0
using System.Reflection;
using SocialPlatform;
var news = new NewsPost { Headline = "Local weather update" };
var graphic = new GraphicPost { Content = "Sensitive content here" };
Console.WriteLine($"16-year-old viewing News: {CanUserViewPost(news, 16)}");
Console.WriteLine($"16-year-old viewing Graphic Post: {CanUserViewPost(graphic, 16)}");
Console.WriteLine($"21-year-old viewing Graphic Post: {CanUserViewPost(graphic, 21)}");
static bool CanUserViewPost(object post, int userAge)
{
Type type = post.GetType();
var attribute = type.GetCustomAttribute<AgeRestrictedAttribute>();
if (attribute != null)
{
return userAge >= attribute.MinimumAge;
}
return true;
}