/// <summary> /// The method will register all matchers (classes implementing IColorMatcher<> interface) /// in the specified assembly using Reflection. /// </summary> public void RegisterMatchers(Assembly assembly) { // get all types implementing IColorMatcher interface IEnumerable <Type> colorMatcherTypes = from type in assembly.GetTypes() where !type.IsGenericType && type.IsAssignableToGenericType(typeof(IColorMatcher <>)) select type; foreach (Type colorMatcherType in colorMatcherTypes) { // for each color matcher, get interface types IEnumerable <Type> colorMatcherInterfaces = from i in colorMatcherType.GetInterfaces() where i.IsConstructedGenericType && i.GetGenericTypeDefinition() == typeof(IColorMatcher <>) select i; foreach (Type colorMatcherInterface in colorMatcherInterfaces) { // for each interface, extract the target type Type matchingType = colorMatcherInterface.GetGenericArguments().First(); if (mMatchers.ContainsKey(matchingType)) { continue; } // if the target type is not already registered, create an instance of the matcher and register it. IColorMatcher matcher = (IColorMatcher)Activator.CreateInstance(colorMatcherType); mMatchers.Add(matchingType, matcher); } } }
internal static Hashtable[] GetMatchColors <T>() { IColorMatcher <T> matcher = (IColorMatcher <T>)MatcherManager.GetMatcher(typeof(T)); return((from match in matcher.GetMatches() select new Hashtable { [HashtableColor] = match.ForegroundColor, [HashtableMatch] = match.GetMatchData() }).ToArray()); }
internal static void SetMatcherColors <T>(Hashtable[] matcherColors) { IColorMatcher <T> matcher = (IColorMatcher <T>)MatcherManager.GetMatcher(typeof(T)); var matches = from matcherColor in matcherColors let foregroundColor = (ConsoleColor)matcherColor[HashtableColor] let match = matcherColor[HashtableMatch] select matcher.CreateMatch(foregroundColor, match); matcher.SetMatches(matches); }
/// <summary> /// Tries to retrieve a matcher based on the target type. /// </summary> public bool TryGetMatcher(Type type, out IColorMatcher matcher) { return(mMatchers.TryGetValue(type, out matcher)); }