PWMIS.Core/Entity/PdfNetSerialize.cs

(开头部分) 10KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

using System;
using System.Collections.Generic;
using System.Text;
using PWMIS.DataMap.Entity;
using System.IO;
using Entity.Common;

namespace PWMIS.DataMap.Entity
{
     
    /// <summary>
    /// PDF.NET 实体类序列化器
    /// </summary>
    public class PdfNetSerialize
    {
       public  const int ENTITY_ITEM_FLAG = 1010280;//纪念今天的日子 2010.10.28
       public  const int ENTITY_ARRAY_FLAG = 1010281;

        /// <summary>
        /// 二进制序列化
        /// </summary>
        /// <param name="entity">实体类实例</param>
        /// <returns>字节数组</returns>
        public static byte[] BinarySerialize(EntityBase entity)
        {
            MemoryStream ms = new MemoryStream();
            BinaryWriter bw = new BinaryWriter(ms);
            Type factEntityType = entity.GetType();
            EntityFields ef = EntityFieldsCache.Item(factEntityType);
            byte b;
            //写入实体类标记
            bw.Write(ENTITY_ITEM_FLAG);
            int length = entity.PropertyValues.Length-1;
            for (int i = 0; i <= length; i++)
            {
                object obj = entity.PropertyValues[i];
                //if (obj == System.DBNull.Value) obj = null;//DBNull.Value在Convert 的时候会失败
                //为每个属性添加null标记
                if (obj == System.DBNull.Value || obj == null)
                    b = 0;//NULL标记
                else
                {
                    if (length == i) b = byte.MaxValue; else b = 1;//如果标志位为 byte.MaxValue ,则表示已经写入所有的属性字段,可用于以后添加实体属性但又要成功读取原有的记录
                }
                

                bw.Write(b); //写入标记
                if (b >0 )
                {
                    Type propertyType = ef.GetPropertyType(entity.PropertyNames[i]);
                    if (propertyType == null)
                        throw new Exception("PDF.NET实体类序列化错误:未知的实体属性类型,请检查实体类的属性和字段定义是否匹配。");

                    switch (propertyType.Name )
                    {
                        case "Int32":
                            bw.Write(Convert.ToInt32(obj)); break;
                        case "String":
                            bw.Write(Convert.ToString(obj));
                            break;
后面还有 205 行代码,购买后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。