private ConfigurableObject <T> ToConfigurableObject(T @object, string objectName = null) { var cfgObject = new ConfigurableObject <T>(); cfgObject.Name = objectName; cfgObject.Target = @object; cfgObject.TypeMetadata = new TypeMetadata(typeof(T)); foreach (PropertyMetadata propertyMetadata in cfgObject.TypeMetadata.Properties) { ConfigurableAttribute cfgAttribute = propertyMetadata.Attributes.OfType <ConfigurableAttribute>().SingleOrDefault(); if (cfgAttribute != null) { var cfgProperty = new ConfigurableProperty(); cfgProperty.IsRequired = cfgAttribute.IsRequired; cfgProperty.PropertyMetadata = propertyMetadata; if (String.IsNullOrEmpty(cfgAttribute.SettingName)) { cfgProperty.PrioritizedSettingNames = GetPrioritizedConventionalSettingNames(cfgObject, cfgProperty).ToArray(); } else { cfgProperty.PrioritizedSettingNames = new[] { cfgAttribute.SettingName.Merge(new { Name = objectName }) }; } cfgObject.Properties.Add(cfgProperty); } } return(cfgObject); }
private bool IsConfig(Type type) { if (interfaceDrawer.SelectedType == null || implementationDrawer.SelectedType == null) { return(false); } if (implementationDrawer.SelectedType != null) { ConfigurableAttribute configurableAttribute = implementationDrawer.SelectedType.GetCustomAttribute <ConfigurableAttribute>(); if (configurableAttribute != null) { return(configurableAttribute.configType == type); } } if (typeof(ModuleConfig).IsAssignableFrom(type)) { return(true); } return(false); }
private static ConfigElement GenerateElement(PropertyInfo propertyInfo, ConfigurableAttribute configurable, Thickness margin) { var element = new ConfigElement { Label = configurable.Label == null ? null : new TextBlock { Text = configurable.Label, VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Right, Margin = margin }, Content = configurable.ControlType != null ? (FrameworkElement)Activator.CreateInstance(configurable.ControlType) : GenerateUserControl(propertyInfo.PropertyType, margin), Span = configurable.Span }; element.Content.HorizontalAlignment = HorizontalAlignment.Left; element.Content.Margin = margin; // バインディングの作成 { var binding = new Binding(propertyInfo.Name) { UpdateSourceTrigger = configurable.UpdateSourceTrigger, Mode = configurable.Mode }; if (configurable.ValidationRuleType != null) { binding.ValidationRules.Add( (ValidationRule)Activator.CreateInstance(configurable.ValidationRuleType)); } if (configurable.StringFormat != null) { binding.StringFormat = configurable.StringFormat; } if (configurable.ConverterType != null) { binding.Converter = (IValueConverter)Activator.CreateInstance(configurable.ConverterType); } // バインド var dependancyProperty = configurable.ControlType == null ? FrameworkElement.DataContextProperty : (DependencyProperty) (configurable.ControlType.GetField(configurable.PropertyName + "Property", BindingFlags.FlattenHierarchy | BindingFlags.Static | BindingFlags.Public).GetValue(null)); element.Content.SetBinding(dependancyProperty, binding); } Type controlType = configurable.ControlType ?? typeof(UserControl); // Binder属性の処理 foreach (BinderAttribute binder in Attribute.GetCustomAttributes(propertyInfo, typeof(BinderAttribute))) { var binding = new Binding(binder.SrcPropertyName) { UpdateSourceTrigger = binder.UpdateSourceTrigger, Mode = binder.Mode }; if (binder.ValidationRuleType != null) { binding.ValidationRules.Add((ValidationRule)Activator.CreateInstance(binder.ValidationRuleType)); } if (binder.StringFormat != null) { binding.StringFormat = binder.StringFormat; } if (binder.ConverterType != null) { binding.Converter = (IValueConverter)Activator.CreateInstance(binder.ConverterType); } // バインド var dependancyProperty = (DependencyProperty) (controlType.GetField(binder.DstPropertyName + "Property", BindingFlags.FlattenHierarchy | BindingFlags.Static | BindingFlags.Public).GetValue(null)); element.Content.SetBinding(dependancyProperty, binding); } // Setter属性の処理 foreach (SetterAttribute setter in Attribute.GetCustomAttributes(propertyInfo, typeof(SetterAttribute))) { var dependancyProperty = (DependencyProperty) (controlType.GetField(setter.PropertyName + "Property", BindingFlags.FlattenHierarchy | BindingFlags.Static | BindingFlags.Public).GetValue(null)); element.Content.SetValue(dependancyProperty, setter.Value); } return(element); }