Skip to content

Instantly share code, notes, and snippets.

@luisdeol
Created July 14, 2019 10:30
Show Gist options
  • Save luisdeol/c7d74df57bb2aa8b8d2526fb930eb273 to your computer and use it in GitHub Desktop.
Save luisdeol/c7d74df57bb2aa8b8d2526fb930eb273 to your computer and use it in GitHub Desktop.
2.5 - Creating and accessing Attributes
using System;
namespace _25RuntimeReflection
{
class Program
{
static void Main(string[] args)
{
// Checking if the Phrase property was set when using the custom attribute on UsingAttribute class.
var attribute = (MyCoolAttribute)Attribute.GetCustomAttribute(typeof(UsingAttribute), typeof(MyCoolAttribute));
Console.WriteLine(attribute.Phrase);
Console.Read();
}
}
class MyCoolAttribute : Attribute
{
public string Phrase { get; set; }
public MyCoolAttribute(string phrase)
{
Phrase = phrase;
}
}
[MyCool("Let's clear that certification!")]
class UsingAttribute
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment