什么是反射
反射,Reflection,通过它我们可以在运行时获得各种信息,如程序集、模块、类型、字段、属性、方法和事件
通过对类型动态实例化后,还可以对其执行操作
简单来说就是用string可以在runtime为所欲为的东西,实际上就是一个.net framework内建的万能工厂
一般用于插件式框架程序和设计模式的实现,当然反射是一种手段可以充分发挥其能量来完成你想做的任何事情(前面好象见过一位高人用反射调用一个官方类库中未说明的函数。。。)
示例:
using System;
using System.Collections.Generic;
using System.Text;
namespace Example25Lib
{
public class Class1
{
private string name;
private int age;
//如果显式的声明了无参数构造函数,客户端只需要用程序集的CreateInstance即可实例化该类
//在此特意不实现,以便在客户调用端体现构造函数的反射实现
//public Class1()
//{
//}
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);
}
}
}
反射实例化对象并调用其方法,属性和事件的反射调用略去
using System;
using System.Collections.Generic;
using System.Text;
//注意添加该反射的命名空间
using System.Reflection;
namespace Example25
{
class Program
{
static void Main(string[] args)
{
//加载程序集
Assembly tmpAss = Assembly.LoadFile(AppDomain.CurrentDomain.BaseDirectory + “Example25Lib.dll”);
//遍历程序集内所有的类型,并实例化
Type[] tmpTypes = tmpAss.GetTypes();
foreach (Type tmpType in tmpTypes)
{
//获取第一个类型的构造函数信息
ConstructorInfo[] tmpConsInfos = tmpType.GetConstructors();
foreach (ConstructorInfo tmpConsInfo in tmpConsInfos)
{
//为构造函数生成调用的参数集合
ParameterInfo[] tmpParamInfos = tmpConsInfo.GetParameters();
object[] tmpParams = new object[tmpParamInfos.Length];
for (int i = 0; i < tmpParamInfos.Length; i++)
{
tmpParams[i] = tmpAss.CreateInstance(tmpParamInfos[i].ParameterType.FullName);
if (tmpParamInfos[i].ParameterType.FullName == “System.String”)
{
tmpParams[i] = “Clark”;
}
}
//实例化对象
object tmpObj = tmpConsInfo.Invoke(tmpParams);
Console.WriteLine(tmpObj);
//获取所有方法并执行
foreach (MethodInfo tmpMethod in tmpType.GetMethods())
{
//为方法的调用创建参数集合
tmpParamInfos = tmpMethod.GetParameters();
tmpParams = new object[tmpParamInfos.Length];
for (int i = 0; i < tmpParamInfos.Length; i++)
{
tmpParams[i] = tmpAss.CreateInstance(tmpParamInfos[i].ParameterType.FullName);
if (tmpParamInfos[i].ParameterType.FullName == “System.String”)
{
tmpParams[i] = “Clark Zheng”;
}
if (tmpParamInfos[i].ParameterType.FullName == “System.Int32″)
{
tmpParams[i] = 27;
}
}
tmpMethod.Invoke(tmpObj, tmpParams);
}
//调用完方法后再次打印对象,比较结果
Console.WriteLine(tmpObj);
}
}
Console.ReadLine();
}
}
}
结果:
Name: Clark, Age: 0
Name: Clark Zheng, Age: 27
通过对类型动态实例化后,还可以对其执行操作
简单来说就是用string可以在runtime为所欲为的东西,实际上就是一个.net framework内建的万能工厂
一般用于插件式框架程序和设计模式的实现,当然反射是一种手段可以充分发挥其能量来完成你想做的任何事情(前面好象见过一位高人用反射调用一个官方类库中未说明的函数。。。)
示例:
using System;
using System.Collections.Generic;
using System.Text;
namespace Example25Lib
{
public class Class1
{
private string name;
private int age;
//如果显式的声明了无参数构造函数,客户端只需要用程序集的CreateInstance即可实例化该类
//在此特意不实现,以便在客户调用端体现构造函数的反射实现
//public Class1()
//{
//}
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);
}
}
}
反射实例化对象并调用其方法,属性和事件的反射调用略去
using System;
using System.Collections.Generic;
using System.Text;
//注意添加该反射的命名空间
using System.Reflection;
namespace Example25
{
class Program
{
static void Main(string[] args)
{
//加载程序集
Assembly tmpAss = Assembly.LoadFile(AppDomain.CurrentDomain.BaseDirectory + “Example25Lib.dll”);
//遍历程序集内所有的类型,并实例化
Type[] tmpTypes = tmpAss.GetTypes();
foreach (Type tmpType in tmpTypes)
{
//获取第一个类型的构造函数信息
ConstructorInfo[] tmpConsInfos = tmpType.GetConstructors();
foreach (ConstructorInfo tmpConsInfo in tmpConsInfos)
{
//为构造函数生成调用的参数集合
ParameterInfo[] tmpParamInfos = tmpConsInfo.GetParameters();
object[] tmpParams = new object[tmpParamInfos.Length];
for (int i = 0; i < tmpParamInfos.Length; i++)
{
tmpParams[i] = tmpAss.CreateInstance(tmpParamInfos[i].ParameterType.FullName);
if (tmpParamInfos[i].ParameterType.FullName == “System.String”)
{
tmpParams[i] = “Clark”;
}
}
//实例化对象
object tmpObj = tmpConsInfo.Invoke(tmpParams);
Console.WriteLine(tmpObj);
//获取所有方法并执行
foreach (MethodInfo tmpMethod in tmpType.GetMethods())
{
//为方法的调用创建参数集合
tmpParamInfos = tmpMethod.GetParameters();
tmpParams = new object[tmpParamInfos.Length];
for (int i = 0; i < tmpParamInfos.Length; i++)
{
tmpParams[i] = tmpAss.CreateInstance(tmpParamInfos[i].ParameterType.FullName);
if (tmpParamInfos[i].ParameterType.FullName == “System.String”)
{
tmpParams[i] = “Clark Zheng”;
}
if (tmpParamInfos[i].ParameterType.FullName == “System.Int32″)
{
tmpParams[i] = 27;
}
}
tmpMethod.Invoke(tmpObj, tmpParams);
}
//调用完方法后再次打印对象,比较结果
Console.WriteLine(tmpObj);
}
}
Console.ReadLine();
}
}
}
结果:
Name: Clark, Age: 0
Name: Clark Zheng, Age: 27
【什么是反射】相关文章
3. 什么是反射
4. 什么叫应用程序域?什么是托管代码?什么是强类型系统?什么是装箱和拆箱?什么是重载?CTS、CLS和CLR分别作何解释?
5. 什么叫应用程序域?什么是受管制的代码?什么是强类型系统?什么是装箱和拆箱?
6. 什么是静态路由,其特点是什么?什么是动态路由,其特点是什么?
8. 什么是符号链接,什么是硬链接?符号链接与硬链接的区别是什么?
9. 什么是典型的软件三层结构?软件设计为什么要分层?软件分层有什么好处?
10. 什么是Deployment descriptors;都有什么类型的部署描述符
本文来源:https://www.mianshiwenti.com/a13014.html
进入下载页面
﹝什么是反射﹞相关内容
- Spring面试:什么是Spring, 它有什么特点
- 什么是托管函数?托管函数有什么用?
- 什么是smarty? Smarty的优点是什么?
- 什么是触发器(trigger)? 触发器有什么作用?
- 什么是事务?为什么需要事务?
- 什么是UTL_FILE, 她相关的Procedure和函数都有什么
- 什么是虚拟内存?虚拟内存有什么优势?
- 什么是职业操守?
- 什么是职业道德
- 什么是教师职业道德