【发布时间】:2010-09-29 05:16:09
【问题描述】:
我想要命名空间中的所有 Method-ClassName
就像我有 system.windows.Forms
当我们在 Visual Studio 中安装 system.windows.Forms 时。它会建议所有相关方法、类、枚举的框
我需要获取相同的,我如何在 C# 中做到这一点
【问题讨论】:
标签: .net class reflection namespaces
我想要命名空间中的所有 Method-ClassName
就像我有 system.windows.Forms
当我们在 Visual Studio 中安装 system.windows.Forms 时。它会建议所有相关方法、类、枚举的框
我需要获取相同的,我如何在 C# 中做到这一点
【问题讨论】:
标签: .net class reflection namespaces
首先,命名空间中没有方法——只有类型。
要从特定程序集中的一个命名空间中获取所有类型,您可以使用(假设 .NET 3.5 用于 LINQ 位)Assembly.GetTypes:
var types = assembly.GetTypes().Where(type => type.Namespace == desiredNamespace);
但是,类型可以分布在多个程序集中。
一旦你有了一个类型,你就可以使用Type.GetMethods 来检索可用的方法。使用适当的 BindingFlags 来获取静态/实例、公共/非公共方法等。
如果这没有帮助,请澄清问题。
【讨论】:
这种功能称为“反射”。
例如http://www.codersource.net/published/view/291/reflection_in.aspx(我通过谷歌搜索“反射”和“C#”发现)提到了您从 C# 调用的相关 .NET API 方法。
【讨论】: