集体更换异常类所在命名空间,因为异常经常用到,所以应该让它享受更高层次的命名空间
Stone authored at 2014-12-03 16:04:24
1.69 KiB
X_NET20
using NewLife.Xml;

namespace NewLife.Serialization
{
    /// <summary>读写器服务。将来可以改为对象容器支持</summary>
    static class RWService
    {
        public static IReader CreateReader(RWKinds kind)
        {
            switch (kind)
            {
                case RWKinds.Binary:
                    return new BinaryReaderX();
                case RWKinds.Xml:
                    return new XmlReaderX();
                case RWKinds.Json:
                    return new JsonReader();
                default:
                    break;
            }
            return null;
        }

        public static IWriter CreateWriter(RWKinds kind)
        {
            switch (kind)
            {
                case RWKinds.Binary:
                    return new BinaryWriterX();
                case RWKinds.Xml:
                    return new XmlWriterX();
                case RWKinds.Json:
                    return new JsonWriter();
                default:
                    break;
            }
            return null;
        }

        public static RWKinds GetKind(this IReaderWriter rw)
        {
            var type = rw.GetType();
            if (type == typeof(BinaryReaderX)) return RWKinds.Binary;
            if (type == typeof(BinaryWriterX)) return RWKinds.Binary;
            if (type == typeof(XmlReaderX)) return RWKinds.Xml;
            if (type == typeof(XmlWriterX)) return RWKinds.Xml;
            if (type == typeof(JsonReader)) return RWKinds.Json;
            if (type == typeof(JsonWriter)) return RWKinds.Json;

            throw new XException("未识别的读写器类型{0}!", type);
        }
    }
}