/// <summary> /// 根据属性ID获取动态属性 /// </summary> /// <param name="propertyID">属性ID</param> /// <returns>动态属性</returns> public DynProperty GetProperty(short propertyID) { if (_quickFindProperty == null) { _quickFindProperty = new Dictionary <short, DynProperty>(); } DynProperty result = null; if (_quickFindProperty.TryGetValue(propertyID, out result)) { return(result); } if (_propertiesByID.TryGetValue(propertyID, out result)) { _quickFindProperty[propertyID] = result; return(result); } else { if (!string.IsNullOrEmpty(_baseClassName)) { result = BaseClass.GetProperty(propertyID); _quickFindProperty[propertyID] = result; return(result); } throw new ApplicationException(string.Format("当前类{0}或基类没有ID为{1}的属性!", _name, propertyID)); } }
/// <summary> /// 设置默认值 /// </summary> /// <param name="dynProperty">属性</param> public void SetDefaultValue(DynProperty dynProperty) { if (dynProperty == null) { throw new ApplicationException("设置属性默认值时 入参不能为空"); } _propertyValues[dynProperty.Name] = dynProperty.GetDefaultValue(); }
/// <summary> /// 刷新为空的默认值 /// </summary> public void FlushDefault() { foreach (var propertyValue in _propertyValues) { if (propertyValue.Value == null) { DynProperty p = this.DynClass.GetProperty(propertyValue.Key); SetDefaultValue(p); } } }
/// <summary> /// 强制赋值 但是值是否正确不做检查 后果自负 /// </summary> /// <param name="propertyName"></param> /// <param name="propertyValue"></param> public void SetPropertyValueWithOutCheck(DynProperty property, object propertyValue) { if (propertyValue != null) { _propertyValues[property.Name] = propertyValue; } else { SetDefaultValue(property); } }
/// <summary> /// 强制赋值 但是值是否正确不做检查 后果自负 /// </summary> /// <param name="propertyName"></param> /// <param name="propertyValue"></param> public void SetPropertyValueWithOutCheck(string propertyName, object propertyValue) { if (_dynClass.ContainsProperty(propertyName)) { DynProperty dynProperty = _dynClass.GetProperty(propertyName); SetPropertyValueWithOutCheck(dynProperty, propertyValue); } else { throw new ApplicationException(string.Format("不存在属性{0}", propertyName)); } }
/// <summary> /// 根据ID移除属性 /// </summary> /// <param name="propertyID">属性ID</param> public void RemoveProperty(short propertyID) { DynProperty dynProperty = null; if (_propertiesByID.TryGetValue(propertyID, out dynProperty)) { _propertiesByID.Remove(propertyID); _propertiesByName.Remove(dynProperty.Name); } else { throw new ApplicationException(string.Format("不存在属性ID为{0}的属性", propertyID)); } }
public void RemoveRelation(DynProperty dynProperty) { if (dynProperty == null || !this.ContainsProperty(dynProperty.Name)) { throw new ApplicationException("寻求关联的类不存在"); } if (dynProperty.CollectionType == CollectionType.None && dynProperty.DynType == DynType.Struct) { DynClass relationDynClass = DynTypeManager.GetClass(dynProperty.StructName); if (relationDynClass == null) { throw new ApplicationException(string.Format("类{0}的属性{1}所添加的关联类{2}不存在", this.Name, dynProperty.Name, dynProperty.StructName)); } relationDynClass.RelationDynClassDict.Remove(this.Name); relationDynClass.RelationDynPropertyDict.Remove(this.Name); } }
/// <summary> /// 添加动态属性 /// </summary> /// <param name="dynProperty">动态属性</param> public void AddProperty(DynProperty dynProperty) { if (dynProperty != null) { if (!_propertiesByName.ContainsKey(dynProperty.Name) && !_propertiesByID.ContainsKey(dynProperty.ID)) { _propertiesByName.Add(dynProperty.Name, dynProperty); _propertiesByID.Add(dynProperty.ID, dynProperty); } else { throw new ApplicationException(string.Format("当前类{0}已经存在名称为{1}或ID为{2}的属性", _name, dynProperty.Name, dynProperty.ID)); } } else { throw new ApplicationException("添加的属性不能为null"); } }
/// <summary> /// 根据名称移除属性 /// </summary> /// <param name="propertyName">属性名称</param> public void RemoveProperty(string propertyName) { if (!string.IsNullOrEmpty(propertyName)) { if (_propertiesByName.ContainsKey(propertyName)) { DynProperty dynProperty = _propertiesByName[propertyName]; _propertiesByName.Remove(propertyName); _propertiesByID.Remove(dynProperty.ID); } else { throw new ApplicationException(string.Format("当前类{0}不存在属性名为{1}的属性", _name, propertyName)); } } else { throw new ApplicationException("属性名不能为空或null"); } }
/// <summary> /// Get the property value. /// </summary> /// <param name="propertyName"></param> /// <returns>The value.</returns> public object GetPropertyValue(string propertyName) { DynProperty dynProperty = GetProperty(propertyName); object value = null; if (_propertyValues.TryGetValue(propertyName, out value)) { return(value); } else if (dynProperty != null) { SetPropertyValue(propertyName, null); return(_propertyValues[propertyName]); } else { throw new ApplicationException(string.Format("类{0}不存在属性{1}", _dynClass == null ? "[无]" : _dynClass.Name, propertyName)); } }
/// <summary> /// 转化符合属性类型的返回值 /// </summary> /// <param name="obj">输入的值</param> /// <param name="dynProperty">输入的参数的属性</param> /// <param name="result">返回的转化好类型的值</param> /// <returns>是否转换成功</returns> public bool TryPasentValue(object obj, DynProperty dynProperty, ref object result) { bool isSucces = false; try { switch (dynProperty.CollectionType) { case CollectionType.None: switch (dynProperty.DynType) { case DynType.Void: break; case DynType.Bool: result = Convert.ToBoolean(obj); isSucces = true; break; case DynType.Byte: result = Convert.ToByte(obj); isSucces = true; break; case DynType.String: result = Convert.ToString(obj); isSucces = true; break; case DynType.Struct: isSucces = false; break; case DynType.DateTime: result = Convert.ToDateTime(obj); isSucces = true; break; case DynType.Double: result = Convert.ToDouble(obj); isSucces = true; break; case DynType.Decimal: result = Convert.ToDecimal(obj); isSucces = true; break; case DynType.I16: result = Convert.ToInt16(obj); isSucces = true; break; case DynType.I32: result = Convert.ToInt32(obj); isSucces = true; break; case DynType.I64: result = Convert.ToInt64(obj); isSucces = true; break; default: break; } break; case CollectionType.List: switch (dynProperty.DynType) { case DynType.Void: case DynType.Bool: if (obj is string) { string[] trueObjs = obj.ToString().Split(','); List <Boolean> trueValue = new List <Boolean>(); foreach (var trueObj in trueObjs) { trueValue.Add(Convert.ToBoolean(trueObj)); } result = trueValue; } else { isSucces = obj is List <Boolean>; result = obj as List <Boolean>; } break; case DynType.Byte: if (obj is string) { string[] trueObjs = obj.ToString().Split(','); List <Byte> trueValue = new List <Byte>(); foreach (var trueObj in trueObjs) { trueValue.Add(Convert.ToByte(trueObj)); } result = trueValue; } else { isSucces = obj is List <Byte>; result = obj as List <Byte>; } break; case DynType.String: if (obj is string) { string[] trueObjs = obj.ToString().Split(','); List <String> trueValue = new List <String>(); foreach (var trueObj in trueObjs) { trueValue.Add(Convert.ToString(trueObj)); } result = trueValue; } else { isSucces = obj is List <String>; result = obj as List <String>; } break; case DynType.Struct: isSucces = false; break; case DynType.Double: if (obj is string) { string[] trueObjs = obj.ToString().Split(','); List <Double> trueValue = new List <Double>(); foreach (var trueObj in trueObjs) { trueValue.Add(Convert.ToDouble(trueObj)); } result = trueValue; } else { isSucces = obj is List <Double>; result = obj as List <Double>; } break; case DynType.Decimal: if (obj is string) { string[] trueObjs = obj.ToString().Split(','); List <Decimal> trueValue = new List <Decimal>(); foreach (var trueObj in trueObjs) { trueValue.Add(Convert.ToDecimal(trueObj)); } result = trueValue; } else { isSucces = obj is List <Decimal>; result = obj as List <Decimal>; } break; case DynType.I16: if (obj is string) { string[] trueObjs = obj.ToString().Split(','); List <Int16> trueValue = new List <Int16>(); foreach (var trueObj in trueObjs) { trueValue.Add(Convert.ToInt16(trueObj)); } result = trueValue; } else { isSucces = obj is List <Int16>; result = obj as List <Int16>; } break; case DynType.I32: if (obj is string) { string[] trueObjs = obj.ToString().Split(','); List <Int32> trueValue = new List <Int32>(); foreach (var trueObj in trueObjs) { trueValue.Add(Convert.ToInt32(trueObj)); } result = trueValue; } else { isSucces = obj is List <Int32>; result = obj as List <Int32>; } break; case DynType.I64: if (obj is string) { string[] trueObjs = obj.ToString().Split(','); List <Int64> trueValue = new List <Int64>(); foreach (var trueObj in trueObjs) { trueValue.Add(Convert.ToInt64(trueObj)); } result = trueValue; } else { isSucces = obj is List <Int64>; result = obj as List <Int64>; } break; case DynType.DateTime: if (obj is string) { string[] trueObjs = obj.ToString().Split(','); List <DateTime> trueValue = new List <DateTime>(); foreach (var trueObj in trueObjs) { trueValue.Add(Convert.ToDateTime(trueObj)); } result = trueValue; } else { isSucces = obj is List <DateTime>; result = obj as List <DateTime>; } break; default: break; } break; case CollectionType.Map: if (obj is string) { string objStr = obj.ToString(); if (objStr.Contains(":")) { string[] trueObjs = obj.ToString().Split(','); Dictionary <string, object> trueValue = new Dictionary <string, object>(); foreach (var trueObj in trueObjs) { string[] trueObjKeyValue = trueObj.Split(':'); trueValue.Add(trueObjKeyValue[0], trueObj.Substring(trueObjKeyValue[0].Length)); } isSucces = true; result = trueValue; } } break; } } catch { isSucces = false; } return(isSucces); }
/// <summary> /// Set the property value /// </summary> /// <param name="propertyName"></param> /// <param name="propertyValue"></param> public void SetPropertyValue(DynProperty property, object propertyValue) { if (propertyValue != null) { CollectionType collectionType = property.CollectionType; DynType dynType = property.DynType; string structName = property.StructName; bool isCorrectType = false; #region 类型判断 switch (collectionType) { case CollectionType.None: switch (dynType) { case DynType.Void: break; case DynType.Bool: isCorrectType = propertyValue is Boolean; break; case DynType.Byte: isCorrectType = propertyValue is Byte; break; case DynType.Double: isCorrectType = propertyValue is Double || propertyValue is Single || propertyValue is Int64 || propertyValue is Int32 || propertyValue is UInt32 || propertyValue is UInt16 || propertyValue is Int16; break; case DynType.Decimal: isCorrectType = propertyValue is Decimal || propertyValue is Double || propertyValue is Single || propertyValue is Int64 || propertyValue is Int32 || propertyValue is UInt32 || propertyValue is UInt16 || propertyValue is Int16; break; case DynType.I16: isCorrectType = propertyValue is Int16; break; case DynType.I32: isCorrectType = propertyValue is Int32 || propertyValue is UInt16 || propertyValue is Int16; break; case DynType.I64: isCorrectType = propertyValue is Int64 || propertyValue is Int32 || propertyValue is UInt32 || propertyValue is UInt16 || propertyValue is Int16; break; case DynType.String: isCorrectType = propertyValue is String; break; case DynType.DateTime: isCorrectType = propertyValue is DateTime; break; case DynType.Struct: isCorrectType = propertyValue is DynObject; break; case DynType.Binary: isCorrectType = propertyValue is byte[]; break; default: break; } break; case CollectionType.List: switch (dynType) { case DynType.Void: break; case DynType.Bool: isCorrectType = propertyValue is List <Boolean>; break; case DynType.Byte: isCorrectType = propertyValue is List <Byte>; break; case DynType.Double: isCorrectType = propertyValue is List <Double>; break; case DynType.Decimal: isCorrectType = propertyValue is List <Decimal>; break; case DynType.I16: isCorrectType = propertyValue is List <Int16>; break; case DynType.I32: isCorrectType = propertyValue is List <Int32>; break; case DynType.I64: isCorrectType = propertyValue is List <Int64>; break; case DynType.String: isCorrectType = propertyValue is List <String>; break; case DynType.DateTime: isCorrectType = propertyValue is List <String>; break; case DynType.Struct: isCorrectType = propertyValue is List <DynObject>; break; default: break; } break; case CollectionType.Set: break; case CollectionType.Map: isCorrectType = propertyValue is Dictionary <string, object>; break; default: break; } #endregion if (isCorrectType) { _propertyValues[property.Name] = propertyValue; } else { string msg = string.Format("属性{0}应该是{1}:{2}类型,而把{3}类型赋给它", property.Name, Enum.GetName(typeof(CollectionType), collectionType), Enum.GetName(typeof(DynType), dynType), propertyValue.GetType().ToString()); throw new ApplicationException(msg); } } else { SetDefaultValue(property); } }