[英]How to get all Guid properties and their values with the name "Id" from class?
我有一個具有嵌套對象的類。 我想使用名稱“Id”獲取所有 Guid 屬性及其值。 這是我的課程的簡單版本:
public class Main
{
public Guid Id { get; set; }
public ICollection<Child> Children { get; set; }
}
public class Child
{
public Guid Id { get; set; }
public ICollection<GrandChild> GrandChildren { get; set; }
}
public class GrandChild
{
public Guid Id { get; set; }
}
我將擁有 Main 類對象,並希望檢索所有 Guid 屬性及其所有嵌套等的值,以進行進一步的邏輯。我需要使用反射。 主類就是一個例子。 我可以在運行時擁有泛型類型。 有什么建議?
在這種情況下,我使用字典來定義如何查找后代。 但這可以通過搜索泛型類型直接完成。 因此,這個不使用匿名類型。 然而,結果是一樣的,你必須遞歸遍歷層次結構。
private static Dictionary<Type, Func<Type>> childTypeAccessor = new Dictionary<Type, Func<Type>>()
{
{ typeof(Main), () => typeof(Child) },
{ typeof(Child), () => typeof(GrandChild) }
};
public class TypeDef
{
public Type Type {get; set;}
public string PropertyName { get; set; }
}
private IEnumerable<TypeDef> SearchTypeProperties(Type type, Type searchType)
{
var properties = type.GetProperties().Where(p => p.PropertyType == searchType).Select(p =>
new TypeDef { Type = type, PropertyName = p.Name }
);
if (childTypeAccessor.ContainsKey(type))
{
var subproperties = SearchTypeProperties(childTypeAccessor[type](), searchType);
return properties.Concat(subproperties).ToList();
}
return properties.ToList();
}
使用遞歸函數遍歷層次結構。 像這樣的東西:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp34
{
public class Main
{
public Guid Id { get; set; }
public ICollection<Child> Children { get; set; }
}
public class Child
{
public Guid Id { get; set; }
public ICollection<GrandChild> GrandChildren { get; set; }
}
public class GrandChild
{
public Guid Id { get; set; }
}
class Program
{
static void Main(string[] args)
{
var main = new Main()
{
Children = new List<Child>()
{
new Child()
{
Id = Guid.NewGuid(),
GrandChildren = new List<GrandChild>()
{
new GrandChild() {Id=Guid.NewGuid() } ,
new GrandChild() {Id=Guid.NewGuid() } ,
new GrandChild() {Id=Guid.NewGuid() } ,
new GrandChild() {Id=Guid.NewGuid() }
}
},
new Child()
{
Id = Guid.NewGuid(),
GrandChildren = new List<GrandChild>()
{
new GrandChild() {Id=Guid.NewGuid() } ,
new GrandChild() {Id=Guid.NewGuid() } ,
new GrandChild() {Id=Guid.NewGuid() } ,
new GrandChild() {Id=Guid.NewGuid() }
}
}
}
};
foreach (var val in GetIds(main))
{
Console.WriteLine($"{val.Item1.GetType().Name} Id {val.Item2}");
}
}
internal static IEnumerable<(object,Guid)> GetIds(Object o)
{
var t = o.GetType();
foreach (var pId in t.GetProperties().Where( p => p.Name == "Id" && p.PropertyType == typeof(Guid)))
{
yield return (o, (Guid)pId.GetValue(o));
}
foreach (var pChild in t.GetProperties().Where(p => p.PropertyType.IsGenericType && p.PropertyType.GetGenericTypeDefinition() == typeof(ICollection<>)))
{
var children = (IEnumerable)pChild.GetValue(o);
foreach (var child in children)
{
foreach (var val in GetIds(child))
{
yield return val;
}
}
}
}
}
}
產出
Main Id 00000000-0000-0000-0000-000000000000
Child Id 971310cb-b1f6-4d27-91de-f1f4edcb1a3b
GrandChild Id 160ce573-e8bc-4cd6-9569-86d82e50d531
GrandChild Id 3524d68e-cbba-453f-a62c-b7530923f937
GrandChild Id 205ef5ac-c97c-45fc-b07a-aead02bd047e
GrandChild Id f79aff79-d3ef-46e0-a637-089bb4b8d9ec
Child Id dd789259-b1cb-4d12-b109-6318afbbc33f
GrandChild Id e909ef22-abf7-4aa7-bf07-8e82ea941203
GrandChild Id 16243269-1994-4d6e-b18e-1661c7e08867
GrandChild Id fba632c9-0450-4d42-86e6-b2b64479bc52
GrandChild Id 2ccb1f39-f046-41fe-b9db-68492c0d9010
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.