GetSettingsInterfaces( System.Type settingsType, System.Collections.Generic.IEnumerable <System.Type> currentInterfaces) { lock (Cache) { if (!Cache.ContainsKey(settingsType)) { var attributeType = typeof(SettingsExtensionsAttribute); var interfaces = new SettingsInterfaces(); foreach (var i in currentInterfaces) { var attributeArray = i.GetCustomAttributes(attributeType, false); if (0 == attributeArray.Length) { throw new Exception("Settings interface {0} is missing attribute {1}", i.ToString(), attributeType.ToString()); } var newData = new InterfaceData(); newData.InterfaceType = i; var attribute = attributeArray[0] as SettingsExtensionsAttribute; // TODO: the Empty function could be replaced by the auto-property initializers in C#6.0 (when Mono catches up) // although it won't then be a centralized definition, so the extension method as-is is probably better newData.emptyMethod = attribute.GetMethod("Empty", new[] { i }); newData.defaultMethod = attribute.GetMethod("Defaults", new[] { i, typeof(Module) }); interfaces.Data.Add(newData); } Cache.Add(settingsType, interfaces); } return(Cache[settingsType]); } }
InitializeAllInterfaces( Module module, bool emptyFirst, bool useDefaults) { this.Module = module; var settingsType = this.GetType(); lock (Cache) { if (!Cache.ContainsKey(settingsType)) { var attributeType = typeof(SettingsExtensionsAttribute); var interfaces = new SettingsInterfaces(); foreach (var i in this.Interfaces()) { var attributeArray = i.GetCustomAttributes(attributeType, false); if (0 == attributeArray.Length) { throw new Exception("Settings interface {0} is missing attribute {1}", i.ToString(), attributeType.ToString()); } var newData = new InterfaceData(); newData.InterfaceType = i; var attribute = attributeArray[0] as SettingsExtensionsAttribute; // TODO: the Empty function could be replaced by the auto-property initializers in C#6.0 (when Mono catches up) // although it won't then be a centralized definition, so the extension method as-is is probably better newData.emptyMethod = attribute.GetMethod("Empty", new[] { i }); newData.defaultMethod = attribute.GetMethod("Defaults", new[] { i, typeof(Module) }); interfaces.Data.Add(newData); } Cache.Add(settingsType, interfaces); } } var data = Cache[settingsType]; foreach (var i in data.Data) { if (emptyFirst) { if (null != i.emptyMethod) { i.emptyMethod.Invoke(null, new[] { this }); } } if (useDefaults) { if (null != i.defaultMethod) { i.defaultMethod.Invoke(null, new object[] { this, module }); } } } if (useDefaults && (null != LocalPolicy)) { LocalPolicy.DefineLocalSettings(this, module); } }