如何在Reflection中迭代List
我有一个名为Students的属性,类型为List
。
在反思中,我可以获得学生财产的价值。
现在问题是如何迭代学生列表。
我需要检查StudentID [某个值]是否在该集合中。
var collection = studentPro.GetValue(studentObj,null); //I need to iterate like this, foreach(var item in collection) { if(item.StudentID == 33) //Do stuff }
请帮我。
你只需要施放它:
var collection = (List) studentPro.GetValue(studentObj,null);
返回给您并存储在var
是object
类型。 因此,在尝试循环之前,首先需要将其强制转换为List
。
RANT
这就是为什么我个人不喜欢var
,它隐藏了类型 – 除非在VS中你将它hover在它上面。 如果它是一个声明的类型object
,很明显我们不能迭代它。
UPDATE
是,很好。 但是应该用reflection进行铸造。 在反思中,我们不知道List的类型。 我们不知道studentObj的实际类型
为此,您可以转换为IEnumerable
:
var collection = (IEnumerable) studentPro.GetValue(studentObj,null);
试试这个
IEnumerable collection = (IEnumerable )studentPro.GetValue(studentObj,null);
其他人建议转换为List但我会认为这对你不起作用……如果你有权访问Student类,你就不会使用reflection开始。 因此,只需要转换为IEnumerable然后在循环中,您将不得不再次使用reflection来访问集合中每个项目所需的任何属性。
var collection = (IEnumerable)studentPro.GetValue(studentObj,null)
你尝试的方式是正确的。 您只需要修复代码并从GetValue
返回值:
var collection = (List)studentPro.GetValue(studentObj,null); foreach(var item in collection) { if(item.StudentID == 33) //Do stuff }
您可以使用下面的内容从代理对象中创建POCO对象。 请注意,我依赖于使用XMLIgnore属性来打破循环引用
上述就是C#学习教程:如何在Reflection中迭代List分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
static object DeepCopy(object obj, Type targetType) { if (obj != null) { Type t = obj.GetType(); object objCopy = Activator.CreateInstance(targetType); Type copyType = targetType; var props = t.GetProperties(); //.Where(x => x.PropertyType.GetCustomAttributes(typeof(XmlIgnoreAttribute), false).Length == 0); foreach (var propertyInfo in props) { var targetProperty = copyType.GetProperties().Where(x => x.Name == propertyInfo.Name).First(); if (targetProperty.GetCustomAttributes(typeof(XmlIgnoreAttribute), false).Length > 0) { continue; } if (propertyInfo.PropertyType.IsClass) { if (propertyInfo.PropertyType.GetInterface("IList", true)!=null) { var list = (IList)Activator.CreateInstance(targetProperty.PropertyType); targetProperty.SetValue(objCopy,list); var sourceList = propertyInfo.GetValue(obj) as IList; foreach (var o in sourceList) { list.Add(DeepCopy(o, targetProperty.PropertyType.UnderlyingSystemType.GenericTypeArguments[0])); } } else if (propertyInfo.PropertyType == typeof(string)) { targetProperty.SetValue(objCopy, propertyInfo.GetValue(obj)); } else { targetProperty.SetValue(objCopy, DeepCopy(propertyInfo.GetValue(obj), targetProperty.PropertyType)); } } else { targetProperty.SetValue(objCopy,propertyInfo.GetValue(obj)); } } return objCopy; } return null; } class MyDbContext:DbContext { public MyDbContext():base(@"Server=(LocalDb)v12.0;Trusted_Connection=True;") { } public DbSet Table1s { get; set; } public DbSet Table2s { get; set; } } public class Table1 { public int ID { get; set; } public string name { get; set; } virtual public List Table2s { get; set; } } public class Table2 { public int ID { get; set; } public string Name { get; set; } [XmlIgnore] virtual public Table1 Table1 { get; set; } }
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1009454.html