Exemplo n.º 1
0
        /// <summary>
        /// 判断是否是自身有效的字节序列
        /// </summary>
        /// <param name="ntkBytes">网络字节序列</param>
        /// <param name="targetType">Type of the target.</param>
        /// <param name="diffWriter">The diff writer.</param>
        /// <param name="instance">输出有效的实例</param>
        /// <returns>
        ///     <c>true</c> if [is valid instance] [the specified NTK bytes]; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsValidInstance(byte[] ntkBytes, Type targetType, TextWriter diffWriter, out ESPDataBase instance)
        {
            MethodInfo gm = typeof(ESPDataBase).GetMethod("IsValidESPInstance",
                                                          BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Static);

            gm = gm.MakeGenericMethod(targetType);

            ValidESPDataBaseWrap wrap = gm.Invoke(null, new object[] { ntkBytes, diffWriter }) as ValidESPDataBaseWrap;

            if (wrap != null)
            {
                instance = wrap.Instance;
                return(wrap.IsValid);
            }
            else
            {
                instance = null;
                return(false);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 判断当前实例类型绑定的相关网络序列是否有效
        /// </summary>
        /// <typeparam name="TEntity">实例类型</typeparam>
        /// <param name="ntkBytes">网络序列</param>
        /// <param name="diffWriter">二进制标记文本输出</param>
        /// <returns>如果是有效的的协议数据则返回为true。</returns>
        public static ValidESPDataBaseWrap IsValidESPInstance <TEntity>(byte[] ntkBytes, TextWriter diffWriter)
            where TEntity : ESPDataBase, new()
        {
            ValidESPDataBaseWrap wrap     = new ValidESPDataBaseWrap();
            ESPDataBase          instance = null;

            try
            {
                wrap.IsValid = true;
                instance     = ESPDataBase.BindFromNetworkBytes <TEntity>(ntkBytes);
            }
            catch (Exception bindExp)
            {
                wrap.IsValid = false;
                instance     = new TEntity();
                if (diffWriter != null)
                {
                    string outMsg = string.Empty;
                    bindExp = SpecUtil.GetTriggerException(bindExp, ref outMsg);
                    diffWriter.WriteLine("*对象绑定失败,异常:{0}", outMsg + bindExp.ToString());
                }
            }

            byte[] bytes2cmp = instance.GetNetworkBytes();
            bool   blnResult = SpecUtil.AreEqual(bytes2cmp, ntkBytes);

            if (diffWriter != null && blnResult == false)
            {
                diffWriter.WriteLine("数据类型:{0}", typeof(TEntity).AssemblyQualifiedName);
                diffWriter.WriteLine("原始序列 长度:{0}\r\n{1}", ntkBytes.Length, ntkBytes.GetHexViewString());
                diffWriter.WriteLine();
                diffWriter.WriteLine("绑定序列 长度:{0}\r\n{1}", bytes2cmp.Length, bytes2cmp.GetHexViewString());
            }
            wrap.Instance = instance;
            return(wrap);
        }