Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- namespace tester_plugin
- {
- interface IProgram
- {
- string Name { get; set; }
- string Action(string file, string raw);
- }
- [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
- class InvokeOnLoadAttribute : Attribute
- {
- public InvokeOnLoadAttribute(bool invokeOnLoad)
- {
- InvokeOnLoad = invokeOnLoad;
- }
- public bool InvokeOnLoad { get; }
- public bool IsMultiThread { get; set; }
- }
- [InvokeOnLoad(true, IsMultiThread = false)]
- class MyClass : IProgram
- {
- public string Name { get; set; }
- public string Action(string file, string raw)
- {
- Console.WriteLine("Invoking...");
- return "all done!";
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- var myClass = new MyClass();
- // only use getcustomattributesdata in reflection only context
- var attributeData = myClass.GetType().GetCustomAttributesData();
- attributeData.First().
- Console.ReadLine();
- return;
- var programType = typeof(IProgram);
- var bindings = BindingFlags.Public | BindingFlags.Instance |
- BindingFlags.DeclaredOnly | BindingFlags.ExactBinding;
- //foreach (MemberInfo memberInfo in programType.GetMembers(bindings))
- //{
- // Console.WriteLine($"Member: {memberInfo.Name}, MemberType: {memberInfo.MemberType.ToString()}, Reflected-type-name: {memberInfo.ReflectedType.Name}");
- //}
- foreach (MethodInfo methodInfo in programType.GetMethods(bindings))
- {
- Console.WriteLine($"Member: {methodInfo.Name}, MemberType: {methodInfo.MemberType.ToString()}, Reflected-type-name: {methodInfo.ReflectedType.Name}");
- }
- Console.ReadLine();
- }
- private static void GetCustomAttribute()
- {
- var myClass = new MyClass();
- var containsAttrib = myClass.GetType().GetCustomAttributes().Any(at =>
- {
- var invokeOnLoad = at.GetType().GetProperty("InvokeOnLoad");
- if (invokeOnLoad == null)
- {
- return false;
- }
- Console.WriteLine("attribute found!");
- return (bool)invokeOnLoad.GetValue(at, null);
- });
- Console.WriteLine(containsAttrib);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement