【发布时间】:2020-10-23 08:58:58
【问题描述】:
我有一个如下的原型文件:
syntax = "proto3";
package _abbbd3832ad747a0a1b0cd1b3c6d6579;
enum Allocator {
// This needs to be here as a zero value.
DO_NOT_REMOVE = 0;
VALUE = 1;
};
enum StandardEventCodes {
// Signal: N/A
POWER_UP = 0;
// Signal: N/A
WAKE_UP = 1;
// Signal: N/A
POWER_OR_WAKE_UP = 2;
};
enum StandardFilterCodes {
// Signal: The current profile.
IS_CURRENT_PROFILE = 0;
// Signal: The restricted mode
// 0 - unrestricted
// 1 - restricted
// 2 - transport
// 3 - invalid
IS_RESTRICTED_MODE = 1;
};
enum StandardActionCodes {
// Signal: The profile to apply.
APPLY_PROFILE = 0;
};
message Config {
message Profile {
enum State {
ACTIVE = 0;
SLEEP = 1;
HIBERNATE = 2;
};
// The profile's state.
State state = 1;
// What will wake the device up (input, adc_threshold, cell, etc).
uint32 wakeup_mask = 2;
// How long to wait before applying this profile in seconds.
uint32 delay_s = 3;
};
repeated Profile profiles = 1;
};
我已经设法从 protoc 编译器生成了一个 cs。但是,是否可以通用生成一个json文件。
var instances = from t in Assembly.GetExecutingAssembly().GetTypes()
where t.GetInterfaces().Contains(typeof(IMessage))
&& t.GetConstructor(Type.EmptyTypes) != null
select Activator.CreateInstance(t) as IMessage;
foreach (var instance in instances)
{
string json = instance.ToString(); // doesnot work
Console.WriteLine(instance.GetType().GetNestedTypes().ToString()); // doesnot work
}
请提出建议。
【问题讨论】:
标签: c# json protocol-buffers proto