예제 #1
0
        private static bool IsSequenceEqual(string[,] a1, string[,] a2)
        {
            Func <object, object, bool> Equals = (x, y) =>
            {
                if (x == null && y == null)
                {
                    return(true);
                }
                if (x == null || y == null)
                {
                    return(false);
                }
                return(x.Equals(y));
            };

            if (a1.Length != a2.Length)
            {
                return(false);
            }

            var enumerator = a1.GetEnumerator();

            foreach (var s0 in a2)
            {
                enumerator.MoveNext();
                if (!Equals(s0, enumerator.Current))
                {
                    return(false);
                }
            }

            return(true);
        }
예제 #2
0
        /// <summary>
        /// Transform 2-dimensional string array into enumerable sequence of classes.
        /// </summary>
        /// <typeparam name="T">Class type to transform into. Properties must be read/writable. Properties may be nullable types.</typeparam>
        /// <param name="array">2-Dimensional array to parse</param>
        /// <param name="hasHeader">
        ///   True if first row is a header. Header names must match class property names. The order is not important. Mismatched columns are ignored.
        ///   False if there is no header. Number of class Properties must exactly match number of columns in 2-D array.
        /// </param>
        /// <returns>Enumerable sequence of class T</returns>
        public static IEnumerable <T> ToModels <T>(this string[,] array, bool hasHeader) where T : class, new()
        {
            var properties = GetProperties(typeof(T));

            T           obj        = null;
            int         c          = 0;
            IEnumerator enumerator = array.GetEnumerator();

            if (hasHeader)
            {
                var list = new List <string>(properties.Count);
                while (c++ < array.GetLength(1) && enumerator.MoveNext())
                {
                    list.Add((string)enumerator.Current);
                }

                properties = SyncWithHeaders(properties, list);
                c          = 0;
            }
            else
            {
                // No header, so we assume a 1-to-1 match of properties with 2-D array columns.
                if (properties.Count != array.GetLength(1))
                {
                    throw new DataMisalignedException($"Number of valid properties ({properties.Count}) in {typeof(T).Name} does not match the number of columns ({array.GetLength(1)}) in 2-D array.");
                }
            }

            // Finally, populate the array of T with data from 2-D array;

            while (enumerator.MoveNext())
            {
                if (c == 0)
                {
                    obj = new T();
                }

                var s = (string)enumerator.Current;
                var p = properties[c];
                if (!string.IsNullOrEmpty(s))
                {
                    p.SetValue(obj, s);
                }

                c = ++c % properties.Count;

                if (c == 0)
                {
                    yield return(obj);
                }
            }

            yield break;
        }
예제 #3
0
        private static void WriteStrDataToFile(string filePath, string[,] data)
        {
            string strData = FileUtil.BuildStringData(data.GetEnumerator(), data.GetLength(0), data.GetLength(1), StrDelims);

            FileUtil.WriteStrToFile(filePath, strData, Encode);
        }