簡體   English   中英

將字符串分配給字符串類型C#的屬性

[英]assigning string to a property of string type C#

我可以通過代碼更好地解釋我的問題
這里是

strind abc="12345678<9";
row1ViewModel data = new row1ViewModel();
data.identityType = abc[0].ToString();
data.passportType = abc[1].ToString();
data.issuingOrg = abc.Substring(2, 3);
var  actual = "";
data.lastName = actual;
//data.lastName = actual;
if (abc[5] == '<')
{
    actual = "Not specified";
}
else
{
    string tempq = abc.Substring(5);
    int index = tempq.IndexOf('<');
    actual = abc.Substring(5, index);
}
//data.GetType().GetProperty(data.lastName).GetValue(actual,null)

在這里,我需要將屬性(字符串類型的data.lastname)設置為字符串實際值。 但是如何?

你需要移動你的

 data.lastName = actual;

到您的else循環之后,如果即時通訊讀取了正確的邏輯。

您首先分配了空字符串屬性:

var  actual = "";
     data.lastName = actual; 

然后更新實際值,但該屬性仍然具有“”作為其值,您應該移動

data.lastName = actual; 

到代碼末尾:

         strind abc="12345678<9";
     row1ViewModel data = new row1ViewModel();
   data.identityType = abc[0].ToString();
      data.passportType = abc[1].ToString();
      data.issuingOrg = abc.Substring(2, 3);
      var  actual = "";          
     //data.lastName = actual;
      if (abc[5] == '<')
      {
          actual = "Not specified"; 
     } 
     else 
     { 
         string tempq = abc.Substring(5);
          int index = tempq.IndexOf('<'); 
         actual = abc.Substring(5, index); 
       } 
     //data.GetType().GetProperty(data.lastName).GetValue(actual,null)`  
     data.lastName = actual; 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM