Reflection
Reflection
1,动态加载DLL, Assembly.Load(string dllName);反射的入口
2,从metadata DLL中元数据,获取类型.使用activator 创建对象
3,调用函数, Invoke(object,new object[]{11}
非重载函数:
MethodInfo name=type.Getmethod("show3")
name.Invoke(object,new object[]{string}
重载函数
函数调用必须指定(函数名称,参数数量.参数类型)
比如:type.Getmethod("show3",new type[]{}) /无参; type.Getmethod("show3",new type[]{type of (int)}); /1个int参数; type.Getmethod("show3",new type[]{type of (string)}) /一个string参数;
调用private 方法:type.GetType("方法名称",(BindingFlags.Instance|bindingFlags.Public|BindingFlags.Nonpublic)) /nonpublic private
反射:
好处:课动态加载,可配置课扩展
不好:耗资源/编译器无法检查,破坏单例
应用:
1,数据库访问层的实现,结合泛型实现单个方法访问数据库所有表

浙公网安备 33010602011771号