/// <summary> /// 将指定类型的实例序列化为二进制数据 /// </summary> /// <param name="type">类型</param> /// <param name="instance">实例对象</param> /// <returns>返回序列化后的二进制数据, 如果instance为null, 则返回null</returns> /// <exception cref="ArgumentNullException">type 参数不能为空</exception> /// <exception cref="NotSupportedException">不被支持的类型</exception> public static byte[] ToBytes(Type type, object instance) { if (type == null) { throw new ArgumentNullException("type"); } if (instance == null) { return(null); } MemoryAllotter.Instance.Initialize(); IMemorySegmentProxy proxy = null; try { proxy = MemorySegmentProxyFactory.Create(); ToBytes(type, instance, proxy); return(proxy.GetBytes()); } catch { if (proxy != null) { proxy.Dispose(); } throw; } }
/// <summary> /// 第三方数据转换成元数据 /// </summary> /// <param name="metadata">第三方数据对象</param> /// <returns>元数据的二进制表现形式</returns> public static byte[] ToBytes(ResourceBlock metadata) { if (metadata == null) { return(null); } MemoryAllotter.Instance.Initialize(); IMemorySegmentProxy proxy = null; try { proxy = MemorySegmentProxyFactory.Create(); ToBytes(metadata, proxy); return(proxy.GetBytes()); } catch { if (proxy != null) { proxy.Dispose(); } throw; } }
/// <summary> /// 将一个智能对象转换为二进制元数据 /// </summary> /// <param name="obj">智能对象</param> /// <returns>返回二进制元数据</returns> /// <exception cref="PropertyNullValueException">字段相关的Attribute.IsRequire = true, 并且该字段的值为null</exception> /// <exception cref="UnexpectedValueException">不期待的结果异常,通常因为对Blob类型的取值 = null</exception> /// <exception cref="NotSupportedException">系统不支持的序列化类型</exception> /// <exception cref="DefineNoMeaningException">无意义的智能字段Attribute值</exception> /// <exception cref="MethodAccessException">类型权限定义错误</exception> /// <exception cref="Exception">内部错误</exception> public static byte[] ToBytes(IIntellectObject obj) { if (obj == null) { return(null); } MemoryAllotter.Instance.Initialize(); IMemorySegmentProxy proxy = null; try { proxy = MemorySegmentProxyFactory.Create(); ToBytes(obj, proxy); return(proxy.GetBytes()); } catch { if (proxy != null) { proxy.Dispose(); } throw; } }