여기 리플렉션을 사용하여 런타임에 동적으로 속성을 설정하는 방법을 보여주는 샘플 Windows Forms 응용 프로그램 샘플을 만들었습니다. 이를 깨끗하게 처리하는 좋은 방법은 아마도 일부 컬렉션 유형을 반복하는 것일 것입니다.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//This sample shows invocation using a loosely typed object.
//Assumes you will send the correct object type to the property setter at runtime
Dictionary<string, object> setupObjectValues = new Dictionary<string, object>();
setupObjectValues.Add("Text", "Hello World");
setupObjectValues.Add("ReadOnly", true);
setupObjectValues.Add("Location", new Point(10, 5));
foreach (string val in setupObjectValues.Keys)
{
UpdateControlObjectValue(textBox1, val, setupObjectValues[val]);
}
}
public delegate void SetObjectPropertyDelegate(Control param, string PropertyName, object NewValue);
public void UpdateControlObjectValue(Control sender, string PropertyName, object Value)
{
//Pass the method name into the delegate and match signatures
//params collection used for parameters on method called in this case object and string
sender.Invoke(new SetObjectPropertyDelegate(SetObjectProperty), sender, PropertyName, Value);
}
//Called by delegate (Matches signature of SetObjectPropertyDelegate)
private void SetObjectProperty(Control sender, string PropertyName, object NewValue)
{
if (sender == null || String.IsNullOrEmpty(PropertyName) || NewValue == null)
throw new ArgumentException("Invalid Argument");
try
{
//Guaranteed to be a control due to strong typing on parameter declaration
sender.GetType().GetProperty(PropertyName).SetValue(sender, NewValue, null);
//Set Value on MSDN doc : http://msdn.microsoft.com/en-us/library/xb5dd1f1.aspx
}
catch (System.Reflection.AmbiguousMatchException ex)
{
//Two properties were found that match your Property Name
}
catch (ArgumentNullException ex)
{
//Null Property Found
}
catch (ArgumentException ex)
{
//Invalid Argument
}
catch (System.Reflection.TargetException ex)
{
//Invalid Target
}
catch (System.Reflection.TargetParameterCountException ex)
{
//Invalid number of parameters passed to reflection invoker
}
catch (System.MethodAccessException ex)
{
//Could not access the method to invoke it
}
catch (System.Reflection.TargetInvocationException ex)
{
//Problem invoking the target method
}
catch (Exception ex)
{
//serious problem
}
}
}
}
은 예에는'Installation' 없다, 그래서 우리는 정말에 대해 언급 할 수 없습니다 "설치가 필드하지만 방법처럼 사용됩니다." –