private static void PopulateOneAssemblyMapping(Assembly assembly) { // A bit slow, but only executes this once during the life of the program: IEnumerable <Type> allStructureTypes = assembly.GetTypes().Where(t => t.IsSubclassOf(typeof(Structure)) || t == typeof(Structure)); // The nomenclature mapping data is contained in KOSNomenclatureAttributes that are attached to the classes: foreach (Type t in allStructureTypes) { object[] attribs = t.GetCustomAttributes(typeof(KOSNomenclatureAttribute), false); foreach (object obj in attribs) { KOSNomenclatureAttribute attrib = obj as KOSNomenclatureAttribute; if (attrib == null) { continue; // hypothetically impossible since GetCustomAttributes explicitly asked for only attributes of this type. } try { if (attrib.CSharpToKOS) { cSharpToKosMap.Add(t, attrib.KOSName); } } catch (ArgumentException) { // There can be a many-to-one map (given two different C# types, they both return the same KOS type), but // not a one-to-many map (given one C# type, it has two kOS types it tries to return). string msg = "kOS developer error: name clash in KOSNomenclature: two mappings from C# class " + t.FullName + " found."; Debug.AddNagMessage(Debug.NagType.NAGFOREVER, msg); } try { if (attrib.KOSToCSharp) { kosToCSharpMap.Add(attrib.KOSName, t); } } catch (ArgumentException) { // There can be a many-to-one map (given two different kos types, they both return the same C# type), but // not a one-to-many map (given one kos type, it has two C# types it tries to return). string msg = "kOS developer error: name clash in KOSNomenclature: two mappings from KOS name " + attrib.KOSName + " found."; Debug.AddNagMessage(Debug.NagType.NAGFOREVER, msg); } } } NagCheck(allStructureTypes); }
/// <summary> /// Populate the nomenclature dictionaries based on the given type, which inherits /// from Structure. This method is automatically called by the AssemblyWalkAttribute /// </summary> /// <param name="t">A type inheriting from Structure</param> public static void PopulateType(Type t) { object[] attribs = t.GetCustomAttributes(typeof(KOSNomenclatureAttribute), false); foreach (object obj in attribs) { KOSNomenclatureAttribute attrib = obj as KOSNomenclatureAttribute; if (attrib == null) { continue; // hypothetically impossible since GetCustomAttributes explicitly asked for only attributes of this type. } try { if (attrib.CSharpToKOS) { cSharpToKosMap.Add(t, attrib.KOSName); } } catch (ArgumentException) { // There can be a many-to-one map (given two different C# types, they both return the same KOS type), but // not a one-to-many map (given one C# type, it has two kOS types it tries to return). string msg = "kOS developer error: name clash in KOSNomenclature: two mappings from C# class " + t.FullName + " found."; Debug.AddNagMessage(Debug.NagType.NAGFOREVER, msg); } try { if (attrib.KOSToCSharp) { kosToCSharpMap.Add(attrib.KOSName, t); } } catch (ArgumentException) { // There can be a many-to-one map (given two different kos types, they both return the same C# type), but // not a one-to-many map (given one kos type, it has two C# types it tries to return). string msg = "kOS developer error: name clash in KOSNomenclature: two mappings from KOS name " + attrib.KOSName + " found."; Debug.AddNagMessage(Debug.NagType.NAGFOREVER, msg); } } NagCheck(t); }