using System; using System.Collections.Generic; using System.Text; namespace Ips.Library.Entity { /// /// 时隙 /// public class Timeslot { /// /// 名称 /// public string Name { get; set; } /// /// 起始样点 /// public int Start { get; set; } /// /// 信号长度 /// public int Length { get; set; } public Timeslot() { } public Timeslot(string result) { FromString(result); } public void FromString(string result) { if (string.IsNullOrWhiteSpace(result)) return; var items = result.Split(','); if (items.Length == 3 && int.TryParse(items[1], out int start) && int.TryParse(items[2], out int length)) { Name = items[0]; Start = start; Length = length; } else { throw new ArgumentException(result, nameof(result)); } } public static Timeslot[] FromListString(string result, string itemsep = "\r\n") { if (string.IsNullOrWhiteSpace(result)) return Array.Empty(); var items = result.Split(itemsep.ToCharArray(), StringSplitOptions.RemoveEmptyEntries); Timeslot[] results = new Timeslot[items.Length]; for (int i = 0; i < items.Length; i++) { results[i] = new Timeslot(items[i]); } return results; } public override string ToString() { return $"{Name},{Start},{Length}"; } } public static class TimeslotExtensions { public static int[] ToTimeslotArray(this IEnumerable timeslots) { List results = new List(); foreach(var ts in timeslots) { results.Add(ts.Start); results.Add(ts.Length); } return results.ToArray(); } } }