如何以编程方式从一堆子类中找到最深入的公共基类型?

Mar*_*eIV 5 c# reflection inheritance subclass

给定一组不同的对象,是否有可能找到他们共享的最具体的基类?

例如,给定具有这些类层次结构的对象......

object -> Vehicle -> WheeledVehicle -> Car -> SportsCar
object -> Vehicle -> WheeledVehicle -> Bus
object -> Vehicle -> WheeledVehicle -> MotorCycle
object -> Vehicle -> WheeledVehicle -> Tricycle -> BigWheel
object -> Vehicle -> WheeledVehicle -> Tricycle -> Green Machine
Run Code Online (Sandbox Code Playgroud)

(为了好玩... http://www.wired.com/autopia/2011/03/green-machine-bike-is-a-big-wheel-for-grownups)

是否可以使用此签名编写函数...

public Type GetCommonBaseType(List<object> objects)
{
    ...
};
Run Code Online (Sandbox Code Playgroud)

...让它返回'WheeledVehicle'?

我的想法是以某种方式为每个对象构建继承链列表,反转它们以便它们都以"对象"开头,然后向下走,检查所有列表中的匹配.如果任何项目不匹配,那么前面的步骤是您最深的匹配基类型.

但是,由于"基地"是内部成员,我不确定如何建立链条.这是你可以使用Reflection确定的东西吗?

Jon*_*Jon 5

您可以使用反射来执行此操作.假设我们从Type实例开始而不是从对象开始 - 这是更通用的,您可以简单地将对象列表转换为其运行时类型列表,以涵盖您提到的用例.

我们的想法是遍历每个输入类型的所有基类,并为每个输入类型增加一个"类型实例计数器".执行此操作后,输入类型的所有公共基础必须使其计数器等于输入类型的数量.哪一个是派生最多的?轻松,选择任何输入类型并开始遍历其类型层次结构,直到找到一个共同的基础; 该类型是派生最多的共同基础.

代码

我将使用此扩展方法:

public static IEnumerable<Type> TypeHierarchy(this Type type)
{
    do
    {
        yield return type;
        type = type.BaseType;
    } while (type != null);
}
Run Code Online (Sandbox Code Playgroud)

然后允许这个实现 - 感谢LINQ,它读起来几乎像英语:

public Type MostDerivedCommonBase(IEnumerable<Type> types)
{
    if (!types.Any()) return null;

    var counts = new Dictionary<Type, int>();
    var total = types.Count();
    foreach(var type in types.SelectMany(t => t.TypeHierarchy()))
    {
        if (!counts.ContainsKey(type))
        {
            counts[type] = 1;
        }
        else
        {
            counts[type]++;
        }
    }

    return types.First().TypeHierarchy().First(t => counts[t] == total);
}
Run Code Online (Sandbox Code Playgroud)

你可以很容易地用这个来锻炼

var types = new[] { typeof(MemoryStream), typeof(FileStream) };
Console.WriteLine(MostDerivedCommonBase(types)); // System.IO.Stream
Run Code Online (Sandbox Code Playgroud)

更新

事后看来,很明显,构建类型字典也可以完全使用LINQ来完成.因此,如果紧凑是你的一杯茶,代码可以简化为:

public Type MostDerivedCommonBase(IEnumerable<Type> types)
{
    if (!types.Any()) return null;

    var counts = types.SelectMany(t => t.TypeHierarchy())
                      .GroupBy(t => t)
                      .ToDictionary(g => g.Key, g => g.Count());

    var total = counts[typeof(object)]; // optimization instead of types.Count()
    return types.First().TypeHierarchy().First(t => counts[t] == total);
}
Run Code Online (Sandbox Code Playgroud)