中国开发网: 论坛: 程序员情感CBD: 贴子 129736
haitao
你皤羽钴厌个呃憷.net/c#钴晚姻
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));
}
}
}

酥 菡
逯  姿

炱菡 桤 沂
我的blog:http://szhaitao.blog.hexun.com & http://www.hoolee.com/user/haitao
--以上均为泛泛之谈--
不尽牛人滚滚来,无边硬伤纷纷现 人在江湖(出来的),哪能不挨刀(总归是要的)
网络对话,歧义纷生;你以为明白了对方的话,其实呢?

您所在的IP暂时不能使用低版本的QQ,请到:http://im.qq.com/下载安装最新版的QQ,感谢您对QQ的支持和使用

相关信息:


欢迎光临本社区,您还没有登录,不能发贴子。请在 这里登录