クラスのプロパティ、フィールド情報を取得する:リフレクションを使用する
/// <summary>
/// object型の拡張メソッドを管理するクラス
/// </summary>
public static class ObjectExtensions
{
private const string SEPARATOR = "\n"; // 区切り記号として使用する文字列
private const string FORMAT = "{0}:{1}"; // 複合書式指定文字列
/// <summary>
/// すべての公開フィールドの情報を文字列にして返します
/// </summary>
public static string ToStringFields<T>( this T obj, string separator = null )
{
if (separator == null)
separator = SEPARATOR;
return string.Join(
separator,
obj
.GetType()
.GetFields( BindingFlags.Instance | BindingFlags.Public )
.Select( c => string.Format( FORMAT, c.Name, c.GetValue( obj ) ) ) );
}
/// <summary>
/// すべての公開フィールドの変数名を文字列にして返します
/// </summary>
public static string ToStringFieldNames<T>( this T obj, string separator = null )
{
if (separator == null)
separator = SEPARATOR;
return string.Join(
separator,
obj
.GetType()
.GetFields( BindingFlags.Instance | BindingFlags.Public )
.Select( c => "{" + c.Name + "}" ) );
}
/// <summary>
/// すべての公開フィールドの値を文字列にして返します
/// </summary>
public static string ToStringFieldValues<T>( this T obj, string separator = null )
{
if (separator == null)
separator = SEPARATOR;
return string.Join(
separator,
obj
.GetType()
.GetFields( BindingFlags.Instance | BindingFlags.Public )
.Select( c => "{" + c.GetValue( obj ) + "}" ) );
}
/// <summary>
/// すべての公開プロパティの情報を文字列にして返します
/// </summary>
public static string ToStringProperties<T>( this T obj, string separator = null )
{
if (separator == null)
separator = SEPARATOR;
return string.Join(
separator,
obj
.GetType()
.GetProperties( BindingFlags.Instance | BindingFlags.Public )
.Where( c => c.CanRead )
.Select( c => string.Format( FORMAT, c.Name, c.GetValue( obj, null ) ) ) );
}
/// <summary>
/// すべての公開プロパティの変数名を文字列にして返します
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <param name="separator"></param>
/// <returns></returns>
public static string ToStringPropertieNames<T>( this T obj, string separator = null )
{
if (separator == null)
separator = SEPARATOR;
return string.Join(
separator,
obj
.GetType()
.GetProperties( BindingFlags.Instance | BindingFlags.Public )
.Where( c => c.CanRead )
.Select( c => "{" + c.Name + "}" ) );
}
/// <summary>
/// すべての公開プロパティの値を文字列にして返します
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <param name="separator"></param>
/// <returns></returns>
public static string ToStringPropertieValues<T>( this T obj, string separator = null )
{
if (separator == null)
separator = SEPARATOR;
return string.Join(
separator,
obj
.GetType()
.GetProperties( BindingFlags.Instance | BindingFlags.Public )
.Where( c => c.CanRead )
.Select( c => "{" + c.GetValue( obj, null ) + "}" ) );
}
/// <summary>
/// すべての公開フィールドと公開プロパティの情報を文字列にして返します
/// </summary>
public static string ToStringReflection<T>( this T obj, string separator = null )
{
if (separator == null)
separator = SEPARATOR;
return string.Join(
separator,
obj.ToStringFields(),
obj.ToStringProperties() );
}
/// <summary>
/// すべての公開フィールドと公開プロパティの変数名を文字列にして返します
/// </summary>
public static string ToStringReflectionHeaders<T>( this T obj, string separator = null )
{
if (separator == null)
separator = SEPARATOR;
return string.Join(
separator,
obj.ToStringFieldNames(),
obj.ToStringPropertieNames() );
}
/// <summary>
/// すべての公開フィールドと公開プロパティの値を文字列にして返します
/// </summary>
public static string ToStringReflectionValues<T>( this T obj, string separator = null )
{
if (separator == null)
separator = SEPARATOR;
return string.Join(
separator,
obj.ToStringFieldValues(),
obj.ToStringPropertieValues() );
}
}