public object Build(Type interfaceType, KeyedDescription description) { DialogTrace.WriteLine("Building {0} for {1}", interfaceType.Name, description.Key ?? "-empty-"); if (!ShouldBuildDescription(description)) { DialogTrace.WriteLine("Skipping - not for this platform"); return null; } TypedUserInterfaceBuilder typeBuilder; if (!_builderRegistry.TryGetValue(interfaceType, out typeBuilder)) { DialogTrace.WriteLine("No builder found for that {0}", interfaceType.Name); return null; } var userInterfaceInstance = typeBuilder.Build(description); if (userInterfaceInstance == null) { DialogTrace.WriteLine("Builder returned NULL for {0}", interfaceType.Name); return null; } FillProperties(userInterfaceInstance, description.Properties); FillBuildableProperties(description, userInterfaceInstance); return userInterfaceInstance; }
private void FillBuildableProperties(KeyedDescription description, object userInterfaceInstance) { var reservedPropertyNames = new[] { "Key", "Properties", "NotFor", "OnlyFor" }; var buildableProperties = from p in description.GetType().GetProperties() where !reservedPropertyNames.Contains(p.Name) select p; foreach (var buildablePropertyInfo in buildableProperties) { FillBuildableProperty(description, userInterfaceInstance, buildablePropertyInfo); } }
private void FillBuildableProperty(KeyedDescription description, object userInterfaceInstance, PropertyInfo buildablePropertyInfo) { var buildablePropertyValue = buildablePropertyInfo.GetValue(description, null); var userInterfacePropertyInfo = userInterfaceInstance.GetType().GetProperty(buildablePropertyInfo.Name); if (userInterfacePropertyInfo == null) { var props = userInterfaceInstance.GetType().GetProperties().Select(p => p.Name); var available = string.Join("'", props); DialogTrace.WriteLine("No User Interface member for description property {0} on {1}", buildablePropertyInfo.Name, available); return; } if (buildablePropertyInfo.PropertyType.GetTypeInfo().IsGenericType) { var genericPropertyType = buildablePropertyInfo.PropertyType.GetGenericTypeDefinition(); if (genericPropertyType == typeof(Dictionary<int, int>).GetGenericTypeDefinition()) { DialogTrace.WriteLine("Filling Dictionary {0}", buildablePropertyInfo.Name); FillDictionary(buildablePropertyInfo, buildablePropertyValue, userInterfacePropertyInfo, userInterfaceInstance); } else if (genericPropertyType == typeof(List<int>).GetGenericTypeDefinition()) { DialogTrace.WriteLine("Filling List {0}", buildablePropertyInfo.Name); FillList(buildablePropertyInfo, buildablePropertyValue, userInterfacePropertyInfo, userInterfaceInstance); } else { throw new Exception("Unexpected Generic Property Type " + buildablePropertyInfo.PropertyType); } } else { DialogTrace.WriteLine("Filling Element {0}", buildablePropertyInfo.Name); FillUserInterfaceElement(buildablePropertyInfo, buildablePropertyValue, userInterfacePropertyInfo, userInterfaceInstance); } }
public object Build(KeyedDescription description) { var key = string.IsNullOrEmpty(description.Key) ? DefaultKey : description.Key; Type type; if (!KnownKeys.TryGetValue(key, out type)) { throw new KeyNotFoundException("Could not find class for " + description.Key); } var constructor = type.GetConstructors() .FirstOrDefault(c => c.GetParameters().All(p => p.IsOptional)); if (constructor == null) { throw new ArgumentException("No parameterless Constructor found for " + key); } //var parameters = constructor.GetParameters().Select(p => (object)Type.Missing).ToArray(); var parameters = constructor.GetParameters().Select(p => p.DefaultValue).ToArray(); var instance = constructor.Invoke(parameters); return instance; }
public void Fill(KeyedDescription keyedDescription) { base.Fill(keyedDescription); keyedDescription.Key = this.Key; }