Complete the code to declare a custom attribute class named MyAttribute.
public class [1] : System.Attribute {}
The custom attribute class must be named MyAttribute and inherit from System.Attribute.
Complete the code to apply the custom attribute MyAttribute to the class Sample.
[[1]] public class Sample {}
To apply the attribute, use its class name without the 'Attribute' suffix if desired, but here the full name MyAttribute is used.
Fix the error in the attribute constructor to accept a string parameter named description.
public class MyAttribute : System.Attribute { public string Description { get; } public MyAttribute([1]) { Description = description; } }
The constructor must accept a string parameter named description to assign it to the property.
Fill both blanks to create a dictionary using ToDictionary that maps property names to their types for properties with type string.
var stringProps = typeof(MyAttribute).GetProperties()
.Where(prop => prop.PropertyType == typeof(string))
.ToDictionary(prop => [1], prop => [2]);The dictionary keys are property names (prop.Name) and values are their types (prop.PropertyType).
Fill all three blanks to create a method that returns the description from the MyAttribute applied to a class.
public static string GetDescription(Type t) {
var attr = (MyAttribute)Attribute.GetCustomAttribute(t, typeof([1]));
return attr == null ? [2] : attr.[3];
}The method gets the MyAttribute from the type, returns empty string if none, else returns the Description property.