C# 如何在反射中迭代列表

C# 如何在反射中迭代列表,c#,reflection,C#,Reflection,我有一个名为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

我有一个名为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
}
请帮帮我。

试试这个

IEnumerable<Student> collection = (IEnumerable<Student>)studentPro.GetValue(studentObj,null);
IEnumerable集合=(IEnumerable)studentPro.GetValue(studentObj,null);

您尝试的方法是正确的。您只需修复代码并从
GetValue
转换返回值:

var collection = (List<Student>)studentPro.GetValue(studentObj,null);

foreach(var item in collection)
{
     if(item.StudentID == 33)
         //Do stuff
}
var collection=(List)studentPro.GetValue(studentObj,null);
foreach(集合中的var项)
{
如果(item.StudentID==33)
//做事
}

您只需投下它:

var collection = (List<Student>) studentPro.GetValue(studentObj,null);

其他人建议选演员,但我认为这对你不起作用。。。如果您可以访问学生类,那么您就不会从使用反射开始。因此,只需转换为IEnumerable,然后在循环中,您必须再次使用反射来访问集合中每个项的任何属性

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=(IEnumerable)studentPro.GetValue(studentObj,null)
您可以使用下面的方法从代理对象中创建POCO对象。请注意,我依靠XMLIgnore属性来打破循环引用

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<Table1> Table1s { get; set; }

    public DbSet<Table2> Table2s { get; set; }

}

public class Table1
{
    public int ID { get; set; }

    public string name { get; set; }

    virtual public List<Table2> Table2s { get; set; }
}


public class Table2
{
    public int ID { get; set; }

    public string Name { get; set; }
    [XmlIgnore]
    virtual public Table1 Table1 { get; set; }
}
static object DeepCopy(object obj,类型targetType)
{
如果(obj!=null)
{
类型t=obj.GetType();
object objCopy=Activator.CreateInstance(targetType);
类型copyType=targetType;
var道具=
t、 GetProperties();
//.Where(x=>x.PropertyType.GetCustomAttributes(typeof(XmlIgnoreAttribute),false)。长度==0);
foreach(道具中的var propertyInfo)
{
var targetProperty=copyType.GetProperties().Where(x=>x.Name==propertyInfo.Name).First();
if(targetProperty.GetCustomAttributes(typeof(XmlIgnoreAttribute),false).Length>0)
{
继续;
}
if(propertyInfo.PropertyType.IsClass)
{
if(propertyInfo.PropertyType.GetInterface(“IList”,true)!=null)
{
var list=(IList)Activator.CreateInstance(targetProperty.PropertyType);
SetValue(对象复制,列表);
var sourceList=propertyInfo.GetValue(obj)作为IList;
foreach(sourceList中的var o)
{
添加(DeepCopy(o,targetProperty.PropertyType.UnderlineingSystemType.GenericTypeArguments[0]);
}
}
else if(propertyInfo.PropertyType==typeof(string))
{
SetValue(objCopy,propertyInfo.GetValue(obj));
}
其他的
{
SetValue(objCopy,DeepCopy(propertyInfo.GetValue(obj),targetProperty.PropertyType));
}
}
其他的
{
SetValue(objCopy,propertyInfo.GetValue(obj));
}
}
返回对象副本;
}
返回null;
}
类MyDbContext:DbContext
{
public MyDbContext():base(@“Server=(LocalDb)\v12.0;Trusted_Connection=True;”)
{
}
公共DbSet Table1s{get;set;}
公共DbSet Table2s{get;set;}
}
公共课表1
{
公共int ID{get;set;}
公共字符串名称{get;set;}
虚拟公共列表表2{get;set;}
}
公共课表2
{
公共int ID{get;set;}
公共字符串名称{get;set;}
[XmlIgnore]
虚拟公共表1表1{get;set;}
}

尝试此操作时,我遇到一个错误-使用泛型类型“System.Collections.generic.IEnumerable”需要1个类型argumentstry(System.Collections.IEnumerable)来解决他的问题。虽然我的问题是自己无法解决这么简单的问题。他真的不应该使用普通的收藏。是的,很好。但是,铸造应该与反射完成。在反思中,我们不知道列表的类型。我们不知道studentObj的实际类型。尝试此操作时,我遇到一个错误-使用泛型类型“System.Collections.generic.IEnumerable”需要1个类型参数。Jagan-您发布的错误很明显,它需要一个类型。此外,如果您不知道如何使用,您不应该使用您使用的方法(考虑到您的代码在概念上是正确的,除了您忘记强制转换的事实)。好的。在反思中,只需考虑我有一个对象,而不是它的类型。如果该对象是集合,我需要迭代它并打印所有值。