public object GetCompProperty(object form, string compName, string propertyName)
{
Type formType = form.GetType();
FieldInfo compInfo = formType.GetField(compName, BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
Component obj = (Component)compInfo.GetValue(form);
Type compType = obj.GetType();
PropertyDescriptorCollection propertyDescriptorCollection = TypeDescriptor.GetProperties(obj);
PropertyDescriptor propertyDescriptor = propertyDescriptorCollection.Find(propertyName, false);
if (propertyDescriptor != null)
{
return propertyDescriptor.GetValue(obj);
}
return null;
}
public void SetCompProperty(object form, string compName, string propertyName, object value)
{
Type formType = form.GetType();
FieldInfo compInfo = formType.GetField(compName, BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
Component obj = (Component)compInfo.GetValue(form);
Type compType = obj.GetType();
PropertyDescriptorCollection propertyDescriptorCollection = TypeDescriptor.GetProperties(obj);
PropertyDescriptor propertyDescriptor = propertyDescriptorCollection.Find(propertyName, false);
if (propertyDescriptor != null)
{
propertyDescriptor.SetValue(obj, value);
}
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show(GetCompProperty(this, "button1", "Width").ToString());
SetCompProperty(this, "button1", "Width", 100);
}
{
Type formType = form.GetType();
FieldInfo compInfo = formType.GetField(compName, BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
Component obj = (Component)compInfo.GetValue(form);
Type compType = obj.GetType();
PropertyDescriptorCollection propertyDescriptorCollection = TypeDescriptor.GetProperties(obj);
PropertyDescriptor propertyDescriptor = propertyDescriptorCollection.Find(propertyName, false);
if (propertyDescriptor != null)
{
return propertyDescriptor.GetValue(obj);
}
return null;
}
public void SetCompProperty(object form, string compName, string propertyName, object value)
{
Type formType = form.GetType();
FieldInfo compInfo = formType.GetField(compName, BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
Component obj = (Component)compInfo.GetValue(form);
Type compType = obj.GetType();
PropertyDescriptorCollection propertyDescriptorCollection = TypeDescriptor.GetProperties(obj);
PropertyDescriptor propertyDescriptor = propertyDescriptorCollection.Find(propertyName, false);
if (propertyDescriptor != null)
{
propertyDescriptor.SetValue(obj, value);
}
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show(GetCompProperty(this, "button1", "Width").ToString());
SetCompProperty(this, "button1", "Width", 100);
}