Advertisement
renix1

Just kidding with Attribute in C#

Nov 28th, 2019
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.56 KB | None | 0 0
  1. using System;
  2.  
  3. public class Program
  4. {
  5.     public static void Main()
  6.     {
  7.         Test t = new Test();
  8.         string name = t.callMe();
  9.         Console.WriteLine(name);
  10.     }
  11. }
  12.  
  13. [MyName("Reni")]
  14. public class Test {
  15.     public string callMe () {
  16.         return GetAttribute(typeof(Test));
  17.     }
  18.    
  19.     public string GetAttribute(Type t) {
  20.         MyName myname = (MyName) Attribute.GetCustomAttribute(t, typeof(MyName));
  21.         return myname.Name;
  22.     }
  23. }
  24.  
  25. public class MyName : System.Attribute {
  26.     public string Name { get; private set; }
  27.    
  28.     public MyName (string name) {
  29.         this.Name = name;
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement