protobuf-net - 支持的类型列表

4
我正在开发一个自定义的ProtoBufFormatter(: MediaTypeFormatter),它能够在运行时将自己的类型注册到用于序列化/反序列化的RuntimeTypeModel中。
为了减少try{}catch{}块的需求,最好在将它们添加到RuntimeTypeModel之前过滤掉已经支持的类型。Readme文件只提供了一个“模糊”的默认支持类型列表,而Model.GetTypes()方法只返回手动添加到当前模型的类型列表。
Readme: https://github.com/mgravell/protobuf-net 我正在使用protobuf-net 2.4.0
所以我想知道是否有任何简单的方法来检查一个类型是否已经被当前的RuntimeTypeModel支持?目前,我正在使用类似以下代码来预先过滤类型:
    private bool IsSimpleType(Type type)
    {
        return
            type.IsPrimitive ||
            _additionalSimpleTypes.Contains(type) ||
            Convert.GetTypeCode(type) != TypeCode.Object ||
            (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>) && IsSimpleType(type.GetGenericArguments()[0]));
    }

    private Type[] _additionalSimpleTypes = new Type[]
    {
                typeof(Enum),
                typeof(String),
                typeof(String[]),
                typeof(Decimal),
                typeof(DateTime),
                typeof(DateTimeOffset),
                typeof(TimeSpan),
                typeof(Guid),
                typeof(Uri),
                typeof(Byte),
                typeof(Byte[]),
                typeof(Char),
                typeof(Boolean),
                typeof(Object),
                typeof(Version)
    };

    private Type[] _listTypes = new Type[]
    {
        typeof(Enum),
                typeof(IEnumerable<>),
                typeof(List<>),
                typeof(IList<>)
    };

我认为目前没有这样的方法;当然我们可以添加一个... - Marc Gravell
那太好了。还有其他的想法如何测试一个类型是否已经被支持,而不需要尝试和错误? - M. Altmann
1个回答

5

尝试:

 ProtoBuf.Meta.RuntimeTypeModel.Default.CanSerialize(Type type)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接