代码之家  ›  专栏  ›  技术社区  ›  Mark P Neyer

给定一个C#类型,获取它的基类和实现的接口

  •  5
  • Mark P Neyer  · 技术社区  · 16 年前

    我正在用C#开发一个游戏引擎。我正在上的课叫 CEntityRegistry ,它的工作是跟踪 CEntity 在游戏中。我的目标是能够查询 中心区 使用给定的类型,并获取每个类型的列表 厘米 那种类型的。

    因此,我想做的是维护一张地图:

    private IDictionary<Type, HashSet<CEntity>> m_TypeToEntitySet;
    

    并据此更新注册表:

    private void m_UpdateEntityList()
            {
                foreach (CEntity theEntity in m_EntitiesToRemove.dequeueAll())
                {
                    foreach (HashSet<CEntity> set in m_TypeToEntitySet.Values)
                    {
                        if (set.Contains(theEntity))
                            set.Remove(theEntity);
                    }
                }
                foreach (CEntity theEntity in m_EntitiesToAdd.dequeueAll())
                {
                    Type entityType = theEntity.GetType();
                    foreach (Type baseClass in entityType.GetAllBaseClassesAndInterfaces())
                      m_TypeToEntitySet[baseClass].Add(theEntity);
    
                }
            }
    

    我的问题是没有函数 Type.GetAllBaseClassesAndInterfaces -我该怎么写呢?

    4 回复  |  直到 16 年前
        1
  •  8
  •   Nate C-K    11 年前

    https://msdn.microsoft.com/en-us/library/system.type.aspx

    所以实际上,它几乎有 Type.GetAllBaseClassesAndInterfaces ,但你必须打两个电话,而不是一个。

        2
  •  19
  •   SLaks    16 年前

    您可以编写如下扩展方法:

    public static IEnumerable<Type> GetBaseTypes(this Type type) {
        if(type.BaseType == null) return type.GetInterfaces();
    
        return Enumerable.Repeat(type.BaseType, 1)
                         .Concat(type.GetInterfaces())
                         .Concat(type.GetInterfaces().SelectMany<Type, Type>(GetBaseTypes))
                         .Concat(type.BaseType.GetBaseTypes());
    }
    
        3
  •  9
  •   dadhi    13 年前

    基于SLaks的一个更精确的答案是:

    public static IEnumerable<Type> GetBaseClassesAndInterfaces(this Type type)
    {
        return type.BaseType == typeof(object) 
            ? type.GetInterfaces()
            : Enumerable
                .Repeat(type.BaseType, 1)
                .Concat(type.GetInterfaces())
                .Concat(type.BaseType.GetBaseClassesAndInterfaces())
                .Distinct();
    }
    
        4
  •  3
  •   N73k    7 年前

    在这里前面的答案有问题。

        public static IEnumerable<Type> GetBaseTypes(this Type type, bool bIncludeInterfaces = false)
        {
            if (type == null)
                yield break;
    
            for (var nextType = type.BaseType; nextType != null; nextType = nextType.BaseType)
                yield return nextType;
    
            if (!bIncludeInterfaces)
                yield break;
    
            foreach (var i in type.GetInterfaces())
                yield return i;
        }
    
        5
  •  0
  •   CesarGon    16 年前

    使用此代码:

    Func<Type, List<Type>> f = ty =>
    {
        var tysReturn = new List<Type>();
        if (ty.BaseType != null)
        {
            tysReturn.Add(ty.BaseType);
        }
        tysReturn.AddRange(ty.GetInterfaces());
        return tysReturn;
    };
    

    功能 f 将获取一个类型并返回其基类型和接口的列表。

    希望能有帮助。