반사를 이용하여 하나의 클래스에서 속성의 대량 할당 조작을 완성하다

3820 단어 .Net
메서드 1, 객체 등록 정보가 순차적으로 지정되고 객체 멤버가 변경되면 메서드를 수정합니다.
bs.add=true;
bs.edit=true;
bs.list=true;
...
return bs;

방법2, 반사를 이용하여 대량으로 값을 부여한다.
ButtonState bs = new ButtonState();//
Type t = typeof(ButtonState);
PropertyInfo[] myPropertyInfo;
myPropertyInfo = t.GetProperties();
for (int i = 0; i < myPropertyInfo.Length; i++)
{
   myPropertyInfo[i].SetValue(bs, true, null);
}
return bs;

객체:/// /// /// public class ButtonState { /// /// /// public bool add { get; set; } /// /// /// public bool edit { get; set; } /// /// /// public bool list { get; set; } /// /// /// public bool serch { get; set; } /// /// /// public bool view { get; set; } }

좋은 웹페이지 즐겨찾기