如何将C#对象列表转换为协议缓冲区列表?

时间:2019-01-22 07:49:54

标签: c# .net-core protocol-buffers asp.net-core-2.1 google-protocol-buffer

我正在尝试获取我的userDTO对象的列表并将其转换为protobuf候选列表,但我不知道该怎么做

C#对象

        // Generated save button
        var saveUserBtn = document.createElement('button');
        saveUserBtn.type = 'submit';
        saveUserBtn.innerHTML = "Save";

        // Defining the element that will be created as a list item
        var user_item = document.createElement('form');

        // Creates an ID  and name attribute per list item
        user_item.setAttribute("id", user.name)
        user_item.setAttribute("name", user.barcode);
        user_item.setAttribute("action", users_url + '/' + user.id);
        user_item.setAttribute("method", "POST"); 

        // Displays id, title, ISBN of the users from the database
        user_item.appendChild(userID);
        user_item.appendChild(name);
        user_item.appendChild(barcode);
        user_item.appendChild(memberType);
        user_item.appendChild(saveUserBtn);
        user_item.appendChild(loan);
        user_item.appendChild(deleteUser);

        // Assigns attributes to p items within users items
        userID.setAttribute("id", user.id);
        userID.setAttribute("value", user.id);
        name.setAttribute("id", user.name);
        name.setAttribute("value", user.name);
        barcode.setAttribute("id", user.barcode);
        barcode.setAttribute("value", user.barcode);

        // Adding a generic name to these elements for potential identification and use
        userID.setAttribute("name", "userID");
        name.setAttribute("name", "name");
        barcode.setAttribute("name", "barcode");
        memberType.setAttribute("name", "memberType");
        loan.setAttribute("name", "loan");

        // Converting/communicating dynamically generated Onclick button to saveUserBtn
        saveUserBtn.onclick = function() {
            saveUserEdit();
        }; 

        // Make content user name editable     
        function saveUserEdit() {
            saveUserBtn.addEventListener('click', function(e){
                e.preventDefault();
                name.addEventListener('keypress', (e) => {
                    if (e.keyCode === 13) {
                        xhttp.open("PUT", users_url + '/' + user.id, true); 
                        e.preventDefault();
                        var editName = new FormData();
                        editName.append("name", document.getElementById('name')[0].value);
                        editName.append("barcode", document.getElementById('barcode')[0].value); 
                        xhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
                        xhttp.send();
                    }
                });     
            })    
        }
        user_list.appendChild(user_item);

ProtoBuf

public class UserDTO
  {
    public string UserNumber { get; set; }
    public string FirstName { get; set; }
    public string LastName  { get; set; }
    public string UserId { get; set; }
    public string EmailId { get; set; }
  }

列表功能(不完整)

 message Candidate {
     string userNumber = 1;
     string userId = 2;
     string firstName = 3;
     string lastName = 4;
     string emailId = 5;
    }

message CandidateList{
      repeated Candidate candidateList = 1;
}

1 个答案:

答案 0 :(得分:4)

我可能会误解这里的问题,所以请随时纠正我误解的任何地方。

如果我们使用protobuf-net(出于熟悉和方便的缘故,我只是在引用),那么以下方法将起作用:

  [ProtoContract]
  public class UserDTO
  {
    [ProtoMember(1)]
    public string UserNumber { get; set; }
    [ProtoMember(3)]
    public string FirstName { get; set; }
    [ProtoMember(4)]
    public string LastName  { get; set; }
    [ProtoMember(2)]
    public string UserId { get; set; }
    [ProtoMember(5)]
    public string EmailId { get; set; }
  }

然后假设您已经将数据保存在Stream中(如果使用byte[],则MemoryStream将起作用):

var userList = Serializer.Deserialize<List<UserDTO>>(source);

将为您提供List<UserDTO>,并(正确)假设每个元素都是字段repeated Candidate的{​​{1}}。如果您想更具体一点,可以通过代码生成来运行整个原型架构,以获取完整的架构-例如通过protobuf-net的protogen。点击“生成”,您将获得一个额外的1元素,该元素代表根对象(而不是隐式的)。然后,您将使用:

CandidateList

在两种情况下,使用var root = Serializer.Deserialize<CandidateList>(source); var userList = root.candidateLists; 而不是Serialize的相同方法都可以从输入中生成原始气泡数据。