How to iterate the List in Reflection

后端 未结 5 1988
粉色の甜心
粉色の甜心 2020-12-06 17:21

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

In reflection i can get the value of Students Property.

Now the p

相关标签:
5条回答
  • 2020-12-06 17:53

    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 讨论(0)
  • 2020-12-06 17:54

    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
    }
    
    0 讨论(0)
  • 2020-12-06 17:56

    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);
    
    0 讨论(0)
  • 2020-12-06 17:58

    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)

    0 讨论(0)
  • 2020-12-06 18:03

    Try this

    IEnumerable<Student> collection = (IEnumerable<Student>)studentPro.GetValue(studentObj,null);
    
    0 讨论(0)
提交回复
热议问题