-
Notifications
You must be signed in to change notification settings - Fork 0
Introduction
jyuch edited this page Dec 12, 2015
·
10 revisions
プロジェクトごとソリューションに加えるか、ダウンロードもしくはビルドしたDLLをプロジェクトの参照設定に加えてください。
現時点ではNuGetに対応してませんが、将来的にはNuGetに対応させるつもりです。
文字列形式に表すクラスとして、今回は以下のクラスを想定します。
class Hoge
{
public int MyProperty { get; set; }
public string MyProperty2 { get; set; }
public string MyField1;
}そのインスタンスのプロパティを含んだ文字列形式を得るだけならば、以下のコードで十分です。 フィールドを含む場合については後述します。
Hoge hoge = new Hoge() { MyProperty = 1, MyProperty2 = "Hoge" };
hoge.MyField1 = "HogeHoge";
string str = ToStringBuilder.ToString(hoge);
Console.WriteLine(str);Hoge{MyProperty=1,MyProperty2=Hoge}
フィールドを文字列形式に含める場合はToStringConfig<T>.OutputTargetにFieldもしくはBothを指定します。
Hoge hoge = new Hoge() { MyProperty = 1, MyProperty2 = "Hoge" };
hoge.MyField1 = "HogeHoge";
ToStringConfig<Hoge> conf = new ToStringConfig<Hoge>();
conf.OutputTarget = TargetType.Both;
string str = ToStringBuilder.ToString(hoge, conf);
Console.WriteLine(str);Hoge{MyProperty=1,MyProperty2=Hoge,MyField1=HogeHoge}
nullもしくはToStringの結果が空白のメンバーを無視する場合は、ToStringConfig<T>.IgnoreModeにNullもしくはNullOrWhiteSpaceを指定します。
Hoge hoge = new Hoge() { MyProperty = 1, MyProperty2 = null };
hoge.MyField1 = "HogeHoge";
ToStringConfig<Hoge> conf = new ToStringConfig<Hoge>();
conf.IgnoreMode = IgnoreMemberMode.Null;
string str = ToStringBuilder.ToString(hoge, conf);
Console.WriteLine(str);Hoge{MyProperty=1}
特定のメンバーを常に無視したい場合は、ToStringConfig<T>.SetIgnoreMemberメソッドで無視するメンバーを指定します。
Hoge hoge = new Hoge() { MyProperty = 1, MyProperty2 = "Hoge" };
hoge.MyField1 = "HogeHoge";
ToStringConfig<Hoge> conf = new ToStringConfig<Hoge>();
conf.SetIgnoreMember(it => it.MyProperty);
string str = ToStringBuilder.ToString(hoge, conf);
Console.WriteLine(str);Hoge{MyProperty2=Hoge}
任意のプロパティやフィールドを任意の順番で表示させたい場合、もしくはフィールド名を任意に変更したい場合はToStringMap<T>を使用します。
class HogeMap : ToStringMap<Hoge>
{
public HogeMap()
{
Map(it => it.MyProperty);
Map(it => it.MyProperty2).Ignore();
Map(it => it.MyField1).Name("Field1");
}
}Hoge hoge = new Hoge() { MyProperty = 1, MyProperty2 = "Hoge" };
hoge.MyField1 = "HogeHoge";
HogeMap map = new HogeMap();
string str = map.ToString(hoge);
Console.WriteLine(str);Hoge{MyProperty=1,Field1=HogeHoge}
文字列形式中にIEnumerableを展開したい場合は、ToStringConfig<T>.ExpandIEnumerableにtrueを指定します。
これはIEnumerable内の各要素に対してToStringを発行しているだけです。
class IncludingIEnumerable
{
public IEnumerable<DualPropertyClass2> Property1 { get; set; }
}class DualPropertyClass2 : DualPropertyClass
{
public override string ToString()
{
return ToStringBuilder.ToString(this);
}
}DualPropertyClass2 e1 = new DualPropertyClass2() { Property1 = "Property11", Property2 = "Property12" };
DualPropertyClass2 e2 = new DualPropertyClass2() { Property1 = "Property21", Property2 = "Property22" };
IncludingIEnumerable source = new IncludingIEnumerable()
{
Property1 = new DualPropertyClass2[] { e1, e2 }
};
ToStringConfig<IncludingIEnumerable> config = new ToStringConfig<IncludingIEnumerable>() { ExpandIEnumerable = true };
string str = ToStringBuilder.ToString(source, config);
Console.WriteLine(str);IncludingIEnumerable{Property1=[DualPropertyClass2{Property1=Property11,Property2=Property12},DualPropertyClass2{Property1=Property21,Property2=Property22}]}