Advertisement
ivandrofly

Subtitle Edit: reflection reading attributes

Mar 16th, 2019
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.70 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace tester_plugin
  9. {
  10.  
  11.     interface IProgram
  12.     {
  13.         string Name { get; set; }
  14.  
  15.         string Action(string file, string raw);
  16.     }
  17.  
  18.  
  19.     [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
  20.     class InvokeOnLoadAttribute : Attribute
  21.     {
  22.         public InvokeOnLoadAttribute(bool invokeOnLoad)
  23.         {
  24.             InvokeOnLoad = invokeOnLoad;
  25.         }
  26.  
  27.         public bool InvokeOnLoad { get; }
  28.         public bool IsMultiThread { get; set; }
  29.     }
  30.  
  31.     [InvokeOnLoad(true, IsMultiThread = false)]
  32.     class MyClass : IProgram
  33.     {
  34.         public string Name { get; set; }
  35.  
  36.         public string Action(string file, string raw)
  37.         {
  38.             Console.WriteLine("Invoking...");
  39.  
  40.             return "all done!";
  41.         }
  42.     }
  43.  
  44.     class Program
  45.     {
  46.  
  47.         static void Main(string[] args)
  48.         {
  49.  
  50.             var myClass = new MyClass();
  51.  
  52.             // only use getcustomattributesdata in reflection only context
  53.             var attributeData = myClass.GetType().GetCustomAttributesData();
  54.             attributeData.First().
  55.             Console.ReadLine();
  56.             return;
  57.  
  58.             var programType = typeof(IProgram);
  59.             var bindings = BindingFlags.Public | BindingFlags.Instance |
  60.                 BindingFlags.DeclaredOnly | BindingFlags.ExactBinding;
  61.  
  62.             //foreach (MemberInfo memberInfo in programType.GetMembers(bindings))
  63.             //{
  64.             //    Console.WriteLine($"Member: {memberInfo.Name}, MemberType: {memberInfo.MemberType.ToString()}, Reflected-type-name: {memberInfo.ReflectedType.Name}");
  65.             //}
  66.  
  67.             foreach (MethodInfo methodInfo in programType.GetMethods(bindings))
  68.             {
  69.                 Console.WriteLine($"Member: {methodInfo.Name}, MemberType: {methodInfo.MemberType.ToString()}, Reflected-type-name: {methodInfo.ReflectedType.Name}");
  70.             }
  71.  
  72.             Console.ReadLine();
  73.         }
  74.  
  75.         private static void GetCustomAttribute()
  76.         {
  77.             var myClass = new MyClass();
  78.             var containsAttrib = myClass.GetType().GetCustomAttributes().Any(at =>
  79.             {
  80.                 var invokeOnLoad = at.GetType().GetProperty("InvokeOnLoad");
  81.                 if (invokeOnLoad == null)
  82.                 {
  83.                     return false;
  84.                 }
  85.  
  86.                 Console.WriteLine("attribute found!");
  87.  
  88.                 return (bool)invokeOnLoad.GetValue(at, null);
  89.             });
  90.  
  91.             Console.WriteLine(containsAttrib);
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement