Пример #1
0
        } // end sub

        #endregion


        /// <summary>
        /// <para xml:lang="en">Dump properties of a object to a JSON file.</para>
        /// <para xml:lang="ja">オブジェクトのプロパティをJSONファイルにダンプします</para>
        /// </summary>
        /// <typeparam name="T">
        /// <para xml:lang="en">The type of object to dump.</para>
        /// <para xml:lang="ja">ダンプするオブジェクトの型</para>
        /// </typeparam>
        /// <param name="target">
        /// <para xml:lang="en">The object to dump.</para>
        /// <para xml:lang="ja">ダンプするオブジェクト</para>
        /// </param>
        /// <param name="name">
        /// <para xml:lang="en">The name which is used for a file.</para>
        /// <para xml:lang="ja">ファイルに使用される名前。</para>
        /// </param>
        /// <param name="format">
        /// <para xml:lang="en">
        /// The lambda expression which is used for choosing properties to dump.
        /// Set like <code>a=&gt;new {a.A, a.B}</code>  to dump A property and B property of the object.
        /// Set like <code>a=&gt;a</code>  to dump all properties of the object.
        /// </para>
        /// <para xml:lang="ja">
        /// ダンプするプロパティを選択するラムダ式。
        /// オブジェクトのAプロパティとBプロパティをダンプする場合には、<code>a=&gt;new {a.A, a.B}</code> のように指定します。
        /// 全てのプロパティをダンプする場合には<code>a=&gt;a</code>を指定します。
        /// </para>
        /// </param>
        public static void DumpJson <T>(T target, string name, Func <T, object> format)
        {
            Dumper.WriteTextFile(name, ".json", writer =>
            {
                writer.Write(format(target).ToJson());
            });
        } // end sub
Пример #2
0
        }     // end sub

        /// <summary>
        /// <para xml:lang="en">Output the names of properties of a object as header of a TSV file.</para>
        /// <para xml:lang="ja">TSVファイルヘッダとして、オブジェクトのプロパティの名前を出力します。</para>
        /// </summary>
        /// <typeparam name="T">
        /// <para xml:lang="en">The type of object to dump.</para>
        /// <para xml:lang="ja">ダンプするオブジェクトの型</para>
        /// </typeparam>
        /// <param name="target">
        /// <para xml:lang="en">
        /// The object whose properties names are read. However, those properties values are not outputted.
        /// Use DumpTsvRecord method, DumpTsv method or other methods to output values.
        /// </para>
        /// <para xml:lang="ja">
        /// プロパティの名前を読み取るオブジェクト。ただしプロパティの値は出力されません。
        /// 値を出力するには DumpTsvRecord メソッドやDumpTsvメソッドなどを使用します。
        /// </para>
        /// </param>
        /// <param name="name">
        /// <para xml:lang="en">The name which is used for a file.</para>
        /// <para xml:lang="ja">ファイルに使用される名前。</para>
        /// </param>
        /// <param name="format">
        /// <para xml:lang="en">
        /// The lambda expression which is used for choosing properties to dump.
        /// Set like <code>a=&gt;new {a.A, a.B}</code>  to dump A property and B property of the object.
        /// Set like <code>a=&gt;a</code>  to dump all properties of the object.
        /// </para>
        /// <para xml:lang="ja">
        /// ダンプするプロパティを選択するラムダ式。
        /// オブジェクトのAプロパティとBプロパティをダンプする場合には、<code>a=&gt;new {a.A, a.B}</code> のように指定します。
        /// 全てのプロパティをダンプする場合には<code>a=&gt;a</code>を指定します。
        /// </para>
        /// </param>
        public static void DumpTsvHeader <T>(T target, string name, Func <T, object> format)
        {
            Dumper.WriteTextFile(name, ".tsv", writer =>
            {
                var configuration = new CsvHelper.Configuration.Configuration {
                    Delimiter = "\t"
                };
                using (var tsv = new CsvHelper.CsvWriter(writer, configuration))
                {
                    var v = format(target);
                    tsv.WriteHeader(v.GetType());
                    tsv.NextRecord();
                } // end using (tsv)
            });
        }         // end sub
Пример #3
0
        } // end sub

        /// <summary>
        /// <para xml:lang="en">Dump properties of a object to a TSV file. Output multiple objects on each rows of the TSV file.</para>
        /// <para xml:lang="ja">オブジェクトのプロパティをTSVファイルにダンプします。複数のオブジェクトをTSVファイルの各行に出力します。</para>
        /// </summary>
        /// <typeparam name="T">
        /// <para xml:lang="en">The type of object to dump.</para>
        /// <para xml:lang="ja">ダンプするオブジェクトの型</para>
        /// </typeparam>
        /// <param name="target">
        /// <para xml:lang="en">The collection of objects to dump.</para>
        /// <para xml:lang="ja">ダンプするオブジェクトのコレクション</para>
        /// </param>
        /// <param name="name">
        /// <para xml:lang="en">The name which is used for a file.</para>
        /// <para xml:lang="ja">ファイルに使用される名前。</para>
        /// </param>
        /// <param name="format">
        /// <para xml:lang="en">
        /// The lambda expression which is used for choosing properties to dump.
        /// Set like <code>a=&gt;new {a.A, a.B}</code>  to dump A property and B property of the object.
        /// Set like <code>a=&gt;a</code>  to dump all properties of the object.
        /// </para>
        /// <para xml:lang="ja">
        /// ダンプするプロパティを選択するラムダ式。
        /// オブジェクトのAプロパティとBプロパティをダンプする場合には、<code>a=&gt;new {a.A, a.B}</code> のように指定します。
        /// 全てのプロパティをダンプする場合には<code>a=&gt;a</code>を指定します。
        /// </para>
        /// </param>
        /// <param name="writeHeader">
        /// <para xml:lang="en">
        /// Set true to output a header of a TSV file. The names of properties of the object are output.
        /// Set false and so a header is not outputted. Use the DumpTsvHeader method or the WriteTsvHeader method to output the header, separately.
        /// </para>
        /// <para xml:lang="ja">
        /// TSVファイルの1行目にヘッダを出力する場合はtrueを指定します。オブジェクトのプロパティ名が出力されます。
        /// falseを指定するとヘッダが出力されません。別途、DumpTsvHeaderメソッドやWriteTsvHeaderメソッドを使用してヘッダを出力してください。
        /// </para>
        /// </param>
        public static void DumpTsv <T>(IEnumerable <T> target, string name, Func <T, object> format, bool writeHeader = true)
        {
            Dumper.WriteTextFile(name, ".tsv", writer =>
            {
                var configuration = new CsvHelper.Configuration.Configuration {
                    Delimiter = "\t"
                };
                using (var tsv = new CsvHelper.CsvWriter(writer, configuration))
                {
                    var isFirst = true;
                    foreach (var item in target)
                    {
                        var v = format(item);
                        if (isFirst)
                        {
                            isFirst = false;
                            if (writeHeader)
                            {
                                if (typeof(T) == typeof(string))
                                {
                                    tsv.WriteField("string");
                                }
                                else
                                {
                                    tsv.WriteHeader(v.GetType());
                                } // end if
                                tsv.NextRecord();
                            }
                        } // end if

                        if (typeof(T) == typeof(string))
                        {
                            tsv.WriteField(v);
                        }
                        else
                        {
                            tsv.WriteRecord(v);
                        } // end if
                        tsv.NextRecord();
                    }     // next item
                }         // end using (tsv)
            });
        }                 // end sub
Пример #4
0
        } // end sub

        /// <summary>
        /// <para xml:lang="en">Output table format values  to a TSV file. Output multiple rows of multiple values on each rows of the TSV file.</para>
        /// <para xml:lang="ja">表形式の値をTSVファイルに出力します。複数の値の複数行をTSVファイルの各行に出力します。</para>
        /// </summary>
        /// <typeparam name="T">
        /// <para xml:lang="en">The type of object to dump.</para>
        /// <para xml:lang="ja">ダンプするオブジェクトの型</para>
        /// </typeparam>
        /// <param name="target">
        /// <para xml:lang="en">The collection of table format values to output.</para>
        /// <para xml:lang="ja">出力する表形式の値のコレクション。</para>
        /// </param>
        /// <param name="name">
        /// <para xml:lang="en">The name which is used for a file.</para>
        /// <para xml:lang="ja">ファイルに使用される名前。</para>
        /// </param>
        /// <param name="header">
        /// <para xml:lang="en">
        /// Set a list of names, to output a header of TSV file.
        /// Set null, and so a header is not outputted. Use the DumpTsvHeader method or the WriteTsvHeader method to output the header, separately.
        /// </para>
        /// <para xml:lang="ja">
        /// TSFファイルのヘッダを出力するときには、名前の一覧を指定します。
        /// nullを指定するとヘッダは出力されません。別途、DumpTsvHeaderメソッドやWriteTsvHeaderメソッドを使用してヘッダを出力してください。
        /// </para>
        /// </param>
        public static void WriteTsv <T>(IEnumerable <IEnumerable <T> > target, string name, IEnumerable <string> header = null)
        {
            Dumper.WriteTextFile(name, ".tsv", writer =>
            {
                var configuration = new CsvHelper.Configuration.Configuration {
                    Delimiter = "\t"
                };
                using (var tsv = new CsvHelper.CsvWriter(writer, configuration))
                {
                    if (header != null)
                    {
                        foreach (var c in header)
                        {
                            tsv.WriteField(c);
                        }
                        tsv.NextRecord();
                    } // end if

                    foreach (var item in target)
                    {
                        if (item.GetType() == typeof(string))
                        {
                            tsv.WriteField(item);
                        }
                        else
                        {
                            foreach (var f in item)
                            {
                                tsv.WriteField(f);
                            }
                        }

                        tsv.NextRecord();
                    } // next item
                }     // end using (tsv)
            });
        }             // end sub