如何创建并初始化程序集里List类型的反射
2024-10-12 23:08:32
参考网址:http://stackoverflow.com/questions/315231/using-reflection-to-set-a-property-with-a-type-of-listcustomclass
遇到的问题:需要反射一个方法,但是它的参数也是程序集里的一个list数组。当调用一般的传参过程会报错,使用下述的语法创建list数组可以当参数传入。
class Foo
{
public string Bar { get; set; }
}
class Program
{
static void Main()
{
Type type = typeof(Foo); // possibly from a string
IList list = (IList) Activator.CreateInstance(
typeof(List<>).MakeGenericType(type));
object obj = Activator.CreateInstance(type);
type.GetProperty("Bar").SetValue(obj, "abc", null);
list.Add(obj);
}
}
最新文章
- eclipse配置tomcat
- C++小项目:directx11图形程序(五):shadersclass
- python脚本基础总结
- c#params ref out
- offsetTop、clientTop、scrollTop、offsetTop
- c笔记
- 在页面中使用Eval的两种方案
- C++中的仿函数,std::function和bind()的用法
- explain 用法详解
- [Javascript] Get Started with LeafletJS Mapping
- VirtualBox设置共享文件夹和镜像访问的方法
- 团队介绍 | 魅动 Magic Motion
- 行业百科知识--Github
- 网络分析法(Analytic Network Process,ANP)
- 【SRM-09 B】撕书II
- vue中使用cookies和crypto-js实现记住密码和加密
- mysql数据库自动备份脚本
- struts2 常量
- 自定义Spark Partitioner提升es-hadoop Bulk效率——续
- linux性能系列--cpu