Created
July 14, 2019 10:30
-
-
Save luisdeol/c7d74df57bb2aa8b8d2526fb930eb273 to your computer and use it in GitHub Desktop.
2.5 - Creating and accessing Attributes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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