public StaticView() { Type thisType = this.GetType(); Type viewType = typeof(ViewAttribute); ViewAttribute viewAttr = thisType.GetCustomAttribute(viewType) as ViewAttribute; if (viewAttr != null) { Title = viewAttr.Title; _goBackMessage = viewAttr.GoBackMessage; } // Build the options from local methods decorated with ViewOption Type viewOptionType = typeof(ViewOptionAttribute); IEnumerable <MethodInfo> allOptionMethods = thisType.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); IEnumerable <MethodInfo> viewOptionMethods = allOptionMethods.Where(o => o.GetCustomAttribute(viewOptionType) != null); foreach (MethodInfo method in viewOptionMethods) { ViewOptionAttribute attr = method.GetCustomAttribute(viewOptionType) as ViewOptionAttribute; if (attr != null) { ParameterInfo[] methodParameters = method.GetParameters(); List <object> implementedMethodParameters = new List <object>(); foreach (ParameterInfo methodParameter in methodParameters) { if (methodParameter.HasDefaultValue) { implementedMethodParameters.Add(methodParameter.DefaultValue); } else if (Nullable.GetUnderlyingType(methodParameter.ParameterType) != null) { implementedMethodParameters.Add(null); } } bool useParameters = methodParameters.Length > 0 && implementedMethodParameters.Count == methodParameters.Length; OptionAction p = delegate() { thisType.InvokeMember( method.Name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod, null, this, useParameters ? implementedMethodParameters.ToArray() : null ); }; Options.Add(new Option( attr.Message, p, attr.Color) ); } } }
public DynamicView() { Type thisType = this.GetType(); Type viewType = typeof(ViewAttribute); ViewAttribute viewAttr = thisType.GetCustomAttribute(viewType) as ViewAttribute; if (viewAttr != null) { Title = viewAttr.Title; _goBackMessage = viewAttr.GoBackMessage; } // Build the options from local methods decorated with ViewOption Type viewOptionType = typeof(DynamicViewOptionAttribute); IEnumerable <MethodInfo> allMethods = thisType.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); IEnumerable <MethodInfo> viewOptionMethods = allMethods.Where(o => o.GetCustomAttribute(viewOptionType) != null); foreach (MethodInfo method in viewOptionMethods) { DynamicViewOptionAttribute attr = method.GetCustomAttribute(viewOptionType) as DynamicViewOptionAttribute; if (attr != null) { MethodInfo messageBuilder = allMethods.FirstOrDefault(o => o.Name == attr.MessageMethod); MethodInfo colorBuilder = null; if (!string.IsNullOrEmpty(attr.ColorMethod)) { colorBuilder = allMethods.FirstOrDefault(o => o.Name == attr.ColorMethod); } if (messageBuilder != null) { ParameterInfo[] methodParameters = method.GetParameters(); List <object> implementedMethodParameters = new List <object>(); foreach (ParameterInfo methodParameter in methodParameters) { if (methodParameter.HasDefaultValue) { implementedMethodParameters.Add(methodParameter.DefaultValue); } else if (Nullable.GetUnderlyingType(methodParameter.ParameterType) != null) { implementedMethodParameters.Add(null); } } bool useParameters = methodParameters.Length > 0 && implementedMethodParameters.Count == methodParameters.Length; Options.Add(new DynamicOption <T>( () => { return(thisType.InvokeMember( attr.MessageMethod, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod, null, this, null ).ToString()); }, () => thisType.InvokeMember( method.Name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod, null, this, useParameters ? implementedMethodParameters.ToArray() : null ), () => { return(colorBuilder != null ? (ConsoleColor)thisType.InvokeMember( attr.ColorMethod, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod, null, this, null ) : RenderOptions.DefaultColor); } )); } } } }