private bool InternalAddItem(TItem item, TOpEnum op, InsertionBehavior behavior) { if (item == null) { return(false); } var itemDataType = GetItemOrgDataType(item); if (itemDataType != null) { if (_matchedItemsType != null) //本块内所含各项与项,项与块整集之间的匹配类型 { var tmpType = TypeMatcher.MatchDataTtype(_matchedItemsType, itemDataType, out _); if (tmpType != null) { _matchedItemsType = tmpType; } else //匹配失败 { throw new ArithmeticException(MsgStrings.InvalidTypeConvert(itemDataType, _matchedItemsType)); } } else { _matchedItemsType = itemDataType; //首次加入的具有非空值项, } } ReMatchDataType(); _itemVersion++; _hashCodeVersion++; return(m_items.Add(op, item, behavior)); }
/// <summary> /// 设置相等比较器实例 /// </summary> public void SetEqualComparer(QueryEqualComparer <TBlock> value) { if (value == null) { throw new ArgumentNullException("SetEqualComparer", MsgStrings.ValueCannotNull("QueryBlock")); } _equalComparer = value as QueryEqualComparer <TBlock> ?? throw new InvalidCastException(MsgStrings.InvalidTypeConvert(value?.GetType(), typeof(QueryEqualComparer <TBlock>))); }
/// <summary> /// 用于批量填充Items重新匹配类型 /// </summary> /// <param name="items"></param> private void ReMatchItemsDataType(NodeOperatorPair <TOpEnum, TItem>[] items) { if (items != null && items.Length > 0) { int start; var count = items.Length; var item = items[0]?.Node; for (start = 1; start < count; start++) //拿到第一个有效项 { item = items[start]?.Node; if (item != null) { break; } } Type tmpType, lastType; lastType = GetItemOrgDataType(item); for (int i = start - 1; i < count; i++) { item = items[i]?.Node; tmpType = GetItemOrgDataType(item); if (tmpType != null) { if (lastType != null) { tmpType = TypeMatcher.MatchDataTtype(lastType, tmpType, out _); if (tmpType != null) { lastType = tmpType; } else//匹配失败 { throw new ArithmeticException(MsgStrings.InvalidTypeConvert(lastType, GetItemOrgDataType(item))); } } else { lastType = tmpType; } }//else 空值没有类型匹配 } _matchedItemsType = lastType; } else { _matchedItemsType = null; } ReMatchDataType(); }
public int GetSize() { int returned = 28; if (Type == 1) { returned += 4; } if (MsgStrings.Length > 0) { returned += 4 + 4 * MsgStrings.Length; } returned += MsgStrings.Sum(x => x.Length); returned += IOTools.Alignment(returned, 4); return(returned); }
/// <summary> /// 用于批量填充Blocks重新匹配类型 /// </summary> /// <param name="blocks"></param> private void ReMatchSubBlocksDataType(params NodeOperatorPair <TOpEnum, TBlock>[] blocks) { if (blocks != null && blocks.Length > 0) { var count = blocks.Length; var block = blocks[0]?.Node; Type tmpType, lastType = GetBlockOrgDataType(block); for (int i = 1; i < count; i++) { block = blocks[i]?.Node; tmpType = block?.TypeAs?.GetElementType() ?? block?.DataType?.GetElementType(); if (tmpType != null) { if (lastType != null) { tmpType = TypeMatcher.MatchDataTtype(lastType, tmpType, out _); if (tmpType != null) { lastType = tmpType; } else//匹配失败 { throw new ArithmeticException(MsgStrings.InvalidTypeConvert(lastType, GetBlockOrgDataType(block))); } } else { lastType = tmpType; } }//else 空值没有类型匹配 } _matchedSubBlocksDataType = lastType; } else { _matchedSubBlocksDataType = null; } ReMatchDataType(); }
/// <summary> /// 在匹配项或块集后,进行项与块集整体之间的匹配 /// </summary> private void ReMatchDataType() { if (_matchedItemsType != null && _matchedSubBlocksDataType != null) { _matchedDataType = TypeMatcher.MatchDataTtype(_matchedItemsType, _matchedSubBlocksDataType, out _); if (_matchedItemsType == null) { throw new ArithmeticException(MsgStrings.InvalidTypeConvert(_matchedDataType, _matchedSubBlocksDataType)); } } else if (_matchedItemsType != null) { _matchedDataType = _matchedItemsType; } else if (_matchedSubBlocksDataType != null) { _matchedDataType = _matchedSubBlocksDataType; } else { _matchedDataType = null; } }
private bool InternalAddBlock(TBlock block, TOpEnum op, InsertionBehavior behavior) { if (block == null) { return(false); } //var blockType = typeof(TBlock); //var blockAllCount = blockType.GetProperty(nameof(AllCount))?.GetValue(block) ?? 0; var blockAllCount = block.AllCount; if ((int)blockAllCount == 0) { return(false); } //var blockParent = blockType.GetProperty(nameof(ParentBlock)); //blockParent.SetValue(block, this); var subBlockDataType = GetBlockOrgDataType(block); if (subBlockDataType != null) { if (_matchedSubBlocksDataType != null) //本块内所含各子块之间的匹配类型 { var tmpType = TypeMatcher.MatchDataTtype(_matchedSubBlocksDataType, subBlockDataType, out _); if (tmpType != null) { _matchedSubBlocksDataType = tmpType; } else //匹配失败 { throw new ArithmeticException(MsgStrings.InvalidTypeConvert(subBlockDataType, _matchedSubBlocksDataType)); } } else { _matchedSubBlocksDataType = subBlockDataType; //加入的是第一个具有块类型的子块, } //if (_matchedDataType != null) //需要重新匹配本块的类型 //{ // tmpType = TypeMatcher.MatchDataTtype(_matchedDataType, _matchedSubBlocksDataType, out _); // if (tmpType != null) // _matchedDataType = tmpType; // else //匹配失败 // throw new ArithmeticException(MsgStrings.InvalidTypeConvert( subBlockDataType)); //} //else //{ // _matchedDataType = _matchedSubBlocksDataType;//首次确认本块的整体类型 //} ReMatchDataType(); } _blockVersion++; _hashCodeVersion++; return(m_blocks.Add(op, block, behavior)); }
/// <summary> /// 根据指定类型,生成一个可空的泛型类型。 /// </summary> /// <param name="type"></param> /// <returns>如果类型是值类型,返回实际类型为本类型的Nullable封闭式泛型,否则返回本类型.</returns> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="ArgumentException"></exception> /// <exception cref="InvalidOperationException"></exception> /// <exception cref="NotSupportedException"></exception> internal static Type MakeNullableType(this Type type) { Debug.Assert(type != null, MsgStrings.ParameterType + MsgStrings.ValueCannotNull("TypeExtentions.MakeNullableType")); return(type.IsNullableType() ? type : typeof(Nullable <>).MakeGenericType(type)); }
/// <summary> /// 将查询值转换至列定义的基本类型,成功则输出转换后的值,失败则输出原值 /// </summary> /// <param name="value"></param> /// <param name="destType">期望的目标类型,通常是被查询属性的基本数据类型,</param> /// <returns>成功:返回True,失败:返回False</returns> /// <exception cref="InvalidCastException"/> public static bool TryConvertSingleValueType(object value, Type destType, out object outValue) { outValue = value; if (value == null) //空值不需要处理 { return(true); } if (destType == null) //目标类型错误 { return(false); } Type orgType = value.GetType(); //是否类型相同 if ((orgType == destType) ||//类型相同,//若是Nullable<T>,取其基础类型,//查询值的类型与字段值类型是否相同 (destType.IsNullableType(out var destUnderlyingType) && destUnderlyingType == orgType) || (orgType.IsNullableType(out var orgUnderlyingType) && destUnderlyingType == orgUnderlyingType)) { return(true); } //throw new Exception(string.Format(MsgStrings.TypeCannotNull, MsgStrings.DestTypeForConvert)); dynamic tmpVal; try { //字符串转布尔型,若转换失败则=false if (destType == Constants.Static_TypeOfBool && orgType == Constants.Static_TypeOfString) { tmpVal = false; var strValue = ((string)value).Trim(); var valueSize = strValue.Length; if (valueSize == 1 && strValue[0] == 1) //比使用数字测方式快10倍 { tmpVal = true; //=1的数字视作true,其它数字视为false } else { for (var i = 0; i < _boolTrueValueStrings.Length; i++) { var str = _boolTrueValueStrings[i]; if (valueSize == str.Length) { if (string.CompareOrdinal(str, strValue) == 0 || (string.Compare(str, strValue, StringComparison.OrdinalIgnoreCase) == 0)) { tmpVal = true; break; } } } } } else //日期型转换 if (destType == Constants.Static_TypeOfDateTime) { if (DateTime.TryParse(value.ToString(), out DateTime outDate)) { tmpVal = outDate; } else { return(false); } } else { if (!(value is IConvertible)) //不支持转换 { return(false); } if (destType.IsNullableType(out destType)) { tmpVal = Convert.ChangeType(value, destType); } else #if DEBUG { throw new InvalidCastException(MsgStrings.InvalidTypeConvert(orgType, destType)); } #endif { return(false); } } } catch (Exception ex) { return(false); //throw new Exception(MsgStrings.InvalidTypeConvert + "\n\r" + ex.Message); } outValue = tmpVal; return(true); }