本类继承了IEnumerable接口这后,支持了简单的遍历,方法将内部的消息输出,提供了两种索引器对它进行读取,数值为索引的和字符为索引的.
namespace Entity.Commons
{
/// <summary>
/// 通用消息序列类
/// </summary>
public class VMessage : IEnumerable<string>
{
/// <summary>
/// 是否完成
/// </summary>
public bool IsComplete { get; set; }
/// <summary>
/// 消息序列
/// </summary>
List<string> list = new List<string>();
Dictionary<string, string> dic = new Dictionary<string, string>();
/// <summary>
/// 向序列中添加新的项
/// </summary>
/// <param name="item"></param>
public void AddItem(string item)
{
this.list.Add(item);
}
/// <summary>
/// 向序列中添加新的项
/// </summary>
/// <param name="index">数值索引</param>
/// <param name="item"></param>
public void AddKeyValueItem(string index, string item)
{
this.dic.Add(index, item);
}
/// <summary>
/// 键值消息键是否存在
/// </summary>
/// <param name="index">键</param>
/// <returns>是否存在</returns>
public bool ContainsKey(string index)
{
return this.dic.ContainsKey(index);
}
/// <summary>
/// 向序列中追加一个新的序列
/// </summary>
/// <param name="itemRange">序列对象</param>
public void AddItemRange(IEnumerable<string> itemRange)
{
this.list.AddRange(itemRange);
}
/// <summary>
/// 消息对象
/// </summary>
public Object Object { get; set; }
/// <summary>
/// 清空所有现有项
/// </summary>
public void Clear()
{
this.list.Clear();
}
/// <summary>
/// 索引器
/// </summary>
/// <param name="index">索引</param>
/// <returns>相应值</returns>
public string this[int index]
{
get { return this.list[index]; }
}
/// <summary>
/// 索引器
/// </summary>
/// <param name="index">索引</param>
/// <returns>相应值</returns>
public string this[string index]
{
get
{
return this.dic[index];
}
}
#region IEnumerable<string> 成员
public IEnumerator<string> GetEnumerator()
{
return this.list.GetEnumerator();
}
#endregion
#region IEnumerable 成员
IEnumerator IEnumerable.GetEnumerator()
{
return this.list.GetEnumerator();
}
#endregion
}
}
作者:仓储大叔,张占岭,
荣誉:微软MVP