/// <summary> /// Initializes a new instance of the <see cref="ObjectConfigurationType"/> class. /// </summary> /// <param name="node">The node.</param> public ObjectConfigurationType(XmlElement node) { properties = new List<PropertyConfigurationType>(); constructorArgs = new List<PropertyConfigurationType>(); name = node.GetAttribute(NameAttr); type = node.GetAttribute(TypeAttr); XmlNodeList nodeProperties = node.SelectNodes(PropertyElement); foreach (XmlNode nodeProperty in nodeProperties) { properties.Add(new PropertyConfigurationType((XmlElement)nodeProperty)); } var nodeConstructorArgs = node.SelectNodes(ConstructorArgElement); const int FIRST_INDEX = 0; var index = FIRST_INDEX; var argMap = new SortedDictionary<int, PropertyConfigurationType>(); foreach (XmlNode nodeConstructorArg in nodeConstructorArgs) { var el = (XmlElement)nodeConstructorArg; if (!string.IsNullOrEmpty(el.GetAttribute(IndexAttr))) { index = Convert.ToInt32(el.GetAttribute(IndexAttr)); } argMap[index++] = new PropertyConfigurationType(el); } if (nodeConstructorArgs.Count > 0 && !argMap.ContainsKey(FIRST_INDEX)) { throw new ConfigurationErrorsException("constructor-arg index must start at: " + FIRST_INDEX); } constructorArgs.AddRange(argMap.Values); }
/// <summary> /// Append the property definition /// </summary> /// <param name="propertyDefinitions"></param> private void AppendPropertyDefinition(IList <PropertyConfigurationType> propertyDefinitions) { for (int i = 0; i < propertyDefinitions.Count; i++) { PropertyConfigurationType propertyDefinition = propertyDefinitions[i]; PropertyInfo pi = objectType.GetProperty(propertyDefinition.Name); if (pi == null) { throw new TypeLoadException( string.Format(ErrorNoPropertyExists, propertyDefinition.Name, objectType.Name)); } if (!string.IsNullOrEmpty(propertyDefinition.Ref)) { var refType = GetRefType(propertyDefinition.Ref, pi.PropertyType); if (refType == null) { throw new TypeLoadException( string.Format(ErrorSettingRefProperty, propertyDefinition.Name, objectType.Name)); } propertyRefTypes.Add(refType); properties.Add(pi); propertyValueTypes.Add(refType.GetType()); } else if (TypeSerializer.CanCreateFromString(pi.PropertyType)) { properties.Add(pi); propertyValueTypes.Add(pi.PropertyType); } else { throw new TypeLoadException( string.Format(ErrorPropertyTypeNotSupported, propertyDefinition.Name, objectType.Name)); } } }
/// <summary> /// Initializes a new instance of the <see cref="ObjectConfigurationType"/> class. /// </summary> /// <param name="node">The node.</param> public ObjectConfigurationType(XmlElement node) { properties = new List <PropertyConfigurationType>(); constructorArgs = new List <PropertyConfigurationType>(); name = node.GetAttribute(NameAttr); type = node.GetAttribute(TypeAttr); XmlNodeList nodeProperties = node.SelectNodes(PropertyElement); foreach (XmlNode nodeProperty in nodeProperties) { properties.Add(new PropertyConfigurationType((XmlElement)nodeProperty)); } var nodeConstructorArgs = node.SelectNodes(ConstructorArgElement); const int FIRST_INDEX = 0; var index = FIRST_INDEX; var argMap = new SortedDictionary <int, PropertyConfigurationType>(); foreach (XmlNode nodeConstructorArg in nodeConstructorArgs) { var el = (XmlElement)nodeConstructorArg; if (!string.IsNullOrEmpty(el.GetAttribute(IndexAttr))) { index = Convert.ToInt32(el.GetAttribute(IndexAttr)); } argMap[index++] = new PropertyConfigurationType(el); } if (nodeConstructorArgs.Count > 0 && !argMap.ContainsKey(FIRST_INDEX)) { throw new ConfigurationErrorsException("constructor-arg index must start at: " + FIRST_INDEX); } constructorArgs.AddRange(argMap.Values); }
/// <summary> /// Find the right constructor for the matching types /// </summary> /// <param name="constructorArgDefinitions"></param> private void AppendConstructorDefinition(IList <PropertyConfigurationType> constructorArgDefinitions) { foreach (ConstructorInfo ci in objectType.GetConstructors()) { ParameterInfo[] constructorParams = ci.GetParameters(); //We can only call a constructor that has the right number of arguments. bool possibleMatch = constructorParams.Length == constructorArgDefinitions.Count; if (possibleMatch) { for (int i = 0; i < constructorParams.Length; i++) { ParameterInfo constructorParam = constructorParams[i]; PropertyConfigurationType constructorArgDefinition = constructorArgDefinitions[i]; if (!string.IsNullOrEmpty(constructorArgDefinition.Ref)) { RefType refType = GetRefType(constructorArgDefinition.Ref, constructorParam.ParameterType); constructorRefTypes.Add(refType); constructorValueTypes.Add(refType.GetType()); } else if (TypeSerializer.CanCreateFromString(constructorParam.ParameterType)) { constructorValueTypes.Add(constructorParam.ParameterType); } else { break; } } bool matchingConstructorFound = constructorValueTypes.Count == constructorParams.Length; if (matchingConstructorFound) { constructorInfo = ci; return; } } } //if it got this far no matching constructor *that we can use* has been found throw new TypeLoadException(string.Format(ErrorNoMatchingConstructor, objectType.Name)); }