public static void Apply(NES.INESBoard board) { var fields = board.GetType().GetFields(); foreach (var field in fields) { var attribs = field.GetCustomAttributes(false); foreach (var attrib in attribs) { if (attrib is MapperPropAttribute) { string Name = ((MapperPropAttribute)attrib).Name ?? field.Name; string Value; if (board.InitialRegisterValues.TryGetValue(Name, out Value)) { try { field.SetValue(board, Convert.ChangeType(Value, field.FieldType)); } catch (Exception e) when(e is InvalidCastException || e is FormatException || e is OverflowException) { throw new InvalidDataException("Auto Mapper Properties were in a bad format!", e); } } break; } } } }
public static void Populate(NES.INESBoard board, NES.NESSyncSettings settings) { var fields = board.GetType().GetFields(); foreach (var field in fields) { var attrib = field.GetCustomAttributes(typeof(MapperPropAttribute), false).OfType <MapperPropAttribute>().SingleOrDefault(); if (attrib == null) { continue; } string Name = attrib.Name ?? field.Name; if (!settings.BoardProperties.ContainsKey(Name)) { settings.BoardProperties.Add(Name, (string)Convert.ChangeType(field.GetValue(board), typeof(string))); } } }