我尝试序列化一棵由 Craft 或 Reagent 组成的树:
[ProtoContract]
[ProtoInclude(1, typeof(Craft))]
[ProtoInclude(2, typeof(Reagent))]
public interface IReagent : IEquatable
{
[ProtoMember(1)]
int ItemId { get; set; }
[ProtoMember(2)]
int Quantity { get; set; }
}
[ProtoContract]
public class Reagent : IReagent
{
[ProtoMember(1)]
public int ItemId { get; set; }
[ProtoMember(2)]
public int Quantity { get; set; }
}
[ProtoContract]
public class Craft : IReagent
{
[ProtoMember(1)]
public int Skill { get; set; }
[ProtoMember(2)]
public Profession Profession { get; set; }
[ProtoMember(3)]
public …
Run Code Online (Sandbox Code Playgroud)Run Code Online (Sandbox Code Playgroud) 我收到错误
一旦生成序列化器,类型就无法更改
当尝试使用 Protobuff.net 进行序列化时。我已设法减少代码以找到罪魁祸首,但想知道为什么它无法序列化此属性。
我找到了一个可以使用的可行解决方案,但对解释此代码失败的原因感兴趣。
[ProtoContract]
public class SomeController
{
[ProtoMember(3)]
public int ControllerValue { get; set; }
[ProtoMember(4, AsReference = true)]
private ITest ITestObj { get; set; }
private SomeController(){}
public SomeController(object something, int value)
{
ControllerValue = value;
ITestObj = something as ITest;
}
}
Run Code Online (Sandbox Code Playgroud)
该错误是由 引起的SomeController.ITestObj。如果我将这个类更改为:
[ProtoContract]
public class SomeController
{
[ProtoMember(3)]
public int ControllerValue { get; set; }
[ProtoMember(4, AsReference = true)]
private TestObj OriginalObject { get; set; }
private ITest …Run Code Online (Sandbox Code Playgroud) 我正在开发一个自定义 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<>)
};
Run Code Online (Sandbox Code Playgroud) 我按照“hello world”grpc教程在.net core(3.0)中创建了grpc服务器和客户端项目。一切都进行得很好。当我完成将 .proto 文件添加到客户端项目的步骤时,我想知道是否有一种方法可以避免复制文件,而是添加服务器 .proto 文件作为客户端项目的链接。
从有关如何将 proto 文件添加到客户端的文档中:
- 在 gRPC 客户端项目中创建 Protos 文件夹。
- 将 Protos\greet.proto 文件从 gRPC Greeter 服务复制到 gRPC 客户端项目。
然后将复制的文件添加到客户端 .csproj,如下所示:
<ItemGroup>
<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)
是否可以将服务器项目中的 .proto 文件添加为客户端的链接?
我知道这两个库的功能和设计,但我没有找到这两者之间的任何直接性能比较.两者都绝对是很好的库.关于设计,我认为protobuf-csharp-port由于反射较少而应该稍快一点,对吧?
此外:
谢谢.
我使用RedisConnection Set方法设置字节数组但是如何获取数据?get返回一个包装的字节数组?
链接:
这有效,但感觉不对:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BookSleeve;
using ProtoBuf;
using System.IO;
namespace RedisTest001
{
[ProtoContract, Serializable]
public class Price
{
private string _ticker;
private double _value;
public Price()
{
}
public Price(string ticker, double value)
{
_ticker = ticker;
_value = value;
}
[ProtoMember(1)]
public string Ticker
{
get { return _ticker; }
set { _ticker = value; }
}
[ProtoMember(2)]
public double Value
{
get { return _value; }
set …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用.NET任务读取使用ProtoBuf.NET序列化的多个文件,如下所示:
public static ResultsDump Amalgamate(RuntimeTypeModel model, IEnumerable<string> files)
{
var readDumpTasks =
files.Select(fn =>
Task<ResultsDump>.Factory.StartNew(() => {
try {
using (var dumpFile = new FileStream(fn, FileMode.Open))
{
var miniDump = (ResultsDump)model.Deserialize(dumpFile, null, typeof(ResultsDump));
if (miniDump == null) {
throw new Exception(string.Format("Failed to deserialize dump file {0}", fn));
}
//readDumps.Add(miniDump);
return miniDump;
}
}
catch (Exception e) {
throw new Exception(string.Format("cannot read dump file {0}: {1}", fn, e.Message), e);
}
})).ToArray();
Task.WaitAll(readDumpTasks);
var allDumps = readDumpTasks.Select(t => t.Result).ToList();
// Goes on.. …Run Code Online (Sandbox Code Playgroud) .net c# parallel-processing protobuf-net task-parallel-library
我有一个我序列化的类,然后使用Protobuf-net版本r431(大概一年左右)反序列化.该类包含一个枚举_type和一个名为的字符串_band.在构造函数中,_type设置为StationType.Other并_band设置为空字符串.
如您所见,我创建了一个包含数据的对象,序列化,然后反序列化.枚举_type(StationType.Streaming)的值丢失,而_band(FM)保留.
我觉得这是一个错误,因为行为不一致.但是,如果我从值1而不是0开始枚举,则一切都按预期工作(例如,保留所有值).
我在这里错过了什么吗?请参阅下面的代码和输出:
using System;
using System.IO;
using ProtoBuf;
namespace ProtoBufsWithEnums
{
class Program
{
static void Main(string[] args)
{
stn1 = new Station{Type = StationType.Streaming, Band = "FM"};
var ms1 = new MemoryStream();
Serializer.Serialize(ms1, stn1); // serialize
byte[] bytes = ms1.ToArray();
var ms2 = new MemoryStream(bytes);
Station stn2 = Serializer.Deserialize<Station>(ms2); // deserialize
Console.WriteLine("Type - Original {0}, New {1}", stn1.Type, stn2.Type);
Console.WriteLine("Band - Original {0}, New {1}", …Run Code Online (Sandbox Code Playgroud) 是否可以在Windows Phone 7/8上使用protobuf-net序列化/反序列化类型?
我已经尝试了下面的代码,似乎不支持Constructor跳过(即UseConstructor = false)所以我创建了一个无参数构造函数,但反序列化失败,"尝试访问该方法失败:Wp7Tests.ImmutablePoint.set_X(System.Double) )"
public class ImmutablePoint
{
public double X { get; private set; }
public double Y { get; private set; }
public ImmutablePoint() {}
public ImmutablePoint(double x, double y)
{
X = x;
Y = y;
}
}
public sub Test()
{
ImmutablePoint pt = new ImmutablePoint(1, 2);
var model = TypeModel.Create();
var ptType = model.Add(typeof(ImmutablePoint), false);
ptType.AddField(1, "X");
ptType.AddField(2, "Y");
IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
using (var stream1 = new IsolatedStorageFileStream("test.bin", FileMode.Create, store))
{ …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用gRPC在工作中构建PoC。此处的google文档为我们提供了一个示例应用程序。我想知道protobuf-net(尤其是protogen)是否有能力理解执行gRPC调用所需的服务定义和类?还是正在处理某些问题?如果我将google的protoc用于客户端和服务器代码生成(涉及服务定义和RPC调用),并将protobuf-net用于我的业务对象,是否可以使用?
protobuf-net ×10
c# ×5
.net ×1
.net-core ×1
booksleeve ×1
grpc ×1
immutability ×1
protogen ×1
redis ×1