[英]parse type hierarchy in assembly
请考虑程序集中的以下类型:BusinessPartnerList,BusinessPartner,PrivateData,CompanyData,AddressList,地址
Type BusinessPartnerList
{
BusinessPartner[]
}
Type BusinessPartner
{
PrivateData
CompanyData
AddressList
}
Type PrivateData
{
System.String FirstName
System.String SurName
}
Type PrivateData
{
System.String CompanName1
System.String CompanName2
}
Type AddressList
{
Address[]
}
我想通用解析类型层次结构,并在树中表示它们,例如简单节点
BusinessPartnerList [] BusinessPartner PrivateData CompanyData AddressList []地址
做这个的最好方式是什么?
不幸的是,您没有对示例数据使用正确的C#语法。 所以我必须做一些假设:
Type
实际上是class
(或struct
)。
类型的内容( BusinessPartner
, PrivateData
, CompanyData
等)表示某些公共属性的类型。
要解析类型层次结构,可以使用反射。 查找给定类型的所有公共属性,然后返回其类型。 由于只需要类型,因此可以使用仅包含不同类型的HashSet
:
public static HashSet<Type> GetPropertyTypes(Type type)
{
return new HashSet<Type>(type.GetProperties(BindingFlags.Instance | BindingFlags.Public)
.Select(prop => prop.PropertyType));
}
但是,似乎您不想获取有关数组的信息,而是获取数组元素的类型。 列表也是如此。 因此,如果类型实现IEnumerable<T>
您希望获取有关类型T
:
private static Type GetElementType(Type type)
{
Type enumerableType = type.GetInterfaces().FirstOrDefault(IsGenericEnumerable);
if (enumerableType != null)
{
Type[] genericArguments = enumerableType.GetGenericArguments();
return genericArguments[0];
}
// return 'object' for a non-generic IEnumerable
return typeof(IEnumerable).IsAssignableFrom(type) ? typeof(object) : type;
}
private static bool IsGenericEnumerable(Type type)
{
return type.IsGenericType &&
type.GetGenericTypeDefinition() == typeof(IEnumerable<>);
}
请注意,对于类型System.String
它将返回char
因为string
实现了IEnumerable<char>
(我将在后面进行处理)。
.NET框架没有可立即使用的树形结构。 因此,您需要自己实施:
public class Node<T>
{
public Node(T value, IEnumerable<Node<T>> children)
{
Value = value;
Children = children.ToList();
}
public T Value
{
get;
private set;
}
public List<Node<T>> Children
{
get;
private set;
}
}
这是一个非常基本的实现,仅用于演示目的。
现在, GetPropertyTypes
方法可以返回Node<Type>
而不是返回List<Type>
,而应将其重命名为CreateTypeNode
:
public static Node<Type> CreateTypeNode(Type type)
{
var children = type.GetProperties(BindingFlags.Instance | BindingFlags.Public)
.Select(prop => GetElementType(prop.PropertyType))
.Select(CreateTypeNode);
return new Node<Type>(type, children);
}
此方法使用递归为给定类型创建完整树。
仍然存在一个问题:如果类型A
引用类型B
,反之亦然呢? 这将最终导致无限递归循环。 而且:如果已经访问过某种类型,则无需再次执行该操作。
我们需要的是已访问类型的缓存。 如果缓存中有类型,我们将使用缓存中的信息:
private static readonly Dictionary<Type, Node<Type>> _visitedTypes = new Dictionary<Type, Node<Type>>();
public static Node<Type> CreateTypeNode(Type type)
{
Node<Type> node;
if (_visitedTypes.TryGetValue(type, out node))
{
return node;
}
// add the key to the cache to prevent infinite recursion; the value will be set later
// if this type will be found again in a recursive call CreateTypeNode returns null
// (null will be filtered out then)
_visitedTypes.Add(type, null);
var properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);
var types = new HashSet<Type>(properties.Select(prop => GetElementType(prop.PropertyType)));
var children = types.Select(CreateTypeNode).Where(n => n != null);
node = new Node<Type>(type, children);
_visitedTypes[type] = node;
return node;
}
我不希望将string
类型报告为char
(因为string
实现了IEnumerable<char>
),您可以在第一次调用GetOrCreateTypeNode
之前将string
的节点添加到缓存中:
_visitedTypes.Add(typeof(string), new Node<Type>(typeof(string), new List<Node<Type>>()));
然后在GetElementType
方法中检查缓存:
private static Type GetElementType(Type type)
{
if (_visitedTypes.ContainsKey(type))
{
return type;
}
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.