如何递归遍历实体的性质 [英] How to recursively Iterate over properties of an Entity

查看:123
本文介绍了如何递归遍历实体的性质的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想这是我在我的数据库



table Ancestor (  
  idAncestor int not null,  
  name varchar(20) not null,  
)  

table Descendant (  
  idDescendant int not null,  
  name varchar(20) not null,  
  Ancestor_idAncestor int not null  
)  

当ADO.NET生成上面两个表的实体对象,我可以访问从高到低 通过 Ancestors.First()。后人

When ADO.NET generates the entity object for the above 2 tables, I can access Descendant of Ancestor through Ancestors.First().Descendants.

如果我是递归遍历一个祖先的后代(S)或后代的后代(S),并打印出 ID 名称,下面是我的尝试

If I were to recursively iterate over an ancestor's descendant(s) or descendant's descendant(s) and print out its id and name, the following is my attempt


public void Iterate(Ancestor a)  
{  
   Type t = a.GetType();
   PropertyInfo[] props = t.GetProperties();
   foreach(var prop in props){
      // pseudo code here
      if(prop is entitycollection)
      {
         // how do I convert prop to entity collection here??
         foreach(var p in prop){
            Iterate(p)
         }         
      } else {
         print prop.GetValue(a, null)
      }
   }       
}

我的问题是试图找出如果一个实体的属性是实体的集合,如果是,则查找该集合类型抱,然后遍历类型,等等。

My problem is trying to figure out if a entity property is of entity collection, if it is, then find the type hold by the collection, then iterate over the type, and so on.

感谢

推荐答案

这应该是一个粗糙的版本你要找的是什么:

This should be a crude version of what you're looking for:

private static void recurseAndPrintProperties(Object ObjectToRecurse) {
   foreach (PropertyInfo pi in ObjectToRecurse.GetType().GetProperties()) {
       if ((pi.PropertyType.IsGenericType && pi.PropertyType.GetGenericTypeDefinition() == typeof(EntityCollection<>))) {
           IEnumerable collection = (IEnumerable)pi.GetValue(ObjectToRecurse, null);

           foreach (object val in collection)
               recurseAndPrintProperties(val);
       } else {
            if (pi.PropertyType == typeof(Descendant)) {
                Descendant actualDescendant = (Descendant)pi.GetValue(ObjectToRecurse, null);
                Console.WriteLine(actualDescendant.idDescendant + " - " + actualDescendant.Name);
            } else
                Console.WriteLine(pi.Name + "  -  " + pi.GetValue(ObjectToRecurse, null));
       }
   }
}

修改

这code来自一些code我有previously打得四处克隆实体。这是(您可能必须修改它也考虑到不重复的特性塔尔被映射到主键)

This code came from some code I had previously played around with for cloning entities. Here it is (you might have to modify it to also take into consideration not duplicating your properties thar are mapped to primary keys)

    public static T CloneEntity<T>(T Obj) where T : EntityObject, new() {
        T Clone = new T();

        Type typeToClone = Obj.GetType();
        Type[] BadGenericTypes = new Type[] { typeof(EntityCollection<>), typeof(EntityReference<>) };
        Type[] BadTypes = new Type[] { typeof(System.Data.EntityKey) };

        foreach (PropertyInfo pi in typeToClone.GetProperties().Where(p => p.CanWrite)) {
            if (pi.PropertyType.IsGenericType && BadGenericTypes.Contains(pi.PropertyType.GetGenericTypeDefinition())
                || (BadTypes.Contains(pi.PropertyType))
                || (pi.Name.Equals(Extension.GetPropertyName(() => new FMVHistory().FMVHistoryId), StringComparison.CurrentCultureIgnoreCase)))
                continue;

            pi.SetValue(Clone, pi.GetValue(Obj, null), null);
        }
        return Clone;
    }

这篇关于如何递归遍历实体的性质的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆