C# 获取使用堆栈或递归调用的对象算法的所有属性信息

C# 获取使用堆栈或递归调用的对象算法的所有属性信息,c#,c#-4.0,C#,C# 4.0,有以下类型 public class Nested1 { public int Id {get; set;} public string Name {get; set;} public string Address {get; set;} } public class Nested2 { public int Id {get; set;} public Nested1 Nested {get; set;} } 我想通过type.GetProperties获取类Ne

有以下类型

public class Nested1
{
   public int Id {get; set;}
   public string Name {get; set;}
   public string Address {get; set;}
} 

public class Nested2
{
  public int Id {get; set;}
  public Nested1 Nested {get; set;}
}

我想通过type.GetProperties获取类Nested2及其子类Nested1的所有属性的列表,因此该列表将是Nested2.Id、Nested2.Nested、Nested1.Id、Nested1.Name、Nested1.Address。我已经使用递归实现了这一点,是否有可能使用堆栈方法?谢谢

我不知道这种方法对不对,但你可以用反射代替它


我不知道这种方法对不对,但你可以用反射代替它


我想你需要这样的东西

public List<PropertyInfo> GetAllProperties(Type type)
{
    var result = new List<PropertyInfo>();
    var queue = new Queue<Type>();
    var processedTypes = new HashSet<Type>();

    queue.Enqueue(type);
    while (queue.Count > 0)
    {
        var currentType = queue.Dequeue();
        processedTypes.Add(currentType);

        var properties = currentType.GetProperties();
        result.AddRange(properties);

        var typesToProcess = properties
            .Select(p => p.PropertyType)
            .Distinct()
            .Where(pt => !pt.IsPrimitive)
            .Where(pt => !processedTypes.Contains(pt));

        foreach(var t in typesToProcess)
        {
            queue.Enqueue(t);
        }
    }

    return result;
}
公共列表GetAllProperties(类型)
{
var result=新列表();
var queue=新队列();
var processedTypes=new HashSet();
队列。排队(类型);
而(queue.Count>0)
{
var currentType=queue.Dequeue();
processedTypes.Add(当前类型);
var properties=currentType.GetProperties();
结果。添加范围(属性);
var typesToProcess=属性
.Select(p=>p.PropertyType)
.Distinct()
.Where(pt=>!pt.IsPrimitive)
其中(pt=>!processedTypes.Contains(pt));
foreach(typesToProcess中的var t)
{
排队,排队(t);
}
}
返回结果;
}

我想你需要这样的东西

public List<PropertyInfo> GetAllProperties(Type type)
{
    var result = new List<PropertyInfo>();
    var queue = new Queue<Type>();
    var processedTypes = new HashSet<Type>();

    queue.Enqueue(type);
    while (queue.Count > 0)
    {
        var currentType = queue.Dequeue();
        processedTypes.Add(currentType);

        var properties = currentType.GetProperties();
        result.AddRange(properties);

        var typesToProcess = properties
            .Select(p => p.PropertyType)
            .Distinct()
            .Where(pt => !pt.IsPrimitive)
            .Where(pt => !processedTypes.Contains(pt));

        foreach(var t in typesToProcess)
        {
            queue.Enqueue(t);
        }
    }

    return result;
}
公共列表GetAllProperties(类型)
{
var result=新列表();
var queue=新队列();
var processedTypes=new HashSet();
队列。排队(类型);
而(queue.Count>0)
{
var currentType=queue.Dequeue();
processedTypes.Add(当前类型);
var properties=currentType.GetProperties();
结果。添加范围(属性);
var typesToProcess=属性
.Select(p=>p.PropertyType)
.Distinct()
.Where(pt=>!pt.IsPrimitive)
其中(pt=>!processedTypes.Contains(pt));
foreach(typesToProcess中的var t)
{
排队,排队(t);
}
}
返回结果;
}
像这样的东西

var types = new Queue<Type>();
var props = new List<PropertyInfo>();
types.Enqueue(typeof(Nested2));

while (types.Count > 0) {
    Type t = types.Dequeue();
    foreach (var prop in t.GetProperties()) {
        if (props.Contains(prop)) { continue; }
        props.Add(prop);
        if (IsCustomType(prop.PropertyType)) { types.Enqueue(prop.PropertyType); }
    }
}
var types=new Queue();
var props=新列表();
types.Enqueue(typeof(Nested2));
而(types.Count>0){
Type t=types.Dequeue();
foreach(t.GetProperties()中的var prop){
如果(props.Contains(prop)){continue;}
道具。添加(道具);
if(IsCustomType(prop.PropertyType)){types.Enqueue(prop.PropertyType);}
}
}
如果列表可能会变长,您可能更应该使用Viacheslav Smityukh建议的哈希集。

类似的内容

var types = new Queue<Type>();
var props = new List<PropertyInfo>();
types.Enqueue(typeof(Nested2));

while (types.Count > 0) {
    Type t = types.Dequeue();
    foreach (var prop in t.GetProperties()) {
        if (props.Contains(prop)) { continue; }
        props.Add(prop);
        if (IsCustomType(prop.PropertyType)) { types.Enqueue(prop.PropertyType); }
    }
}
var types=new Queue();
var props=新列表();
types.Enqueue(typeof(Nested2));
而(types.Count>0){
Type t=types.Dequeue();
foreach(t.GetProperties()中的var prop){
如果(props.Contains(prop)){continue;}
道具。添加(道具);
if(IsCustomType(prop.PropertyType)){types.Enqueue(prop.PropertyType);}
}
}

如果列表可能变长,您可能更应该使用Viacheslav Smityukh建议的哈希集。

我认为该算法类似于

  SomeStructure GetFromObject(object o)
    { var tt = new Stack<Helper>();
            types.Push(new Helper { Type = o.GetType(), Name = string.Empty });

            while (tt.Count > 0)
            {
                Helper tHelper = tt.Pop();
                Type t = tHelper.Type;
                ProcessTheResults( add to a list, whatever ...);
                if (t.IsValueType || t == typeof(string))
                    continue;

                if (t.IsGenericType)
                {
                    foreach (var arg in t.GetGenericArguments())
                        tt.Push(new Helper { Type = arg, Name = string.Empty });
                    continue;

                }

                foreach (var propertyInfo in t.GetProperties())
                {
                    tt.Push(new Helper { Type = propertyInfo.PropertyType, Name = propertyInfo.Name });

                }
            }

            return result;
        }
我唯一不明白的是,我是否有以下结构:

  public class SimpleClass
{
    public int IdSimpleClass { get; set; }
    public string NameSimpleClass { get; set; }
}

public class NestingClass
{
    public string Address { get; set; }
    public List<SimpleClass> SimpleClass { get; set; }
}

public class SuperNestingClass
{
    public string SomeId { get; set; }
    public string AnotherId { get; set; }
    public Guid Guid { get; set; }
    public List<NestingClass> NestingClass { get; set; }
}
公共类SimpleClass
{
public int IdSimpleClass{get;set;}
公共字符串NameSimpleClass{get;set;}
}
公共类嵌套类
{
公共字符串地址{get;set;}
公共列表SimpleClass{get;set;}
}
公共类超级嵌套类
{
公共字符串SomeId{get;set;}
公共字符串AnotherId{get;set;}
公共Guid Guid{get;set;}
公共列表嵌套类{get;set;}
}
控制台中的结果是
正如您所能看到的,地址名称应该是AddressNestingClass,而不仅仅是Address,因为Address属性是NestingClass的成员,而不是SuperNestingClass的成员。知道为什么会发生这种情况吗?

我认为算法与

  SomeStructure GetFromObject(object o)
    { var tt = new Stack<Helper>();
            types.Push(new Helper { Type = o.GetType(), Name = string.Empty });

            while (tt.Count > 0)
            {
                Helper tHelper = tt.Pop();
                Type t = tHelper.Type;
                ProcessTheResults( add to a list, whatever ...);
                if (t.IsValueType || t == typeof(string))
                    continue;

                if (t.IsGenericType)
                {
                    foreach (var arg in t.GetGenericArguments())
                        tt.Push(new Helper { Type = arg, Name = string.Empty });
                    continue;

                }

                foreach (var propertyInfo in t.GetProperties())
                {
                    tt.Push(new Helper { Type = propertyInfo.PropertyType, Name = propertyInfo.Name });

                }
            }

            return result;
        }
我唯一不明白的是,我是否有以下结构:

  public class SimpleClass
{
    public int IdSimpleClass { get; set; }
    public string NameSimpleClass { get; set; }
}

public class NestingClass
{
    public string Address { get; set; }
    public List<SimpleClass> SimpleClass { get; set; }
}

public class SuperNestingClass
{
    public string SomeId { get; set; }
    public string AnotherId { get; set; }
    public Guid Guid { get; set; }
    public List<NestingClass> NestingClass { get; set; }
}
公共类SimpleClass
{
public int IdSimpleClass{get;set;}
公共字符串NameSimpleClass{get;set;}
}
公共类嵌套类
{
公共字符串地址{get;set;}
公共列表SimpleClass{get;set;}
}
公共类超级嵌套类
{
公共字符串SomeId{get;set;}
公共字符串AnotherId{get;set;}
公共Guid Guid{get;set;}
公共列表嵌套类{get;set;}
}
控制台中的结果是
正如您所能看到的,地址名称应该是AddressNestingClass,而不仅仅是Address,因为Address属性是NestingClass的成员,而不是SuperNestingClass的成员。你知道为什么会发生这种情况吗?

你所说的“堆栈方法”是什么意思?你已经在做这个深度优先,把问题看成一棵树。你能发布你的预期输出吗?你所说的“堆栈方法”是什么意思?你已经在做这个深度优先,把问题看成一棵树。你能发布你的预期输出吗?