Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.90 KB | None | 0 0
  1.     using System;
  2.     using System.Linq;
  3.     using System.Reflection;
  4.  
  5.     public class RouteBuilderAttribute : Attribute
  6.     {
  7.         public Type Type { get; }
  8.         public string PropName { get; }
  9.  
  10.         // cache methodInfo
  11.         private MethodInfo methodInfo;
  12.         public MethodInfo Method
  13.         {
  14.             get
  15.             {
  16.                 if(methodInfo == null)
  17.                 {
  18.                     PropertyInfo propInfo = Type.GetProperty(PropName);
  19.                     methodInfo = propInfo.GetGetMethod();
  20.                 }
  21.                 return methodInfo;
  22.             }
  23.         }
  24.  
  25.  
  26.         public RouteBuilderAttribute(Type type, string propName)
  27.         {
  28.             Type = type;
  29.             PropName = propName;
  30.         }
  31.     }
  32.  
  33.     public class Editor
  34.     {
  35.         public static Editor GetEditor(object o)
  36.         {
  37.             // dummy
  38.             return null;
  39.         }
  40.     }
  41.  
  42.     [RouteBuilder(typeof(RouteBuilder), "foo")]
  43.     public class MyEditor : Editor
  44.     {
  45.  
  46.     }
  47.  
  48.     public class RouteBuilderEditorWindow
  49.     {
  50.         private Type[] editorTypes = new Type[]
  51.             {
  52.                 typeof(MyEditor),
  53.             };
  54.  
  55.         private RouteBuilder routeBuilder = new RouteBuilder();
  56.  
  57.         private object GetObjectFromType(Type type, object obj)
  58.         {
  59.             object[] attributes = type.GetCustomAttributes(typeof(RouteBuilderAttribute), false);
  60.             RouteBuilderAttribute rba = attributes.Where(x => x is RouteBuilderAttribute) as RouteBuilderAttribute;
  61.             return rba.Method.Invoke(obj, null);
  62.         }
  63.  
  64.         private bool TryGetEditor(Type editorType, out Editor e)
  65.         {
  66.             object data = GetObjectFromType(editorType, routeBuilder);
  67.             e = Editor.GetEditor(data);
  68.             return true;
  69.         }
  70.     }
  71.  
  72.     public class RouteBuilder
  73.     {
  74.         public int foo;
  75.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement