【问题标题】:Get list of C# Classes Names?获取 C# 类名称列表?
【发布时间】:2012-06-10 05:39:04
【问题描述】:

我正在使用 C# windows 窗体实现一个语法高亮,并且想要获取所有名称的列表,我知道这应该手动完成,添加关键字和名称,但我想知道 C# 中是否有一个函数可以帮我做这件事。

【问题讨论】:

  • 什么是“C# 类名称”?你能提供一个例子,也许用伪代码来展示你需要什么?
  • 如DataTable、ViewState、GridView、......等
  • 所以当用户输入类名时,你需要在语法高亮显示类名??你真正想达到什么目标?
  • “我正在使用 C# windows 窗体实现语法高亮” 为什么不编写 Visual Studio 扩展?这比构建自己的语法荧光笔要容易得多。
  • 你能解释一下“CodeCaster”吗,我正在尝试做这样的事情stackoverflow.com/questions/10892884/…

标签: c# winforms


【解决方案1】:
  Assembly asm = Assembly.GetExecutingAssembly();
  List<string> namespaceList = new List<string>();
  List<string> returnList = new List<string>();
  foreach (Type type in asm.GetTypes())
{
    if (type.Namespace == nameSpace)
        namespaceList.Add(type.Name);
}
    foreach (String className in namespaceList)
    returnList.Add(className);
    return returnList;

【讨论】:

    【解决方案2】:

    使用反射。

    Assembly.GetAssembly(typeof(MyClass)).GetTypes()
    

    【讨论】:

      【解决方案3】:

      您可以使用反射获取 C#.NET 中所有程序集的列表。

      例子:

      Assembly mscorlib = typeof(string).Assembly;
      foreach (Type type in mscorlib.GetTypes())
      {
          Console.WriteLine(type.FullName);
      }
      

      【讨论】:

        【解决方案4】:

        您必须使用reflection 来获取 dll 中类的名称。

        // Using Reflection to get information from an Assembly:
        System.Reflection.Assembly o = System.Reflection.Assembly.Load("mscorlib.dll");
        var types = o.GetTypes();
        

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-01
        相关资源
        最近更新 更多