搜索class中的所有字符串类型成员(包括搜索class类型中的属性)C#
Search all the string type member in a class(including searching the property in class type) C#
我试图从 class 中找出所有 "string" 类型的属性,但想知道如果有 class 类型的 属性 我该怎么做这个class.
以下代码显示了目标 class 和我的解决方案。
public class Credit_Card
{
public string brand { get; set; }
public string billing_phone { get; set; }
public Expiration expiration { get; set; }
}
public class Expiration
{
public string month { get; set; }
}
class program
{
static void Main(string[] args)
{
foreach (PropertyInfo prop in typeof(Credit_Card).GetProperties())
{
if(prop.PropertyType == typeof(string))
{
Console.WriteLine(prop.Name);
}
}
Console.ReadLine();
}
}
我的"Main"方法只能显示Credit_Card类型的"brand"和"billing_phone"属性,但在过期时错过了"month"属性class.
有什么方法可以在 Credit_Card class 中进行递归搜索吗?
您应该能够创建一个递归调用自身的方法,并将要搜索的类型作为参数:
public void OutputStringProperties(Type type)
{
foreach (PropertyInfo prop in type.GetProperties())
{
if(prop.PropertyType == typeof(string))
{
Console.WriteLine(prop.Name);
}
else
{
OutputStringProperties(prop.PropertyType);
}
}
}
那么你的初始调用只是调用它的起点:
OutputStringProperties(typeof(Credit_Card));
但是,请记住,如果您有循环依赖项,这将导致堆栈溢出,除非您首先修改它以跟踪它已经检查了哪些类型。
public static void Main (string[] args)
{
foreach(PropertyInfo prop in GetStringProperties(typeof(Credit_Card)))
Console.WriteLine(prop.Name);
Console.ReadLine();
}
public static IEnumerable<PropertyInfo> GetStringProperties(Type type)
{
return GetStringProperties (type, new HashSet<Type> ());
}
public static IEnumerable<PropertyInfo> GetStringProperties(Type type, HashSet<Type> alreadySeen)
{
foreach(var prop in type.GetProperties())
{
var propType = prop.PropertyType;
if (propType == typeof(string))
yield return prop;
else if(alreadySeen.Add(propType))
foreach(var indirectProp in GetStringProperties(propType, alreadySeen))
yield return indirectProp;
}
}
捕获我们已经处理过的类型很重要,否则您很容易陷入无限循环,但需要一点怜悯,因为这采用递归方法,它会崩溃 WhosebugException
而不是永远挂起就像迭代等价物一样。 (唯一比抛出异常更糟糕的情况是不抛出异常)。
我试图从 class 中找出所有 "string" 类型的属性,但想知道如果有 class 类型的 属性 我该怎么做这个class.
以下代码显示了目标 class 和我的解决方案。
public class Credit_Card
{
public string brand { get; set; }
public string billing_phone { get; set; }
public Expiration expiration { get; set; }
}
public class Expiration
{
public string month { get; set; }
}
class program
{
static void Main(string[] args)
{
foreach (PropertyInfo prop in typeof(Credit_Card).GetProperties())
{
if(prop.PropertyType == typeof(string))
{
Console.WriteLine(prop.Name);
}
}
Console.ReadLine();
}
}
我的"Main"方法只能显示Credit_Card类型的"brand"和"billing_phone"属性,但在过期时错过了"month"属性class.
有什么方法可以在 Credit_Card class 中进行递归搜索吗?
您应该能够创建一个递归调用自身的方法,并将要搜索的类型作为参数:
public void OutputStringProperties(Type type)
{
foreach (PropertyInfo prop in type.GetProperties())
{
if(prop.PropertyType == typeof(string))
{
Console.WriteLine(prop.Name);
}
else
{
OutputStringProperties(prop.PropertyType);
}
}
}
那么你的初始调用只是调用它的起点:
OutputStringProperties(typeof(Credit_Card));
但是,请记住,如果您有循环依赖项,这将导致堆栈溢出,除非您首先修改它以跟踪它已经检查了哪些类型。
public static void Main (string[] args)
{
foreach(PropertyInfo prop in GetStringProperties(typeof(Credit_Card)))
Console.WriteLine(prop.Name);
Console.ReadLine();
}
public static IEnumerable<PropertyInfo> GetStringProperties(Type type)
{
return GetStringProperties (type, new HashSet<Type> ());
}
public static IEnumerable<PropertyInfo> GetStringProperties(Type type, HashSet<Type> alreadySeen)
{
foreach(var prop in type.GetProperties())
{
var propType = prop.PropertyType;
if (propType == typeof(string))
yield return prop;
else if(alreadySeen.Add(propType))
foreach(var indirectProp in GetStringProperties(propType, alreadySeen))
yield return indirectProp;
}
}
捕获我们已经处理过的类型很重要,否则您很容易陷入无限循环,但需要一点怜悯,因为这采用递归方法,它会崩溃 WhosebugException
而不是永远挂起就像迭代等价物一样。 (唯一比抛出异常更糟糕的情况是不抛出异常)。