C# 如何使用C在协议缓冲区中使用名称空间#
我正在使用协议缓冲区,希望使用Protobuf net生成.cs文件 因此,我准备了test1.proto和test2.proto文件,如下所示C# 如何使用C在协议缓冲区中使用名称空间#,c#,protocol-buffers,C#,Protocol Buffers,我正在使用协议缓冲区,希望使用Protobuf net生成.cs文件 因此,我准备了test1.proto和test2.proto文件,如下所示 message Test1 { required string test = 1; } import "test1.proto"; message Test2 { required Test1 test = 1; } //------------------------------------------------------
message Test1 {
required string test = 1;
}
import "test1.proto";
message Test2 {
required Test1 test = 1;
}
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
// Generated from: test1.proto
namespace test1
{
[global::System.Serializable, global::ProtoBuf.ProtoContract(Name=@"Test1")]
public partial class Test1 : global::ProtoBuf.IExtensible
{
public Test1() {}
private string _test;
[global::ProtoBuf.ProtoMember(1, IsRequired = true, Name=@"test", DataFormat = global::ProtoBuf.DataFormat.Default)]
public string test
{
get { return _test; }
set { _test = value; }
}
private global::ProtoBuf.IExtension extensionObject;
global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)
{ return global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject, createIfMissing); }
}
}
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
// Generated from: test2.proto
// Note: requires additional types generated from: test1.proto
namespace test2
{
[global::System.Serializable, global::ProtoBuf.ProtoContract(Name=@"Test2")]
public partial class Test2 : global::ProtoBuf.IExtensible
{
public Test2() {}
private Test1 _test;
[global::ProtoBuf.ProtoMember(1, IsRequired = true, Name=@"test", DataFormat = global::ProtoBuf.DataFormat.Default)]
public Test1 test
{
get { return _test; }
set { _test = value; }
}
private global::ProtoBuf.IExtension extensionObject;
global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)
{ return global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject, createIfMissing); }
}
}
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
// Generated from: test1.proto
namespace test1
{
[global::System.Serializable, global::ProtoBuf.ProtoContract(Name=@"Test1")]
public partial class Test1 : global::ProtoBuf.IExtensible
{
public Test1() {}
private string _test;
[global::ProtoBuf.ProtoMember(1, IsRequired = true, Name=@"test", DataFormat = global::ProtoBuf.DataFormat.Default)]
public string test
{
get { return _test; }
set { _test = value; }
}
private global::ProtoBuf.IExtension extensionObject;
global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)
{ return global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject, createIfMissing); }
}
}
之后,生成test1和test2.cs文件,如下所示
message Test1 {
required string test = 1;
}
import "test1.proto";
message Test2 {
required Test1 test = 1;
}
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
// Generated from: test1.proto
namespace test1
{
[global::System.Serializable, global::ProtoBuf.ProtoContract(Name=@"Test1")]
public partial class Test1 : global::ProtoBuf.IExtensible
{
public Test1() {}
private string _test;
[global::ProtoBuf.ProtoMember(1, IsRequired = true, Name=@"test", DataFormat = global::ProtoBuf.DataFormat.Default)]
public string test
{
get { return _test; }
set { _test = value; }
}
private global::ProtoBuf.IExtension extensionObject;
global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)
{ return global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject, createIfMissing); }
}
}
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
// Generated from: test2.proto
// Note: requires additional types generated from: test1.proto
namespace test2
{
[global::System.Serializable, global::ProtoBuf.ProtoContract(Name=@"Test2")]
public partial class Test2 : global::ProtoBuf.IExtensible
{
public Test2() {}
private Test1 _test;
[global::ProtoBuf.ProtoMember(1, IsRequired = true, Name=@"test", DataFormat = global::ProtoBuf.DataFormat.Default)]
public Test1 test
{
get { return _test; }
set { _test = value; }
}
private global::ProtoBuf.IExtension extensionObject;
global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)
{ return global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject, createIfMissing); }
}
}
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
// Generated from: test1.proto
namespace test1
{
[global::System.Serializable, global::ProtoBuf.ProtoContract(Name=@"Test1")]
public partial class Test1 : global::ProtoBuf.IExtensible
{
public Test1() {}
private string _test;
[global::ProtoBuf.ProtoMember(1, IsRequired = true, Name=@"test", DataFormat = global::ProtoBuf.DataFormat.Default)]
public string test
{
get { return _test; }
set { _test = value; }
}
private global::ProtoBuf.IExtension extensionObject;
global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)
{ return global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject, createIfMissing); }
}
}
//------------------------------------------------------------------------------
//
//这段代码是由一个工具生成的。
//
//对此文件的更改可能会导致不正确的行为,如果
//重新生成代码。
//
//------------------------------------------------------------------------------
//生成自:test1.proto
名称空间test1
{
[global::System.Serializable,global::ProtoBuf.ProtoContract(Name=@“Test1”)]
公共部分类Test1:global::ProtoBuf.IExtensible
{
公共Test1(){}
私有字符串测试;
[global::ProtoBuf.ProtoMember(1,IsRequired=true,Name=@“test”,DataFormat=global::ProtoBuf.DataFormat.Default)]
公共字符串测试
{
获取{return\u test;}
设置{u test=value;}
}
私有全局::ProtoBuf.IExtension扩展对象;
全局::ProtoBuf.IExtension全局::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)
{return global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject,createIfMissing);}
}
}
//------------------------------------------------------------------------------
//
//这段代码是由一个工具生成的。
//
//对此文件的更改可能会导致不正确的行为,如果
//重新生成代码。
//
//------------------------------------------------------------------------------
//生成自:test2.proto
//注意:需要从以下生成的其他类型:test1.proto
名称空间test2
{
[global::System.Serializable,global::ProtoBuf.ProtoContract(Name=@“Test2”)]
公共部分类Test2:global::ProtoBuf.IExtensible
{
公共Test2(){}
私人测试1_测试;
[global::ProtoBuf.ProtoMember(1,IsRequired=true,Name=@“test”,DataFormat=global::ProtoBuf.DataFormat.Default)]
公共测试1测试
{
获取{return\u test;}
设置{u test=value;}
}
私有全局::ProtoBuf.IExtension扩展对象;
全局::ProtoBuf.IExtension全局::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)
{return global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject,createIfMissing);}
}
}
但是,没有“usingtest1”语句。
如果我想添加using语句,手动添加using语句是唯一的方法吗
请教我,谢谢
谢谢你,马克先生。 我使用Protobuf网络。 test1.cs代码如下所示
message Test1 {
required string test = 1;
}
import "test1.proto";
message Test2 {
required Test1 test = 1;
}
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
// Generated from: test1.proto
namespace test1
{
[global::System.Serializable, global::ProtoBuf.ProtoContract(Name=@"Test1")]
public partial class Test1 : global::ProtoBuf.IExtensible
{
public Test1() {}
private string _test;
[global::ProtoBuf.ProtoMember(1, IsRequired = true, Name=@"test", DataFormat = global::ProtoBuf.DataFormat.Default)]
public string test
{
get { return _test; }
set { _test = value; }
}
private global::ProtoBuf.IExtension extensionObject;
global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)
{ return global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject, createIfMissing); }
}
}
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
// Generated from: test2.proto
// Note: requires additional types generated from: test1.proto
namespace test2
{
[global::System.Serializable, global::ProtoBuf.ProtoContract(Name=@"Test2")]
public partial class Test2 : global::ProtoBuf.IExtensible
{
public Test2() {}
private Test1 _test;
[global::ProtoBuf.ProtoMember(1, IsRequired = true, Name=@"test", DataFormat = global::ProtoBuf.DataFormat.Default)]
public Test1 test
{
get { return _test; }
set { _test = value; }
}
private global::ProtoBuf.IExtension extensionObject;
global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)
{ return global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject, createIfMissing); }
}
}
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
// Generated from: test1.proto
namespace test1
{
[global::System.Serializable, global::ProtoBuf.ProtoContract(Name=@"Test1")]
public partial class Test1 : global::ProtoBuf.IExtensible
{
public Test1() {}
private string _test;
[global::ProtoBuf.ProtoMember(1, IsRequired = true, Name=@"test", DataFormat = global::ProtoBuf.DataFormat.Default)]
public string test
{
get { return _test; }
set { _test = value; }
}
private global::ProtoBuf.IExtension extensionObject;
global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)
{ return global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject, createIfMissing); }
}
}
//------------------------------------------------------------------------------
//
//这段代码是由一个工具生成的。
//
//对此文件的更改可能会导致不正确的行为,如果
//重新生成代码。
//
//------------------------------------------------------------------------------
//生成自:test1.proto
名称空间test1
{
[global::System.Serializable,global::ProtoBuf.ProtoContract(Name=@“Test1”)]
公共部分类Test1:global::ProtoBuf.IExtensible
{
公共Test1(){}
私有字符串测试;
[global::ProtoBuf.ProtoMember(1,IsRequired=true,Name=@“test”,DataFormat=global::ProtoBuf.DataFormat.Default)]
公共字符串测试
{
获取{return\u test;}
设置{u test=value;}
}
私有全局::ProtoBuf.IExtension扩展对象;
全局::ProtoBuf.IExtension全局::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)
{return global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject,createIfMissing);}
}
}
您正在使用哪些工具?Protobuf网络?Protobuf csharp端口?Test1在哪个命名空间中生成?注意:您的意思是“使用指令”,而不是“使用语句”