출처 : http://stackoverflow.com/questions/9893028/c-sharp-foreach-property-in-object-is-there-a-simple-way-of-doing-this



클래스 등을 만들었을 때!


모든 속성값을 가져오고 싶을 때가 있다. 


이 때는 아래와 같은 식으로 속성 값을 가져올 수 있더라....



public override string ToString()

{

    foreach (var propertyInfo in this.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))

    {

        // do stuff here

        string name = propertyInfo.Name;

        string value = propertyInfo.GetValue(this).ToString();

    }

}











Posted by motolies
,