您的位置:首页 > 编程语言 > C#

C#的一些笔记(委托和反射)

2013-05-12 21:24 357 查看
多线程以及委托:

private Thread mythread;
mythread = new Thread(new ThreadStart(sayhello));
mythread.Start();
public delegate void SayhelloHander();//声明委托函数,关键字delegate
private void sayhello()
{
//Console.WriteLine("hello world");
// Console.ReadLine();
if (textBox1.InvokeRequired == false) //是不是在主进程?
{
// MessageBox.Show(Thread.CurrentThread.ManagedThreadId.ToString());
textBox1.Text = "hello world";
}
else
{
SayhelloHander say = new SayhelloHander(sayhello); //实例化托管
// MessageBox.Show(Thread.CurrentThread.ManagedThreadId.ToString());
textBox1.Invoke(say); //进入托管函数
//this.button1_Click(null,null);

}
}反射:
x = 10;
assembly = Assembly.LoadFrom("ClassLibrary1.dll"); //载入dll
// assembly.Class1 class1 = new assembly.Class1();
Type j = assembly.GetType("ClassLibrary1.Class1");
Object obj = Activator.CreateInstance(j);//实例化
// j = obj.GetType();
MethodInfo hello = j.GetMethod("hello");//获取方法
hello.Invoke(obj,null); //调用方法
MemberInfo [] z = j.GetMember("name");//获得属性
string s = z[0].Name; //属性的名字

int valueReturn = (int)j.InvokeMember("name",
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.GetProperty,
null,
obj,
null); //获得属性的值
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: