消息没有重新更新
本文关键字:更新 新更新 消息 | 更新日期: 2023-09-27 18:09:37
我为"People"类设置了两种"Description",
- 具有Message:, Priority:和Tag: 的数据
没有这些数据
List<People> lstPeople = new List<People> { new People { Message = "[1y] Message: test messg1 Priority: top Tag: t1" }, new People { Message = "without Message, Priority and Tag desc" } }; var data = lstPeople;
我想提取"Message:"、"Priority:"answers"Tag:"之后的数据,并重新更新"People"类"Message"、"Priority"answers"Tag"的每个实体。
下面的代码更新"优先级"answers"标签",但不更新"消息"。原因是什么呢?
public class People
{
const string ALERT_DESC_MARKER = "Message:";
const string PRIORITY_MARKER = "Priority:";
const string TAG_MARKER = "Tag:";
private string _description;
private string _tag;
private string _priority;
public string Message
{
get
{
if (_description.Contains(ALERT_DESC_MARKER) && _description.Contains(PRIORITY_MARKER))
return _description ?? GetTextPart(_description, ALERT_DESC_MARKER, PRIORITY_MARKER).Trim();
else
return _description;
}
set
{
_description = value;
}
}
public string Priority
{
get
{
if (_description.Contains(PRIORITY_MARKER) && _description.Contains(TAG_MARKER))
return _priority = GetTextPart(_description, PRIORITY_MARKER, TAG_MARKER).Trim();
else
return _priority;
}
set
{
_priority = value;
}
}
public string Tag
{
get
{
if (_description.Contains(TAG_MARKER))
return _tag = GetTextPart(_description, TAG_MARKER, null).Trim();
else
return _tag;
}
set
{
_tag = value;
}
}
private string GetTextPart(string text, string before, string after)
{
string result = null;
int posBefore = text.IndexOf(before);
if (after != null)
{
int posAfter = text.IndexOf(after);
result = text.Remove(posAfter).Substring(posBefore + before.Length).TrimEnd();
}
else
result = text.Substring(posBefore + before.Length);
return result;
}
}
问题出在Message
的getter。如文档所述:
? ?操作符称为空合并操作符。如果操作数不为空,则返回左操作数;否则返回右操作数
所以在你的例子中,如果_description
不是null
,你总是返回整个string
。相反,如果满足此条件(_description != null),则需要返回GetTextPart
。这样的:
if (_description.Contains(ALERT_DESC_MARKER) && _description.Contains(PRIORITY_MARKER))
return _description != null ? GetTextPart(_description, ALERT_DESC_MARKER, PRIORITY_MARKER).Trim() : _description;
编辑:下面是您的测试示例:
List<People> lstPeople = new List<People>
{
new People { Message = "[1y] Message: test messg1 Priority: top Tag: t1" },
new People { Message = "without Message, Priority and Tag desc" }
};
var data = lstPeople;
foreach (var item in lstPeople)
{
Console.WriteLine(item.Message);
Console.WriteLine(item.Priority);
Console.WriteLine(item.Tag);
}
输出第一项:
测试messg1 最高t1
输出第二项:
不带Message、Priority和Tag desc
第二项Priority
和Tag
为空。
编辑2:
这是Message
getter。我用来测试的其余代码与您发布的代码相同。注释掉的部分是你的旧版本
public string Message
{
get
{
if (_description.Contains(ALERT_DESC_MARKER) && _description.Contains(PRIORITY_MARKER))
return _description != null ? GetTextPart(_description, ALERT_DESC_MARKER, PRIORITY_MARKER).Trim(): _description;
//if (_description.Contains(ALERT_DESC_MARKER) && _description.Contains(PRIORITY_MARKER))
// return _description ?? GetTextPart(_description, ALERT_DESC_MARKER, PRIORITY_MARKER).Trim();
else
return _description;
}
set
{
_description = value;
}
}