public PropertyViewModel(SectionViewModel parent, PropertyInfo property, ConfigurationPropertyAttribute attribute) { if (parent == null) throw new ArgumentNullException("parent"); if (property == null) throw new ArgumentNullException("property"); if (attribute == null) throw new ArgumentNullException("attribute"); Parent = parent; Property = property; Attribute = attribute; DefaultValue = Attribute.DefaultValue; Value = DefaultValue; }
public PropertyViewModel(SectionViewModel parent, PropertyInfo property, ConfigurationPropertyAttribute attribute) { if (parent == null) { throw new ArgumentNullException("parent"); } if (property == null) { throw new ArgumentNullException("property"); } if (attribute == null) { throw new ArgumentNullException("attribute"); } Parent = parent; Property = property; Attribute = attribute; DefaultValue = Attribute.DefaultValue; Value = DefaultValue; }
private async Task LoadAssembly(string assemblyFilename) { if (string.IsNullOrWhiteSpace(assemblyFilename)) { throw new ArgumentException("Invalid 'assemblyFilename' argument"); } Assembly assembly = null; try { assembly = await Task.Factory.StartNew(() => { try { return(Assembly.LoadFrom(assemblyFilename)); } catch { return(null); } }); } catch { return; } if (assembly == null) { return; } IEnumerable <Type> configurationSections = null; try { configurationSections = assembly.GetTypes().Where(t => t.IsSubclassOf(typeof(ConfigurationSection))); } catch { return; } foreach (var configSection in configurationSections) { var hasValidProperties = false; var sectionViewModel = new SectionViewModel(assembly, configSection); var properties = await Task.Factory.StartNew(arg => ((Type)arg).GetProperties(BindingFlags.Public | BindingFlags.Instance), configSection); foreach (var property in properties.OrderBy(p => p.Name)) { var attribute = property.GetCustomAttributes(typeof(ConfigurationPropertyAttribute), true) .Cast <ConfigurationPropertyAttribute>() .FirstOrDefault(); if (attribute != null) { var propertyViewModel = new PropertyViewModel(sectionViewModel, property, attribute); sectionViewModel.AddProperty(propertyViewModel); hasValidProperties = true; } } if (hasValidProperties) { workingSections.Add(sectionViewModel); } } }
private void TrackPropertyChanged(SectionViewModel sectionViewModel) { sectionViewModel.PropertyChanged += OnViewModelPropertyChanged; foreach (var child in sectionViewModel.Properties) child.PropertyChanged += OnViewModelPropertyChanged; }
private async Task LoadAssembly(string assemblyFilename) { if (string.IsNullOrWhiteSpace(assemblyFilename)) throw new ArgumentException("Invalid 'assemblyFilename' argument"); Assembly assembly = null; try { assembly = await Task.Factory.StartNew(() => { try { return Assembly.LoadFrom(assemblyFilename); } catch { return null; } }); } catch { return; } if (assembly == null) return; IEnumerable<Type> configurationSections = null; try { configurationSections = assembly.GetTypes().Where(t => t.IsSubclassOf(typeof(ConfigurationSection))); } catch { return; } foreach (var configSection in configurationSections) { var hasValidProperties = false; var sectionViewModel = new SectionViewModel(assembly, configSection); var properties = await Task.Factory.StartNew(arg => ((Type)arg).GetProperties(BindingFlags.Public | BindingFlags.Instance), configSection); foreach (var property in properties.OrderBy(p => p.Name)) { var attribute = property.GetCustomAttributes(typeof(ConfigurationPropertyAttribute), true) .Cast<ConfigurationPropertyAttribute>() .FirstOrDefault(); if (attribute != null) { var propertyViewModel = new PropertyViewModel(sectionViewModel, property, attribute); sectionViewModel.AddProperty(propertyViewModel); hasValidProperties = true; } } if (hasValidProperties) workingSections.Add(sectionViewModel); } }