/// <summary> /// 删除指定名称的属性 /// </summary> /// <param name="strName"></param> public void RemoveAttribute(string strName) { ZYAttribute myAttr = this[strName]; if (myAttr != null) { this.List.Remove(myAttr); } }
public ZYAttribute Clone() { ZYAttribute NewAttribute = new ZYAttribute(); NewAttribute.Name = strName; NewAttribute.objValue = objValue; NewAttribute.DefaultValue = this.DefaultValue; return(NewAttribute); }
/// <summary> /// 获得指定名称的字符串类型的属性值 /// </summary> /// <param name="strName">属性名称</param> /// <returns>属性值</returns> public string GetString(string strName) { foreach (ZYAttribute myAttr in this) { if (myAttr.Name == strName) { return(Convert.ToString(myAttr.Value)); } } return(Convert.ToString(ZYAttribute.GetDefaultValue(strName))); }
/// <summary> /// 获得指定名称的布尔类型的属性值 /// </summary> /// <param name="strName">属性名称</param> /// <returns>属性值</returns> public bool GetBool(string strName) { foreach (ZYAttribute myAttr in this) { if (myAttr.Name == strName) { return((bool)myAttr.Value); } } return((bool)ZYAttribute.GetDefaultValue(strName)); }
/// <summary> /// 获得指定名称的单精度浮点数类型的属性值 /// </summary> /// <param name="strName">属性名称</param> /// <returns>属性值</returns> public float GetFloat(string strName) { try { foreach (ZYAttribute myAttr in this) { if (myAttr.Name == strName) { return(Convert.ToSingle(myAttr.Value)); } } return(Convert.ToSingle(ZYAttribute.GetDefaultValue(strName))); } catch { return(0); } }
/// <summary> /// 获得指定名称的整数类型的属性值 /// </summary> /// <param name="strName">属性名称</param> /// <returns>属性值</returns> public int GetInt32(string strName) { try { foreach (ZYAttribute myAttr in this) { if (myAttr.Name == strName) { return(Convert.ToInt32(myAttr.Value)); } } return((int)ZYAttribute.GetDefaultValue(strName)); } catch { return(0); } }
/// <summary> /// 设置指定名称的属性值 /// </summary> /// <param name="strName">属性名称</param> /// <param name="vValue">属性值</param> public void SetValue(string strName, object vValue) { ZYAttribute myValue = null; foreach (ZYAttribute myAttr in this) { if (myAttr.Name == strName) { myValue = myAttr; break; } } if (myValue == null) { myValue = new ZYAttribute(); myValue.Name = strName; this.List.Add(myValue); } if (myValue.Value == null || myValue.Value.Equals(vValue) == false) { if (myValue.Value != null || vValue != null) { if (OwnerElement != null && OwnerElement.OwnerDocument != null && OwnerElement.OwnerDocument.ContentChangeLog != null) //if (OwnerElement != null && OwnerElement.OwnerDocument != null) { OwnerElement.OwnerDocument.ContentChangeLog.LogAttribute( OwnerElement, myValue, vValue); OwnerElement.OwnerDocument.Modified = true; } myValue.Value = vValue; //if( myValue.Value == myValue.DefaultValue )myList.Remove( myValue ); bolModified = true; } } }
/// <summary> /// load attribute set from a xmlelement's attribute set /// </summary> /// <param name="myElement">xmlelement object</param> /// <returns>handle is ok</returns> public bool FromXML(System.Xml.XmlElement myElement) { // clear owner attribute set this.Clear(); if (myElement == null) { return(false); } // enumerate xmlelement's attribute set // for each xmlattribute create an attribute and add to owner attribute set foreach (System.Xml.XmlAttribute myXMLAttr in myElement.Attributes) { ZYAttribute myAttr = new ZYAttribute(); myAttr.Name = myXMLAttr.Name; myAttr.ValueString = myXMLAttr.Value; if (myAttr.Value != myAttr.DefaultValue) { this.List.Add(myAttr); } } bolModified = false; return(true); }
/// <summary> /// 设置所有属性为默认值 /// </summary> /// <returns>是否修改了数据</returns> public bool SetDefaultValue() { bool bolChange = false; foreach (ZYAttribute myAttr in this) { object DefaultValue = ZYAttribute.GetDefaultValue(myAttr.Name); if (DefaultValue != null && DefaultValue.Equals(myAttr.Value)) { if (OwnerElement != null && OwnerElement.OwnerDocument != null && OwnerElement.OwnerDocument.ContentChangeLog != null) { OwnerElement.OwnerDocument.ContentChangeLog.LogAttribute( OwnerElement, myAttr, DefaultValue); } myAttr.Value = DefaultValue; bolModified = true; bolChange = true; } } return(bolChange); }
/// <summary> /// 比较两个属性对象是否一致 /// </summary> /// <param name="myAttr"></param> /// <returns></returns> public bool EqualsValue(ZYAttribute myAttr) { if (myAttr == null) { return(false); } if (myAttr == this) { return(true); } if (myAttr.Name != strName) { return(false); } if (this.Value != null) { return(this.Value.Equals(myAttr.Value)); } else { return(myAttr.Value == null); } }