1、下列代码在运行中会发生什么问题?如何避免?
List<int> ls = new List<int>(new int[] { 1,2,3,4,5});
foreach (var item in ls)
{
Console.WriteLine(item*item);
ls.Remove(item);
}
会产生运行时错误,不能一边遍历一边修改。建议使用for循环来代替,foreach建议只用在读取上,不用在修改上。
2、反射的实现原理?
审查元数据并收集关于它的类型信息的能力。反射个人认为,就是得到程序集中的属性和方法。
实现步骤:
- 导入using System.Reflection。
- Assembly.Load("程序集")加载程序集,返回类型是一个Assembly。
- foreach (Type type in assembly.GetTypes()) { string t