public static string GetName(Type type) { CommandNameAttribute attribute = type.GetTypeInfo().GetCustomAttributes(typeof(CommandNameAttribute), true).OfType <CommandNameAttribute>().FirstOrDefault(); if (attribute == null) { throw new Exception(string.Format("Missing CommandNameAttribute on type: {0}", type.Name)); } return(attribute.Name); }
private static void GenerateDictionaries() { try { var resultTypes = new Dictionary <string, List <Tuple <ProtocolVersion, Type> > >(); var resultNames = new Dictionary <Type, Tuple <string, ProtocolVersion> >(); var assembly = typeof(CommandNameAttribute).GetTypeInfo().Assembly; foreach (Type type in assembly.GetTypes()) { CommandNameAttribute attribute = type.GetTypeInfo().GetCustomAttributes(typeof(CommandNameAttribute), true).OfType <CommandNameAttribute>().FirstOrDefault(); if (attribute == null) { continue; } if (!typeof(ICommand).GetTypeInfo().IsAssignableFrom(type.GetTypeInfo())) { continue; } if (!resultTypes.TryGetValue(attribute.Name, out var resultTypesForName)) { resultTypesForName = new List <Tuple <ProtocolVersion, Type> >(); } if (resultTypesForName.Count(t => t.Item1 == attribute.MinimumVersion) > 0) { Log.FatalFormat("Duplicate definition for {0} for version {1}", attribute.Name, attribute.MinimumVersion); } resultTypesForName.Add(Tuple.Create(attribute.MinimumVersion, type)); resultTypes[attribute.Name] = resultTypesForName; resultNames[type] = Tuple.Create(attribute.Name, attribute.MinimumVersion); } commandTypes = resultTypes; commandNames = resultNames; } catch (Exception e) { Log.FatalFormat("Failed to find all valid command types: {0}", e.Message); throw; } }
private static void GenerateDictionaries() { try { var resultTypes = new Dictionary <string, Type>(); var resultNames = new Dictionary <Type, string>(); var assembly = typeof(CommandNameAttribute).GetTypeInfo().Assembly; foreach (Type type in assembly.GetTypes()) { CommandNameAttribute attribute = type.GetTypeInfo().GetCustomAttributes(typeof(CommandNameAttribute), true).OfType <CommandNameAttribute>().FirstOrDefault(); if (attribute == null) { continue; } if (!typeof(ICommand).GetTypeInfo().IsAssignableFrom(type.GetTypeInfo())) { continue; } if (resultTypes.ContainsKey(attribute.Name)) { Log.FatalFormat("Duplicate definition for {0}", attribute.Name); } resultTypes[attribute.Name] = type; resultNames[type] = attribute.Name; } commandTypes = resultTypes; commandNames = resultNames; } catch (Exception e) { Log.FatalFormat("Failed to find all valid command types: {0}", e.Message); throw; } }