C# 在运行时创建原型模式?
一段时间以来,我一直在使用protobuf.net的C# 在运行时创建原型模式?,c#,reflection,protocol-buffers,protobuf-net,protobuf.js,C#,Reflection,Protocol Buffers,Protobuf Net,Protobuf.js,一段时间以来,我一直在使用protobuf.net的ieextensible功能(允许我创建运行时proto消息流)。不幸的是,似乎没有任何功能可以从可扩展类中提取proto模式。我需要这个功能,以便让protobuf.js更容易读取消息流 有没有办法为可扩展/动态类生成原型模式 这是我目前的解决方案;它确实有效,但不能直接作用于可扩展对象。我没能解决这个问题。因此,也许有更好的办法 创建类型(具有ProtoMember属性)的步骤 输出: message Foo { optional i
ieextensible
功能(允许我创建运行时proto消息流)。不幸的是,似乎没有任何功能可以从可扩展类中提取proto
模式。我需要这个功能,以便让protobuf.js更容易读取消息流
有没有办法为可扩展/动态类生成原型模式 这是我目前的解决方案;它确实有效,但不能直接作用于可扩展对象。我没能解决这个问题。因此,也许有更好的办法 创建类型(具有ProtoMember属性)的步骤 输出:
message Foo {
optional int32 A = 1 [default = 0];
optional int32 B = 2 [default = 0];
}
message Record {
optional int32 Integer = 1 [default = 0];
optional double Number = 2 [default = 0];
optional string String = 3;
optional Foo MyFoo = 4;
}
internal class Program
{
static void Main(string[] args)
{
var foo = ProtoTypeBuilder.CompileResultType("Foo", new[]
{
new ProtoField ("A", typeof (int)),
new ProtoField ("B", typeof (int)),
});
var record = ProtoTypeBuilder.CompileResultType("Record", new[]
{
new ProtoField ("Integer", typeof (int)),
new ProtoField ("Number", typeof (double)),
new ProtoField ("String", typeof (string)),
new ProtoField ("MyFoo", foo),
});
var proto = ProtoBuf.Meta.RuntimeTypeModel.Default.GetSchema(record);
Console.WriteLine(proto);
}
}
message Foo {
optional int32 A = 1 [default = 0];
optional int32 B = 2 [default = 0];
}
message Record {
optional int32 Integer = 1 [default = 0];
optional double Number = 2 [default = 0];
optional string String = 3;
optional Foo MyFoo = 4;
}