加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C#-protobuf-net-列出支持的类型

发布时间:2020-12-15 07:50:44 所属栏目:百科 来源:网络整理
导读:我正在开发一个自定义的ProtoBufFormatter(:MediaTypeFormatter),它能够将自己的类型动态注册到用于序列化/反序列化的RuntimeTypeModel. 为了减少对try {} catch {}块的需求,在将已经支持的类型添加到RuntimeTypeModel之前,最好先过滤掉它们.自述文件仅提供

我正在开发一个自定义的ProtoBufFormatter(:MediaTypeFormatter),它能够将自己的类型动态注册到用于序列化/反序列化的RuntimeTypeModel.

为了减少对try {} catch {}块的需求,在将已经支持的类型添加到RuntimeTypeModel之前,最好先过滤掉它们.自述文件仅提供默认情况下受支持的“模糊”列表类型,而Model.GetTypes()方法仅返回手动添加到当前模型的类型列表.

自述文件: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<>)
    };
最佳答案
尝试:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读