how to get all primitive type of an object

前端 未结 4 1651
夕颜
夕颜 2020-12-20 01:23

I wanna have all properties of an object which have primitive type, and if the object has a relation to another class , I wanna have the primitive properties of this another

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-20 02:03

    I would made as follows:

        void Traverse(Type type, ISet marks, ICollection result)
        {
            if (marks.Contains(type)) return; else marks.Add(type);
            foreach (var propertyInfo in type.GetProperties())
                if (propertyInfo.PropertyType.IsPrimitive) result.Add(propertyInfo);
                else Traverse(propertyInfo.PropertyType, marks, result);
        }
    

    and

    var props = new List();
    Traverse(yourRootType, new HashSet(), props);
    

提交回复
热议问题