学习笔记--反射入门
2012-12-28 11:08
274 查看
虽然反射一般情况用不到,但是咱能想了解下反射的工作原理
首先定义一个类库ExampleLib,我就加了一个类Class1
namespace ExampleLib
{
public class Class1
{
private string name;
private int age;
public Class1(string Name, int Age)
{
name = Name;
age = Age;
}
public void changeName(string newName)
{
name = newName;
}
public void changeAge(int newAge)
{
age = newAge;
}
public override string ToString()
{
return string.Format("name:{0},age:{1}", name, age);
}
}
}
然后添加一个应用程序RfExamoleLib来分析这个类库
namespace RfExamoleLib
{
class Program
{
//测试输出顺序<本函数跟这个反射的例子没有关系,呵呵...>
public static string testFinally()
{
try
{
string rtn = string.Empty;
//int x = int.Parse(rtn);
Console.WriteLine("try");
return "try";
}
catch (Exception)
{
Console.WriteLine("catch");
//return "catch";
}
finally
{
Console.WriteLine("finally");
}
return "zhe...";
}
static void Main(string[] args)
{
//加载程序集
Assembly tmpAss = Assembly.LoadFile(AppDomain.CurrentDomain.BaseDirectory + "ExampleLib.dll");
//遍历
Type[] tmpTypes = tmpAss.GetTypes();
foreach (Type tmpType in tmpTypes)
{
//获取第一个类型的构造函数信息
ConstructorInfo[] tmpConstructInfos = tmpType.GetConstructors();
foreach (ConstructorInfo tmpConstructInfo in tmpConstructInfos)
{
//为构造函数生成调用的参数集合
ParameterInfo[] tmpParameterInfos = tmpConstructInfo.GetParameters();
object[] tmpParameters = new object[tmpParameterInfos.Length];
for (int i = 0; i < tmpParameterInfos.Length; i++)
{
//创建参数集合
tmpParameters[i] = tmpAss.CreateInstance(tmpParameterInfos[i].ParameterType.FullName);
if (tmpParameterInfos[i].ParameterType.FullName == "System.String")
{
tmpParameters[i] = "chen";
}
if (tmpParameterInfos[i].ParameterType.FullName == "System.Int32")
{
tmpParameters[i] = 29;
}
}
//实例化对象
object tmpObject = tmpConstructInfo.Invoke(tmpParameters);//构造函数实例化
Console.WriteLine(tmpObject);//执行重新的ToString()方法
//获取所有方法并执行
foreach (MethodInfo tmpMehdoInfo in tmpType.GetMethods())
{
//为方法的调用创建参数集合
tmpParameterInfos = tmpMehdoInfo.GetParameters();
tmpParameters = new object[tmpParameterInfos.Length];
for (int i = 0; i < tmpParameterInfos.Length; i++)
{
tmpParameters[i] = tmpAss.CreateInstance(tmpParameterInfos[i].ParameterType.FullName);
if (tmpParameterInfos[i].ParameterType.FullName == "System.String")
{
tmpParameters[i] = "chenzhm";
}
if (tmpParameterInfos[i].ParameterType.FullName == "System.Int32")
{
tmpParameters[i] = 30;
}
}
tmpMehdoInfo.Invoke(tmpObject, tmpParameters);
}
//调用方法后再次打印
Console.WriteLine(tmpObject);
}
}
//Console.WriteLine(AppDomain.CurrentDomain.BaseDirectory);
Console.WriteLine("call result:"+testFinally());
Console.ReadLine();
}
}
}
首先定义一个类库ExampleLib,我就加了一个类Class1
namespace ExampleLib
{
public class Class1
{
private string name;
private int age;
public Class1(string Name, int Age)
{
name = Name;
age = Age;
}
public void changeName(string newName)
{
name = newName;
}
public void changeAge(int newAge)
{
age = newAge;
}
public override string ToString()
{
return string.Format("name:{0},age:{1}", name, age);
}
}
}
然后添加一个应用程序RfExamoleLib来分析这个类库
namespace RfExamoleLib
{
class Program
{
//测试输出顺序<本函数跟这个反射的例子没有关系,呵呵...>
public static string testFinally()
{
try
{
string rtn = string.Empty;
//int x = int.Parse(rtn);
Console.WriteLine("try");
return "try";
}
catch (Exception)
{
Console.WriteLine("catch");
//return "catch";
}
finally
{
Console.WriteLine("finally");
}
return "zhe...";
}
static void Main(string[] args)
{
//加载程序集
Assembly tmpAss = Assembly.LoadFile(AppDomain.CurrentDomain.BaseDirectory + "ExampleLib.dll");
//遍历
Type[] tmpTypes = tmpAss.GetTypes();
foreach (Type tmpType in tmpTypes)
{
//获取第一个类型的构造函数信息
ConstructorInfo[] tmpConstructInfos = tmpType.GetConstructors();
foreach (ConstructorInfo tmpConstructInfo in tmpConstructInfos)
{
//为构造函数生成调用的参数集合
ParameterInfo[] tmpParameterInfos = tmpConstructInfo.GetParameters();
object[] tmpParameters = new object[tmpParameterInfos.Length];
for (int i = 0; i < tmpParameterInfos.Length; i++)
{
//创建参数集合
tmpParameters[i] = tmpAss.CreateInstance(tmpParameterInfos[i].ParameterType.FullName);
if (tmpParameterInfos[i].ParameterType.FullName == "System.String")
{
tmpParameters[i] = "chen";
}
if (tmpParameterInfos[i].ParameterType.FullName == "System.Int32")
{
tmpParameters[i] = 29;
}
}
//实例化对象
object tmpObject = tmpConstructInfo.Invoke(tmpParameters);//构造函数实例化
Console.WriteLine(tmpObject);//执行重新的ToString()方法
//获取所有方法并执行
foreach (MethodInfo tmpMehdoInfo in tmpType.GetMethods())
{
//为方法的调用创建参数集合
tmpParameterInfos = tmpMehdoInfo.GetParameters();
tmpParameters = new object[tmpParameterInfos.Length];
for (int i = 0; i < tmpParameterInfos.Length; i++)
{
tmpParameters[i] = tmpAss.CreateInstance(tmpParameterInfos[i].ParameterType.FullName);
if (tmpParameterInfos[i].ParameterType.FullName == "System.String")
{
tmpParameters[i] = "chenzhm";
}
if (tmpParameterInfos[i].ParameterType.FullName == "System.Int32")
{
tmpParameters[i] = 30;
}
}
tmpMehdoInfo.Invoke(tmpObject, tmpParameters);
}
//调用方法后再次打印
Console.WriteLine(tmpObject);
}
}
//Console.WriteLine(AppDomain.CurrentDomain.BaseDirectory);
Console.WriteLine("call result:"+testFinally());
Console.ReadLine();
}
}
}
相关文章推荐
- [原]java专业程序代写(qq:928900200),学习笔记之基础入门<反射>(二十九)
- java 从零开始,学习笔记之基础入门<反射>(二十九)
- 《unityshader 入门精要》漫反射光照与高光反射光照模型学习笔记
- UnityShader入门精要学习笔记(六):Unity中实现高光反射模型
- Java Reflection(反射) 入门学习笔记 之一
- Java Reflection(反射) 入门学习笔记 之二 (Invoke)
- Java Reflection(反射) 入门学习笔记 之三 (Array)
- Linux基础入门学习笔记三
- 10、ExtJS入门教程【学习笔记】
- magento后台开发学习笔记(入门实例向)
- 【算法零基础入门】 学习笔记一 什么是数据结构
- python小白入门学习笔记-爬虫入门
- quartz学习笔记(一)简单入门
- Javascript教程,javascript入门,学习笔记,Javascript Tutorials
- c#经典入门学习笔记-定义类成员
- python学习笔记--Django入门0 安装dangjo
- 深度学习入门教程UFLDL学习实验笔记三:主成分分析PCA与白化whitening
- java 反射机制-学习笔记(9)
- 卷积神经网络(CNN)学习笔记1:基础入门
- PHP学习笔记 - 入门篇(3)