Ejemplo n.º 1
0
        private static Object _arrayDeserializer(MessageFieldInfo fields_info, Object _obj)
        {
            var       attribute  = fields_info.Attribute;
            FieldInfo field_info = fields_info.Info;
            //var array_value = field_info.GetValue( _obj );
            object array = null;
            Type   item_type;

            if (field_info.FieldType.IsArray)
            {
                //array = Activator.CreateInstance( field_info.FieldType, new object[] { attribute.Length } );

                item_type = field_info.FieldType.GetElementType();
                array     = Array.CreateInstance(item_type, attribute.Length);
            }
            else
            {
                array     = Activator.CreateInstance(field_info.FieldType);
                item_type = field_info.FieldType.GetGenericArguments()[0];
            }

            object [] value = new object[1];
            for (Int32 i = 0; i < attribute.Length; ++i)
            {
                if (item_type.IsPrimitive)
                {
                    value[0] = _primitiveDeserializer(item_type);
                }
                else if (item_type == s_string_type)
                {
                    if (attribute.Length2 == 0)
                    {
                        Utility.Log.Error("字符串数组{0},字符串长度为零!", field_info.Name);
                        break;
                    }
                    value[0] = s_serialize_reader.ReadString(attribute.Length2);
                    if (string.Empty == value[0] as string)
                    {
                        value[0] = string.Empty;
                    }
                }
                else if (item_type.IsClass)
                {
                    value[0] = _deserializer(item_type);
                }
                else
                {
                    Utility.Log.Exception("消息内数组: {0} 容纳类型: {1} 为不支持类型", field_info.Name, item_type.Name);
                }
                if (field_info.FieldType.IsArray)
                {
                    ((Array)array).SetValue(value[0], i);
                }
                else
                {
                    field_info.FieldType.InvokeMember("Add", BindingFlags.DeclaredOnly |
                                                      BindingFlags.Public | BindingFlags.NonPublic |
                                                      BindingFlags.Instance | BindingFlags.InvokeMethod, null, array, value);
                }
            }
            return(array);
        }
        internal static string GetDetailedExceptionMessage(this Exception ex, bool forUser)
        {
            // Must handle EVException separately otherwise its ToString would call us again and cause infinite recursion
            if (ex is EVException)
            {
                return(String.Empty);
            }

            if (forUser)
            {
                // Suppress default message CLR produces if no message is specified in exception
                string realMessage = MessageFieldInfo.GetValue(ex) as string;
                if (String.IsNullOrWhiteSpace(realMessage))
                {
                    return(String.Empty);
                }
            }

            // Debugging
            //FieldInfo[] exceptionFields = typeof(Exception).GetFields(BindingFlags.NonPublic | BindingFlags.Instance);

            // Save InnerException so we can restore it back
            Exception innerException = ex.InnerException;

            // Reset InnerException so that ToString() would not go recursive
            InnerExceptionFieldInfo.SetValue(ex, null);

            // Save StackTraceString so we can restore it back
            object strStackTraceString = StackTraceStringFieldInfo.GetValue(ex);

            // Reset strStackTraceString to empty string so that it would not show up in ToString() output
            StackTraceStringFieldInfo.SetValue(ex, String.Empty);

            // Getting single level (non-recursive) ToString() without StackTrace
            string detailedExceptionMessage = ex.ToString();

            if (forUser)
            {
                // End users are not interested to see the type of exception being thrown. Unfortunately most system exceptions tend to
                // prepend it in their implementation of ToString() overload.
                string typePrefix = ex.GetType().FullName + ": ";
                detailedExceptionMessage = Utils.RemovePrefix(detailedExceptionMessage, typePrefix);
            }

            detailedExceptionMessage = Utils.RemoveSuffix(detailedExceptionMessage, "\r\n");

            IOException ioEx = ex as IOException;

            if (ioEx != null && !ex.GetOrCreatePayload().ContainsProperty(FileNameLabel))
            {
                object objFileName = MaybeFullPathFieldInfo.GetValue(ioEx);
                string fileName    = objFileName == null ? "Unknown" : objFileName.ToString();
                ex.AddNamedProperty(FileNameLabel, fileName);
            }

            // Restoring InnerException and StackTrace back to their original values
            InnerExceptionFieldInfo.SetValue(ex, innerException);
            StackTraceStringFieldInfo.SetValue(ex, strStackTraceString);

            return(detailedExceptionMessage);
        }
Ejemplo n.º 3
0
        private static Int32 _arraySerializer(MessageFieldInfo fields_info, Object _obj)
        {
            var       attribute   = fields_info.Attribute;
            FieldInfo field_info  = fields_info.Info;
            var       array_value = field_info.GetValue(_obj);

            Type item_type;

            // 标准数组
            if (field_info.FieldType.IsArray)
            {
                item_type = field_info.FieldType.GetElementType();
            }
            else
            {
                item_type = field_info.FieldType.GetGenericArguments()[0];
            }
            Int32 item_size    = 0;
            Int32 remain_count = attribute.Length;

            if (array_value == null)
            {
                Utility.Log.Exception("数组{0}({1}, {2})为空,序列化失败!", field_info.Name, field_info.FieldType.Name, item_type.Name);
            }
            foreach (var item in array_value as IList)
            {
                if (remain_count < 0)
                {
                    Utility.Log.Error("数组{0}({1}, {2})实际长度超过可容纳长度: {2}"
                                      , field_info.Name, field_info.FieldType.Name, item_type.Name, attribute.Length);
                    break;
                }
                else if (item == null)
                {
                    continue;
                }
                else if (item_type.IsPrimitive)
                {
                    item_size = _primitiveSerializer(item, item_type);
                }
                else if (item_type == s_string_type)
                {
                    if (attribute.Length2 == 0)
                    {
                        Utility.Log.Error("字符串数组{0},字符串长度为零!", field_info.Name);
                        item_size = 0;
                        break;
                    }
                    item_size += s_serialize_writer.Write((String)item, attribute.Length2);
                }
                else if (item_type.IsClass)
                {
                    item_size = _serializer(item, item_type);
                }
                else
                {
                    Utility.Log.Exception("消息内数组: {0} 容纳类型: {1} 为不支持类型", field_info.Name, item_type.Name);
                }
                --remain_count;
            }
            if (remain_count == attribute.Length)
            {
                Utility.Log.Exception("数组: {0}({1}, {2}), 元素数量为0 或者数组中没有对象实体,序列化失败!", field_info.Name, field_info.FieldType.Name, item_type.Name);
            }

            Int32 array_size = item_size * attribute.Length;

            s_serialize_writer.WriteZero(item_size * remain_count);
            return(array_size);
        }