反射实例:
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”;
}
}