代码之家  ›  专栏  ›  技术社区  ›  AndreyAkinshin

获取c[重复]中命名空间中的类列表

  •  8
  • AndreyAkinshin  · 技术社区  · 15 年前

    这个问题已经有了答案:

    我需要通过程序获得 List 在给定命名空间中的所有类中。我如何才能做到这一点(反思?)在C?

    4 回复  |  直到 15 年前
        1
  •  24
  •   Wai Ha Lee captain-yossarian from Ukraine    8 年前
    var theList = Assembly.GetExecutingAssembly().GetTypes()
                          .Where(t => t.Namespace == "your.name.space")
                          .ToList();
    
        2
  •  6
  •   Wim    15 年前

    没有LINQ:

    尝试:

    Type[] types = Assembly.GetExecutingAssembly().GetTypes();
    List<Type> myTypes = new List<Type>();
    foreach (Type t in types)
    {
      if (t.Namespace=="My.Fancy.Namespace")
        myTypes.Add(t);
    }
    
        3
  •  2
  •   Community c0D3l0g1c    7 年前

    看看这个 How to get all classes within namespace? 提供的答案返回类型为[]的数组。您可以轻松地将其修改为返回列表

        4
  •  1
  •   Pharabus    15 年前

    我只能考虑在assembly中循环遍历类型,以便在正确的命名空间中找到类型。

    public List<Type> GetList()
            {
                List<Type> types = new List<Type>();
                var assembly = Assembly.GetExecutingAssembly();
                foreach (var type in assembly .GetTypes())
                {
                    if (type.Namespace == "Namespace")
                    {
                        types.Add(type);
                    }
                }
                return types;
            }