我如何递归和反射地获取所有可能的字段名称路径的列表?
How do I recursively and reflectively get a list of all possible field name paths?
我正在尝试获取一个字符串集合,为我提供我所有 class 成员的字段名称,以 .
分隔。例如:
public class Apple
{
public Banana MyBanana = new Banana();
public Cranberry MyCranberry = new Cranberry();
}
public class Banana
{
public int MyNumber = 5;
public Cranberry MyCranberry = new Cranberry();
}
public class Cranberry
{
public string MyString = "Hello!";
public int MyOtherNumber = 10;
}
public class Demo
{
public List<string> GetFields(Type Apple)
{
//I can't figure this out
//But it should return a list with these elements:
var result = new List<string>
{
"MyBanana.MyNumber",
"MyBanana.MyCranberry.MyString",
"MyBanana.MyCranberry.MyOtherNumber",
"MyCranberry.MyString",
"MyCranberry.MyOtherNumber"
};
return result;
}
}
我认为需要某种递归和反射,但在编写了几个小时功能失调的代码之后,我需要一些帮助。
我需要这个的原因是因为我正在访问使用这些文件路径作为其各自值的键的第三方代码。
一次失败尝试的示例:
private List<string> GetFields(Type type)
{
var results = new List<string>();
var fields = type.GetFields();
foreach (var field in fields)
{
string fieldName = field.Name;
if (field.ReflectedType.IsValueType)
{
results.Add(fieldName);
}
else
{
results.Add(field.Name + GetFields(field.FieldType));
}
}
return results;
}
我找到了几个相关的问题,但是 none 个完全符合我的问题,我无法自己跳转:Recursively Get Properties & Child Properties Of A Class, https://Whosebug.c"om/questions/6196413/how-to-recursively-print-the-values-of-an-objects-properties-using-reflection, Recursively Get Properties & Child Properties Of An Object, .NET, C#, Reflection: list the fields of a field that, itself, has fields
您需要递归实现:
HashSet<Type> nonRecursiveTypes = new HashSet<Type> { typeof(System.Int32), typeof(System.String) }; // add other simple types here
IEnumerable<string> GetFields(object obj)
{
foreach (var field in obj.GetType().GetFields())
{
if (nonRecursiveTypes.Contains(field.FieldType))
yield return field.Name;
else
foreach (var innerFieldName in GetFields(field.GetValue(obj)))
yield return field.Name + "." + innerFieldName;
}
}
我正在尝试获取一个字符串集合,为我提供我所有 class 成员的字段名称,以 .
分隔。例如:
public class Apple
{
public Banana MyBanana = new Banana();
public Cranberry MyCranberry = new Cranberry();
}
public class Banana
{
public int MyNumber = 5;
public Cranberry MyCranberry = new Cranberry();
}
public class Cranberry
{
public string MyString = "Hello!";
public int MyOtherNumber = 10;
}
public class Demo
{
public List<string> GetFields(Type Apple)
{
//I can't figure this out
//But it should return a list with these elements:
var result = new List<string>
{
"MyBanana.MyNumber",
"MyBanana.MyCranberry.MyString",
"MyBanana.MyCranberry.MyOtherNumber",
"MyCranberry.MyString",
"MyCranberry.MyOtherNumber"
};
return result;
}
}
我认为需要某种递归和反射,但在编写了几个小时功能失调的代码之后,我需要一些帮助。
我需要这个的原因是因为我正在访问使用这些文件路径作为其各自值的键的第三方代码。
一次失败尝试的示例:
private List<string> GetFields(Type type)
{
var results = new List<string>();
var fields = type.GetFields();
foreach (var field in fields)
{
string fieldName = field.Name;
if (field.ReflectedType.IsValueType)
{
results.Add(fieldName);
}
else
{
results.Add(field.Name + GetFields(field.FieldType));
}
}
return results;
}
我找到了几个相关的问题,但是 none 个完全符合我的问题,我无法自己跳转:Recursively Get Properties & Child Properties Of A Class, https://Whosebug.c"om/questions/6196413/how-to-recursively-print-the-values-of-an-objects-properties-using-reflection, Recursively Get Properties & Child Properties Of An Object, .NET, C#, Reflection: list the fields of a field that, itself, has fields
您需要递归实现:
HashSet<Type> nonRecursiveTypes = new HashSet<Type> { typeof(System.Int32), typeof(System.String) }; // add other simple types here
IEnumerable<string> GetFields(object obj)
{
foreach (var field in obj.GetType().GetFields())
{
if (nonRecursiveTypes.Contains(field.FieldType))
yield return field.Name;
else
foreach (var innerFieldName in GetFields(field.GetValue(obj)))
yield return field.Name + "." + innerFieldName;
}
}