消息未重新更新

时间:2016-09-29 06:33:23

标签: c#

我有2种"描述"对于班级"人们",

  1. 拥有Message:,Priority:和Tag:
  2. 的数据
  3. 拥有没有这些的数据

    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;
    
  4. 我想在&#34;消息之后提取数据:&#34;,&#34;优先级:&#34;和&#34;标签:&#34;并重新更新&#34; People&#34;的每个实体。 class&#34; Message&#34;,&#34; Priority&#34;和&#34;标记&#34;。

    以下代码更新了&#34;优先级&#34;和#34;标记&#34;但不是&#34;消息&#34;。可能是什么原因?

        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;
        }
    }
    

1 个答案:

答案 0 :(得分:2)

问题在于Message的吸气者。 正如documentation所说:

  

?? operator被称为null-coalescing运算符。如果操作数不为null,则返回左侧操作数;否则它返回右手操作数。

因此,如果string不是_description,那么您总是会返回整个null。相反,如果满足此条件(_decription!= 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

输出第二项:

  

没有消息,优先级和标记desc

第二项Priority中的

Tag为空。

编辑2:

这是Message吸气剂。我以前测试的其余代码与您发布的代码相同。注释掉的部分是您的旧/已发布版本

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;
    }
}