Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Reflection;
- public class RouteBuilderAttribute : Attribute
- {
- public Type Type { get; }
- public string PropName { get; }
- // cache methodInfo
- private MethodInfo methodInfo;
- public MethodInfo Method
- {
- get
- {
- if(methodInfo == null)
- {
- PropertyInfo propInfo = Type.GetProperty(PropName);
- methodInfo = propInfo.GetGetMethod();
- }
- return methodInfo;
- }
- }
- public RouteBuilderAttribute(Type type, string propName)
- {
- Type = type;
- PropName = propName;
- }
- }
- public class Editor
- {
- public static Editor GetEditor(object o)
- {
- // dummy
- return null;
- }
- }
- [RouteBuilder(typeof(RouteBuilder), "foo")]
- public class MyEditor : Editor
- {
- }
- public class RouteBuilderEditorWindow
- {
- private Type[] editorTypes = new Type[]
- {
- typeof(MyEditor),
- };
- private RouteBuilder routeBuilder = new RouteBuilder();
- private object GetObjectFromType(Type type, object obj)
- {
- object[] attributes = type.GetCustomAttributes(typeof(RouteBuilderAttribute), false);
- RouteBuilderAttribute rba = attributes.Where(x => x is RouteBuilderAttribute) as RouteBuilderAttribute;
- return rba.Method.Invoke(obj, null);
- }
- private bool TryGetEditor(Type editorType, out Editor e)
- {
- object data = GetObjectFromType(editorType, routeBuilder);
- e = Editor.GetEditor(data);
- return true;
- }
- }
- public class RouteBuilder
- {
- public int foo;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement