コード例 #1
0
ファイル: XmlFile.cs プロジェクト: XiBeichuan/hydronumerics
		/// <summary>
		/// Reads the attributes of the current xml element and uses them to populute the target with
		/// </summary>
		/// <param name="target">The object which is populated with the xml attributes</param>
		/// <param name="reader">The xml stream</param>
		/// <param name="root">Top object of the xml stream</param>
		/// <exception cref="System.Exception">Cannot find class type</exception>
		/// <exception cref="System.Exception">Cannot instantiate object for known class type</exception>
		private static void ReadAttributes (IAggregate target, XmlReader reader, object root) 
		{
			// Read all attributes
			string[] properties = target.Properties;
			for (int i = 0; i < properties.Length; i++) 
			{
				string name = XmlFile.GetElementName (target.Source.GetType(), properties[i], false);
				if (name != null) 
				{
					string attributeValue = reader.GetAttribute(name);
					if (attributeValue != null) 
					{
						string classType = (string) MetaInfo.GetAttributeDefault (target.Source.GetType(), properties[i], "XmlType", target.GetType(properties[i]).FullName);
						Type type = ObjectSupport.GetType(classType);
						if (type == null) 
						{
							throw new Exception("Could not find class type " + classType);
						}

						if (type.Equals(typeof(FileInfo))) 
						{
							attributeValue = FileSupport.ExpandRelativePath (XmlFile.GetRegisteredFile(root).Directory, attributeValue).FullName;
						}

						if (type.Equals(typeof(DirectoryInfo))) 
						{
							attributeValue = FileSupport.ExpandRelativeDirectory (XmlFile.GetRegisteredFile(root).Directory, attributeValue).FullName;
						}

						object targetValue = ObjectSupport.GetInstance (type, attributeValue, _culture);
						if (targetValue == null) 
						{
							throw new Exception("Could not instantiate class type " + type.FullName);
						}

						if (target.CanWrite(properties[i])) 
						{
							target.SetValue(properties[i], targetValue);
						}
					}
				}
			}
		}