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); //获得属性的值
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); //获得属性的值
相关文章推荐
- [学习笔记]c#中的委托、事件、Func、Predicate、Observer设计模式以及其他
- C#学习笔记(十):反射
- C# 委托 回调 操作UI主线程 学习笔记
- C#学习笔记 反射
- C#学习笔记 委托
- C# 反射技术测试笔记
- C#基础加强第五天--委托,事件与反射
- 【黑马程序员】C#基础,一些笔记
- C#反射实用技术笔记
- C#学习笔记2——反射
- C#学习笔记(8)——委托应用(显示,写入时间)
- C#学习笔记8——C# 中的委托和事件
- 精通C#学习笔记之事件(一)使用委托的弊端
- c# 我的学习笔记 -反射
- c#网络编程学习笔记00_补上一些基础概念
- C#教程第8讲委托2(学习笔记)
- C#学习笔记之——一些练习(包含了一些out的使用,string的使用,StringBuilder的使用,类的属性,最大公约数的求法,还有英雄,武器类的设置)
- <NET CLR via c# 第4版>笔记 第17章 委托
- C#之委托概念认识学习笔记
- C# 反射技术测试笔记