开发者

How to iterate the List in Reflection

I am having one property called Students which is of type List<Student>.

In reflection i can get the value of Students Property.

Now the problem is How to iterate the List of Students.

I need to check whether StudentID [ some value ] is in that collection.

var collection = studentPro.GetValue(studentObj,null);

//I need to iterat开发者_如何转开发e like this,

foreach(var item in collection)
{
     if(item.StudentID == 33)
         //Do stuff
}

Please help me.


You just need to cast it:

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

The value returned to you and stored in var is of type object. So you need to cast it to List<Student> first, before trying looping through it.

RANT

That is why I personally do not like var, it hides the type - unless in VS you hover on it. If it was a declared with type object it was immediately obvious that we cannot iterate through it.


UPDATE

Yes its good. But casting should be done with reflection. In reflection we dont know the type of List. We dont know the actual type of the studentObj

In order to do that, you can cast to IEnumerable:

var collection = (IEnumerable) studentPro.GetValue(studentObj,null);


Try this

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


Others have suggested casting to List but I will assume that this won't work for you... if you had access to the Student class, you wouldn't be using reflection to begin with. So instead, just cast to IEnumerable and then inside your loop, you'll have to use reflection again to access whatever properties you want off of each item in the collection.

var collection = (IEnumerable)studentPro.GetValue(studentObj,null)


The way you tried is, is the right one. You just need to fix your code and cast the return value from GetValue:

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

foreach(var item in collection)
{
     if(item.StudentID == 33)
         //Do stuff
}


You can have something like below to create a POCO object out of your proxy object. Please note that I am relying on use of XMLIgnore attribute to break circular references

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; }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜