http://home.hoolee.com/~code6421/Doc/Reflection.pdf
婺噎 挈
殳 娉
缬 苇 怼 苇噎
噎
酥 缬 牒
牒 牒
骣菡
菡
噎 寤
缬
菡 枣 缬
噎桤
撄 骊 牒
撄
寤
眍
骊
骊
撄
骊
撄
骊
骊
骊
骊
歃 菡
桤
缬
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Reflection;
namespace ReflectGridDemo
{
public class Form1 : System.Windows.Forms.Form
{
......................
......................
private Type FindType(string typeName)
{
foreach(Assembly assem in AppDomain.CurrentDomain.GetAssemblies())
{
Type t = assem.GetType(typeName);
if(t != null)
return t;
}
MessageBox.Show("Type is not found.");
return null;
}
private void btnProperties_Click(object sender, System.EventArgs e)
{
Type t = FindType(textBox1.Text);
dataGrid1.DataSource = t.GetProperties();
}
private void btnMethods_Click(object sender, System.EventArgs e)
{
Type t = FindType(textBox1.Text);
dataGrid1.DataSource = t.GetMethods();
}
private void btnEvents_Click(object sender, System.EventArgs e)
{
Type t = FindType(textBox1.Text);
dataGrid1.DataSource = t.GetEvents();
}
private void btnMembers_Click(object sender, System.EventArgs e)
{
Type t = FindType(textBox1.Text);
dataGrid1.DataSource = t.GetMembers();
}
private void btnAttributes_Click(object sender, System.EventArgs e)
{
Type t = FindType(textBox1.Text);
ArrayList list = new ArrayList();
object[] minfos = t.GetCustomAttributes(true);
foreach(object mi in minfos)
list.Add(mi.GetType());
dataGrid1.DataSource = list;
}
private void btnFields_Click(object sender, System.EventArgs e)
{
Type t = FindType(textBox1.Text);
dataGrid1.DataSource = t.GetFields();
}
}
}
叱撄 妞 缬
噎
桤 噎 桤
噎
牒 叱撄
叱撄 瀹 菡 叱
Type t = listBox1.GetType();
object o = t.GetProperty("Items").GetValue(listBox1,null);
MethodInfo mi = o.GetType().GetMethod("Clear");
mi.Invoke(o,null);
Type t = FindType(“System.Windows.Forms.TextBox”);
ConstructorInfo ci = t.GetConstructor(new Type[]{});
object o = ci.Invoke(null);
this.Controls.Add((Control)o);
Type t = listBox1.GetType();
object o = t.GetProperty("Items").GetValue(listBox1,null);
o.GetType().InvokeMember("Clear",BindingFlags.InvokeMethod,null,o,null);
噎岘 菡 枣
骊 骊
枣 桤
桤 桤
菡
噎 斓
歃
噎 噎岘 噎
菡 桤 桤
梓
噎 妞 噎
挈 桤
噎
缬
寤
斓
桤 牒
寤
炝
斓 菡
寤
瀹菡
桤 噎 菡
GetEvents
怼
噎
using System;
using System.ComponentModel;
using System.Reflection;
namespace WindowsApplication1
{
public class BindingHelper
{
private object _obj1,_obj2;
private string _property1,_property2;
public BindingHelper() { }
public BindingHelper(object obj1,object obj2,string property1,string property2)
{
_obj1 = obj1;
_obj2 = obj2;
_property1 = property1;
_property2 = property2;
BindToObject();
BindToTarget();
}
private void ObjectChanged(object sender,EventArgs args)
{
PropertyDescriptor prop = TypeDescriptor.GetProperties(_obj2).Find(_property2,false);
PropertyDescriptor value = TypeDescriptor.GetProperties(_obj1).Find(_property1,false);
if(prop.GetValue(_obj2).ToString() != value.GetValue(_obj1).ToString())
prop.SetValue(_obj2,value.GetValue(_obj1).ToString());
}
private void TargetChanged(object sender,EventArgs args)
{
PropertyDescriptor prop = TypeDescriptor.GetProperties(_obj1).Find(_property1,false);
PropertyDescriptor value = TypeDescriptor.GetProperties(_obj2).Find(_property2,false);
if(prop.GetValue(_obj1).ToString() != value.GetValue(_obj2).ToString())
prop.SetValue(_obj1,value.GetValue(_obj2).ToString());
}
private void BindToObject()
{
PropertyDescriptor prop = TypeDescriptor.GetProperties(_obj1).Find(_property1,false);
prop.AddValueChanged(_obj1,new EventHandler(ObjectChanged));
}
private void BindToTarget()
{
EventDescriptor changedEvent =
TypeDescriptor.GetEvents(_obj2).Find(_property2+"Changed",false);
changedEvent.AddEventHandler(_obj2,new EventHandler(TargetChanged));
}
}
}
酥 菡
逯 姿
炱菡 桤 沂