System.Reflection.propertyInfo.SetValue () 버튼의 기본 이벤트 처리기 호출 [폐쇄

StackOverflow https://stackoverflow.com/questions/309706

  •  08-07-2019
  •  | 
  •  

문제

그래서 나는 이것이 왜 일어나고 있는지 잘 모르겠지만, 나는 내가 설정하고 싶은 제어 이름, 속성 및 가치가있는 일부 DataRows를 통과하고 있습니다. 버튼의 텍스트 속성을 설정할 때를 제외하고는 모든 것이 잘 작동합니다. 어떤 이유로 든 클릭 이벤트는 ...

여기에 내가 가진 코드 중 일부가 있습니다.

string controlName, value, property;
Control currentControl = null;
System.Reflection.PropertyInfo propertyInfo = null;

// run through all rows in the table and set the property
foreach (DataRow r in languageDataset.Tables[_parentForm.Name].Rows)
{
  controlName = r["ControlName"].ToString().ToUpper();
  value = r["Value"].ToString();
  property = r["Property"].ToString();

  // check all controls on the form
  foreach (Control c in formControls)
  {
    // only change it if its the right control
    if (c.Name.ToUpper() == controlName)
    {
      propertyInfo = c.GetType().GetProperty(property);

      if (propertyInfo != null)
        propertyInfo.SetValue(c, value, null);  ******Calls Event Handler?!?!******
      //

      currentControl = c;
      break;
    }
  }
}

그렇다면 왜 가치를 설정할 때 이벤트 핸들러를 호출할까요? 여기에 내가 설정 한 내용은 다음과 같습니다.

<SnappletChangePassword>  
  <ControlName>buttonAcceptPassword</ControlName>
  <Property>Text</Property>  
  <Value>Accept</Value>
</SnappletChangePassword>
도움이 되었습니까?

해결책

간단하지만 완전한 프로그램으로 이것을 재현 할 수 없습니다.

using System;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;

class Test
{
    static void Main()
    {
        Button goButton = new Button { 
            Text = "Go!",
            Location = new Point(5, 5)
        };

        Button targetButton = new Button {
            Text = "Target",
            Location = new Point(5, 50)
        };
        goButton.Click += (sender, args) => SetProperty(targetButton, "Text", "Changed");
        targetButton.Click += (sender, args) => MessageBox.Show("Target clicked!");

        Form f = new Form { Width = 200, Height = 120,
                Controls = { goButton, targetButton }
        };
        Application.Run(f);
    }

    private static void SetProperty(object target, string propertyName, object value)
    {
        PropertyInfo property = target.GetType().GetProperty(propertyName);
        property.SetValue(target, value, null);
    }
}

비슷한 완전한 프로그램을 생각해 볼 수 있습니까? 하다 그것을 보여 주시겠습니까?

다른 팁

슬프게도, 나는 그것을 재현 할 수 없었습니다. 원인이 무엇인지 잘 모르겠지만 수정하기 위해 한 모든 것은 버튼을 삭제하고 다시 넣었습니다.

그것이 무엇인지 확실하지 않지만 코드에 감사드립니다.

.NET2.0에서 글을 쓰지 않았습니까?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top