이것은 아마도 긴 샷이지만, 내가 작업하고있는 프로그램에서 반복을 최소화하려고 노력 중이며 걸림돌을 겪었습니다. 아래의 ClearTextBoxes() 메서드에서 볼 수 있듯이 foreach 루프 내에서 간결하게 배치하려는 코드는 매우 반복적입니다. 원래는 foreach (object box in customBoxes)
루프가 없었습니다. 나는 다음 목록으로 이것을 시도했지만 아무 소용이 없다. 나는 이것이 단지 불가능한 것인지, 아니면 단순히 잘못하고 있는지 확실하지 않습니다. 당신이 줄 수있는 도움을 주시면 감사하겠습니다.이 코드 블록을 축소하려면 어떻게해야합니까?변수의 List <>가 가능합니까?
감사합니다.
List<object> customBoxes = new List<object>();
customBoxes.AddRange(new[] { "TextBox", "DateBox", "DigitBox", "PhoneBox", "WaterTextBox" });
public void ClearTextBoxes()
{
ChildControls ccChildren = new ChildControls();
foreach (object o in ccChildren.GetChildren(rvraDockPanel, 2))
{
foreach (object box in customBoxes)
{
if (o.GetType() == typeof(TextBox))
{
TextBox txt = (TextBox)o;
txt.Text = "";
}
if (o.GetType() == typeof(DigitBox))
{
DigitBox digit = (DigitBox)o;
digit.Text = "";
}
if (o.GetType() == typeof(PhoneBox))
{
PhoneBox phone = (PhoneBox)o;
phone.Text = "";
}
if (o.GetType() == typeof(DateBox))
{
DateBox date = (DateBox)o;
date.Text = "";
}
if (o.GetType() == typeof(WatermarkTextBox))
{
WatermarkTextBox water = (WatermarkTextBox)o;
water.Text = "";
}
}
}
}
각 컨트롤에서 상속 받고 ClearText() 메서드를 사용하여 인터페이스를 적용합니다. – James
두 번째 루프의 역할은 무엇입니까 (foreach (customBoxes의 객체 상자) )? – DeveloperX
이 질문을 볼 수 있습니다 : http://stackoverflow.com/questions/619767/net-reflection-set-object-property – GTG