0

I have the following class in my OperationResult.Cs

 public class OperationResult
    {
        public bool Result { get; set; }
        public string Message { get; set; }
        public string ErrorMessage { get; set; }
    }

    public class OperationResult<TResponse> : OperationResult
    {
        public OperationResult() { }
        public OperationResult(TResponse response)
        {
            Response = response;
        }

        public OperationResult(OperationResult result)
        {
            Result = result.Result;
            Message = result.Message;
            ErrorMessage = result.ErrorMessage;
        }
        public TResponse Response { get; set; }
    }

Now I want to convert this class to grpc file called operationResult.proto like this

message OperationResult{
    string Message = 1;
    string ErrorMessage = 2;
    bool Result = 3;
}

message OperationResult<Response>{
    string Message = 1;
    string ErrorMessage = 2;
    bool Result = 3;
    Response Response = 4;
}
message Response{
    Object //for example generic type 
}

I wanna use this file for every service in protobuf so i wanna pass the genric type like c# implementation

Adnan
  • 95
  • 1
  • 6
  • Write the required .proto code? – Robert Harvey Jun 16 '20 at 13:41
  • @RobertHarvey I updated it – Adnan Jun 16 '20 at 14:07
  • Is your question "How do I use protobuf with generic types?" https://stackoverflow.com/q/12305318 – Robert Harvey Jun 16 '20 at 14:33
  • question: what is your objective here? are you actually *after* the .proto file? or do you just want it to work with gRPC? If you just want it to work with gRPC: protobuf-net.Grpc doesn't require .proto at all - you can use regular .NET POCO types like above for the data, and regular .NET interfaces to represent the services – Marc Gravell Jun 16 '20 at 15:18
  • Basically If I have much services in .proto and I wanna handle the response for each function in the service by putting common response. rpc GetUser returns >. So everytime I will call the response message to handle all the functions in the services. This is my question. @MarcGravell – Adnan Jun 17 '20 at 03:05
  • @Adnan but .proto doesn't have generics, so if you're starting from . proto, the answer is "no, you can't do that - you need to use the generated types, which are not generic, because .proto is not generic". If you were starting from a "code first" approach (not .proto), it it probably entirely possible – Marc Gravell Jun 17 '20 at 06:18

0 Answers0