- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个看起来像这样的类层次结构。这些类包含许多我已排除的其他细节。这是一种专注于这些类的序列化方面的简化。
[ProtoInclude(1, typeof(Query<bool>))]
[ProtoInclude(2, typeof(Query<string>))]
[ProtoInclude(3, typeof(Query<int>))]
[ProtoInclude(4, typeof(Query<decimal>))]
[ProtoInclude(5, typeof(Query<DataSet>))]
abstract class Query
{
public string Result { get; set; }
}
[ProtoInclude(1, typeof(SpecialQuery)]
abstract class Query<T> : Query
{
public new T Result { get; set; }
}
abstract class SpecialQuery : Query<DataSet>
{
public new string Result { get; set; }
}
我还有大约 150 个泛型 Query 的自动生成后代,其中包含大量泛型类型。例如:
[ProtoContract]
class W : Query<bool>
{
}
[ProtoContract]
class X : Query<string>
{
}
[ProtoContract]
class Y : Query<int>
{
}
[ProtoContract]
class Z : SpecialQuery
{
}
我还为所有这些类型自动生成了 [ProtoInclude]。例如:
[ProtoInclude(1, typeof(W)]
[ProtoInclude(2, typeof(X)]
[ProtoInclude(3, typeof(Y)]
[ProtoInclude(4, typeof(Z)]
问题是,我该如何部署这 150 个 ProtoIncludes?我尝试了各种看似合乎逻辑的组合,但根据哪些属性出现在哪里,我得到了各种异常。上例中实际需要序列化的类型是 W、X、Y、Z,只有大约 150 个。
protobuf-net 甚至可以处理这样的事情,还是我应该尝试其他类型的序列化?
最佳答案
好的;通过更新的问题,我了解更多。我希望对象模型中间的泛型确实会让生活……“有趣”。它不是“开箱即用”的;我想看看是否可以做一些简单的调整来支持它,但它很快就开始变丑了。我希望如果可能的话,简单地删除中间对泛型的需求会更好——也许保留一个泛型 interface (而不是泛型类)。这是一些确实有效的代码;这如何映射到您的代码......我不能说 100%。请注意,您不必使用 TypeDescriptor
东西(等)- 似乎因为您使用的是代码生成器,所以这可能会使某些事情变得更容易...
(我没有检查 DataSet
的东西 - 只是类的东西)
using System;
using System.ComponentModel;
using System.Data;
using System.IO;
using NUnit.Framework;
using ProtoBuf;
[TestFixture]
public class ComplexGenericTest
{
[Test]
public void TestX()
{
Query query = new X { Result = "abc" };
Assert.AreEqual(typeof(string), query.GetQueryType());
Query clone = Serializer.DeepClone<Query>(query);
Assert.IsNotNull(clone);
Assert.AreNotSame(clone, query);
Assert.IsInstanceOfType(query.GetType(), clone);
Assert.AreEqual(((X)query).Result, ((X)clone).Result);
}
[Test]
public void TestY()
{
Query query = new Y { Result = 1234};
Assert.AreEqual(typeof(int), query.GetQueryType());
Query clone = Serializer.DeepClone<Query>(query);
Assert.IsNotNull(clone);
Assert.AreNotSame(clone, query);
Assert.IsInstanceOfType(query.GetType(), clone);
Assert.AreEqual(((Y)query).Result, ((Y)clone).Result);
}
}
public static class QueryExt {
public static Type GetQueryType(this IQuery query)
{
if (query == null) throw new ArgumentNullException("query");
foreach (Type type in query.GetType().GetInterfaces())
{
if (type.IsGenericType
&& type.GetGenericTypeDefinition() == typeof(IQuery<>))
{
return type.GetGenericArguments()[0];
}
}
throw new ArgumentException("No typed query implemented", "query");
}
}
public interface IQuery
{
string Result { get; set; }
}
public interface IQuery<T> : IQuery
{
new T Result { get; set; }
}
[ProtoInclude(21, typeof(W))]
[ProtoInclude(22, typeof(X))]
[ProtoInclude(23, typeof(Y))]
[ProtoInclude(25, typeof(SpecialQuery))]
[ProtoContract]
abstract class Query : IQuery
{
public string Result
{
get { return ResultString; }
set { ResultString = value; }
}
protected abstract string ResultString { get; set; }
// these are to allow simple ResultString implementations
// without the codegen having to worry about int.Parse etc
protected static string FormatQueryString<T>(T value)
{
return TypeDescriptor.GetConverter(typeof(T))
.ConvertToInvariantString(value);
}
protected static T ParseQueryString<T>(string value)
{
return (T) TypeDescriptor.GetConverter(typeof(T))
.ConvertFromInvariantString(value);
}
}
[ProtoContract]
[ProtoInclude(21, typeof(Z))]
abstract class SpecialQuery : Query, IQuery<DataSet>
{
public new DataSet Result { get; set; }
[ProtoMember(1)]
protected override string ResultString
{
get {
if (Result == null) return null;
using (StringWriter sw = new StringWriter())
{
Result.WriteXml(sw, XmlWriteMode.WriteSchema);
return sw.ToString();
}
}
set {
if (value == null) { Result = null; return; }
using (StringReader sr = new StringReader(value))
{
DataSet ds = new DataSet();
ds.ReadXml(sr, XmlReadMode.ReadSchema);
}
}
}
}
[ProtoContract]
class W : Query, IQuery<bool>
{
[ProtoMember(1)]
public new bool Result { get; set; }
protected override string ResultString
{
get {return FormatQueryString(Result); }
set { Result = ParseQueryString<bool>(value); }
}
}
[ProtoContract]
class X : Query, IQuery<string>
{
[ProtoMember(1)]
public new string Result { get; set; }
protected override string ResultString
{
get { return Result ; }
set { Result = value; }
}
}
[ProtoContract]
class Y : Query, IQuery<int>
{
[ProtoMember(1)]
public new int Result { get; set; }
protected override string ResultString
{
get { return FormatQueryString(Result); }
set { Result = ParseQueryString<int>(value); }
}
}
[ProtoContract]
class Z : SpecialQuery
{
}
关于c# - 具有通用类层次结构的 Protobuf 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1296791/
我试图了解是否有可能采用构成另一个 protobuf 一部分的序列化 protobuf 并将它们合并在一起而不必反序列化第一个 protobuf。 例如,给定一个 protobuf 包装器: synt
正如我最近发现的,我们可以使用两个类 ProtoBuf.Serializer 和 ProtoBuf.Meta.TypeModel 在 protobuf-net 中序列化/反序列化。例如,假设我们有一个
我正在尝试使用 protobuf 序列化我的下面的类,但它因“对象引用”错误而失败。更多详情如下。通过查看错误详细信息,您知道会出现什么问题吗?注意:我的用户对象太大了,它有太多的子对象和属性。所以不
我想识别要反序列化的文件是否是protobuf序列化的。这是因为我想提供不止一种选项来为用户序列化和反序列化文件。我正在使用 protobuf.net 序列化为 protobuf 格式。 最佳答案 不
我已经使用位于 https://protogen.marcgravell.com/ 的工具构建了我的 C# 类来自 https://developers.google.com/transit/gtfs
有一个通过 UDP 接受消息的 Go 服务器。使用这种设计,它只能扫描一种特定类型的实体,world.Entity . for { buf := make([]byte, 10
比如我想序列化和反序列化System.Drawing.Font这是不可变的,不能更改以适应 protobuf-net 约定。一般来说,是否可以在 protobuf-net 中编写某种“自定义”序列化程
我开始用 protobuf 2.2.0 构建一个应用程序,因为它是最新的。现在我正在考虑升级到最新的 protobuf 2.4.0a。 如果我这样做,对于同一架构,一个版本的应用程序生成的消息是否仍然
在我从 BinaryFormatter 切换到 protobuf-net 的过程中, 我在序列化集合时观察到了差异。 在下面的代码示例中,反序列化(protobuf-net v2r470)返回 如果在
知道正在发送的 protobuf 消息类型的 API 是什么? 例如,我使用以下方法获取 SendNameMessage 对象。 SendNameMessage sendNameObj = Seria
我在我们的一个项目中使用 protobuf-net 来序列化/反序列化一大组同类对象。它运行良好,速度非常快。不过只有一个问题。反序列化时是否可以使用 linq(或任何其他机制)指定过滤条件,以便加载
我正在尝试使用 protobuf-net 序列化一些对象,但不幸的是他们自由地使用了 DateTimeOffset , protobuf-net 尚不支持。这导致了很多: No serializer
我在 ionic2 项目中使用 protobuf.js。我有一个有效的 .proto 文件,我首先将其转换为静态 javascript 文件: pbjs -t static databaseapi.p
我通过 vcpkg vcpkg install protobuf:x64-windows 安装了 protobuf .显然它安装了最新版本(3.6.1)。对于我需要版本<=3.5.1的项目。有没有办法
我有以下类(class):- [Serializable] [DataContract(Name = "StateValueWrapper")] public class StateValueWrap
protobuf net 似乎不支持列表/数组的 AsReference 以及列表/数组内对象的 AsReference。这会在最终的 v2 中得到支持吗? [ProtoMember(1, AsRef
我正在使用 protobuf-net 来序列化和反序列化我的消息。我的消息还包含可以为空的字符串。但是,当我在另一侧反序列化它们时,我得到空字符串 ("")。 根据谷歌文档,空字符串中字符串类型的默认
我已经阅读了有关继承的各种帖子,并且 Protocol Buffer 不支持继承。我不想继承 Protocol Buffers 消息,而是继承,这样我就可以轻松处理我的所有 Protocol Buff
我知道带有 protobuf.net 的列表不支持 AsReference,因此我尝试了解决此限制的方法。我创建了一个名为 SuperList 的自定义列表,其中包含包装在 SuperListItem
我正在尝试使用 ProtoMember 中的 AsReference 选项进行递归引用。如果我使用公共(public)构造函数创建 AnOwner 然后序列化/反序列化,AnOwner.Data 变为
我是一名优秀的程序员,十分优秀!