Created
October 10, 2018 07:10
-
-
Save kiichi54321/77fb0816b841a338221a11a9bb047d00 to your computer and use it in GitHub Desktop.
Testプロジェクト用、拡張メソッド
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
namespace MyLib.Test.Extend | |
{ | |
public static class TestExtend | |
{ | |
public static IEnumerable<T> ConsoleWriteLine<T>(this IEnumerable<T> list, Func<T, string> func) | |
{ | |
foreach (var item in list) | |
{ | |
System.Console.WriteLine(func(item)); | |
} | |
return list; | |
} | |
public static string ConsoleWriteLine(this string text) | |
{ | |
System.Console.WriteLine(text); | |
return text; | |
} | |
public static IEnumerable<T> ConsoleWriteLine<T>(this IEnumerable<T> list) | |
{ | |
foreach (var item in list) | |
{ | |
System.Console.WriteLine(item.ToString()); | |
} | |
return list; | |
} | |
public static IEnumerable<T> FileWriteLine<T>(this IEnumerable<T> list, string fileName, Func<T, string> func) | |
{ | |
using (var file = System.IO.File.CreateText(fileName)) | |
{ | |
foreach (var item in list) | |
{ | |
try | |
{ | |
file.Encoding.GetBytes(func(item)); | |
file.WriteLine(func(item)); | |
} | |
catch { } | |
} | |
} | |
return list; | |
} | |
public static string ToJson(this object obj, Newtonsoft.Json.Formatting formatting = Newtonsoft.Json.Formatting.Indented) | |
{ | |
return Newtonsoft.Json.JsonConvert.SerializeObject(obj, formatting); | |
} | |
public static void JsonSave(this object obj,string file) | |
{ | |
System.IO.File.WriteAllText(file, obj.ToJson()); | |
} | |
public static void ConsoleWriteJson(this object obj) | |
{ | |
System.Console.WriteLine(obj.ToJson()); | |
} | |
public static void ConsoleWriteTsv<T>(this IEnumerable<T> obj) | |
{ | |
System.Console.WriteLine(obj.ToTsv()); | |
} | |
public static string ToTsv<T>(this IEnumerable<T> list) | |
{ | |
System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(); | |
stringBuilder.AppendLine(string.Join("\t", typeof(T).GetProperties().Select(n => n.Name).ToArray())); | |
foreach (var item in list) | |
{ | |
stringBuilder.AppendLine(string.Join("\t", typeof(T).GetProperties().Select(n => n.GetValue(item)?.ToString()))); | |
} | |
return stringBuilder.ToString(); | |
} | |
public static void ConsoleWriteMarkDownTable<T>(this IEnumerable<T> list) | |
{ | |
System.Console.WriteLine(list.ToMarkDownTable()); | |
} | |
public static string ToMarkDownTable<T>(this IEnumerable<T> list) | |
{ | |
System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(); | |
stringBuilder.AppendLine(string.Join(" | ", typeof(T).GetProperties().Select(n => n.Name).ToArray())); | |
stringBuilder.AppendLine(string.Join(" | ", typeof(T).GetProperties().Select(n => "---------").ToArray())); | |
foreach (var item in list) | |
{ | |
stringBuilder.AppendLine(string.Join(" | ", typeof(T).GetProperties().Select(n => n.GetValue(item)?.ToString()))); | |
} | |
return stringBuilder.ToString(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
テストプロジェクト用拡張メソッド。
自分でもわけわからなくなっているので、ここで集中管理したい。