/// <summary> /// 从当前的条件类中通过反射得到所有的特性,放到StringCollection集合中 /// </summary> private void SetRequiredAttributes() { Type currentType = assembly.GetType(className); while (currentType != typeof(object)) { FieldInfo[] fieldInfoArray = currentType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance); foreach (FieldInfo fieldInfo in fieldInfoArray) { // process TaskAttribute attributes XmlMemberAttributeAttribute codonAttribute = (XmlMemberAttributeAttribute)Attribute.GetCustomAttribute(fieldInfo, typeof(XmlMemberAttributeAttribute)); if (codonAttribute != null && codonAttribute.IsRequired) { requiredAttributes.Add(codonAttribute.Name); } } currentType = currentType.BaseType; } }
/// <summary> /// Autoinitialized all fields of the customizer object to the values /// in the codonNode using the XmlMemberAttributeAttribute. /// </summary> void AutoInitializeAttributes(object obj, XmlNode codonNode) { Type currentType = obj.GetType(); while (currentType != typeof(object)) { FieldInfo[] fieldInfoArray = currentType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance); foreach (FieldInfo fieldInfo in fieldInfoArray) { // process XmlMemberAttributeAttribute attributes XmlMemberAttributeAttribute codonAttribute = (XmlMemberAttributeAttribute)Attribute.GetCustomAttribute(fieldInfo, typeof(XmlMemberAttributeAttribute)); if (codonAttribute != null) { // get value from xml file XmlNode node = codonNode.SelectSingleNode("@" + codonAttribute.Name); // check if its required if (node == null && codonAttribute.IsRequired) { throw new AddInLoadException(String.Format("{0} is a required attribute for node '{1}' ", codonAttribute.Name, codonNode.Name)); } if (node != null) { if (fieldInfo.FieldType.IsSubclassOf(typeof(System.Enum))) { fieldInfo.SetValue(obj, Convert.ChangeType(Enum.Parse(fieldInfo.FieldType, node.Value), fieldInfo.FieldType)); } else { PathAttribute pathAttribute = (PathAttribute)Attribute.GetCustomAttribute(fieldInfo, typeof(PathAttribute)); if (pathAttribute != null) { fieldInfo.SetValue(obj, fileUtilityService.GetDirectoryNameWithSeparator(Path.GetDirectoryName(fileName)) + Convert.ChangeType(node.Value, fieldInfo.FieldType).ToString()); } else { fieldInfo.SetValue(obj, Convert.ChangeType(node.Value, fieldInfo.FieldType)); } } } } // process XmlMemberAttributeAttribute attributes XmlMemberArrayAttribute codonArrayAttribute = (XmlMemberArrayAttribute)Attribute.GetCustomAttribute(fieldInfo, typeof(XmlMemberArrayAttribute)); if (codonArrayAttribute != null) { // get value from xml file XmlNode node = codonNode.SelectSingleNode("@" + codonArrayAttribute.Name); // check if its required if (node == null && codonArrayAttribute.IsRequired) { throw new ApplicationException(String.Format("{0} is a required attribute.", codonArrayAttribute.Name)); } if (node != null) { string[] attrArray = node.Value.Split(codonArrayAttribute.Separator); // TODO : convert array types (currently only string arrays are supported) fieldInfo.SetValue(obj, attrArray); } } } currentType = currentType.BaseType; } }