我有一个函数接收一个类型并返回true或false。 我需要找出哪些是所有类型的特定名称空间,该函数将为他们返回true。 谢谢。有没有办法在foreach循环中的所有类型的命名空间?
0
A
回答
10
下面是获取命名空间中的所有类的函数:
using System.Reflection;
using System.Collections.Generic;
/// <summary>
/// Method to populate a list with all the class
/// in the namespace provided by the user
/// </summary>
/// <param name="nameSpace">The namespace the user wants searched</param>
/// <returns></returns>
static List<string> GetAllClasses(string nameSpace)
{
//create an Assembly and use its GetExecutingAssembly Method
//http://msdn2.microsoft.com/en-us/library/system.reflection.assembly.getexecutingassembly.aspx
Assembly asm = Assembly.GetExecutingAssembly();
//create a list for the namespaces
List<string> namespaceList = new List<string>();
//create a list that will hold all the classes
//the suplied namespace is executing
List<string> returnList = new List<string>();
//loop through all the "Types" in the Assembly using
//the GetType method:
//http://msdn2.microsoft.com/en-us/library/system.reflection.assembly.gettypes.aspx
foreach (Type type in asm.GetTypes())
{
if (type.Namespace == nameSpace)
namespaceList.Add(type.Name);
}
foreach (String className in namespaceList)
returnList.Add(className);
return returnList;
}
[更新]
这里有一个更紧凑的方式做到这一点,但是这需要.NET 3.5(从here ):
public static IEnumerable<Type> GetTypesFromNamespace(Assembly assembly,
String desiredNamepace)
{
return assembly.GetTypes()
.Where(type => type.Namespace == desiredNamespace);
}
1
System.Reflection.Assembly.GetTypes()
:获取此程序定义的所有类型*
应该做的工作=)
1
添加你要搜索和执行MyFunction的方法的组件:
class Program
{
static void Main(string[] args)
{
List<Assembly> assembliesToSearch = new List<Assembly>();
// Add the assemblies that you want to search here:
assembliesToSearch.Add(Assembly.Load("mscorlib")); // sample assembly, you might need Assembly.GetExecutingAssembly
foreach (Assembly assembly in assembliesToSearch)
{
var typesForThatTheFunctionReturnedTrue = assembly.GetTypes().Where(type => MyFunction(type) == true);
foreach (Type type in typesForThatTheFunctionReturnedTrue)
{
Console.WriteLine(type.Name);
}
}
}
static bool MyFunction(Type t)
{
// Put your logic here
return true;
}
}
相关问题
- 1. 有没有办法告诉Doxygen忽略(所有)命名空间?
- 2. 有没有办法使用红宝石命名空间的ctags?
- 3. 有没有办法断言当前的命名空间?
- 4. 有没有办法在jQuery中删除所有名称空间的绑定?
- 5. 有没有办法强制php在当前命名空间中使用类?
- 6. 查找所有命名空间中的所有模型
- 7. 有没有办法循环并执行Python类中的所有函数?
- 8. C++:有没有办法创建模板命名空间?
- 9. 在web.config中没有命名空间的注册类型
- 10. 有没有办法在foreach循环中创建一个数组中的对象
- 11. 有没有办法绕过Python中的命名空间/模块名称?
- 12. 有没有办法修改foreach循环变量?
- 13. 有没有办法通过命名空间在Fluent NHibernate自动映射中忽略基类型/类?
- 14. 有没有办法在字段集中的所有字段循环?
- 15. 在pylongs中,有没有办法循环所有的控制器和操作?
- 16. 有没有办法在VB中转义根名称空间?
- 17. 有没有办法在类中获得某种类型的所有变量?
- 18. 有没有办法在foreach循环中产生一个项目一次?
- 19. ::没有命名空间
- 20. XElement.Descendants没有命名空间
- 21. 没有命名空间
- 22. 没有命名空间
- 23. 没有命名空间
- 24. 使用Ninject.InSingletonScope()在定义的命名空间中的所有类
- 25. 具有空命名空间的AC#类
- 26. 有没有办法重新命名由D3创建的类?
- 27. 有没有办法刻意改变当前的Python命名空间?
- 28. 循环中的XPath表达式有命名空间
- 29. 有没有办法使用任意类型的值作为R中的环境或命名列表中的键?
- 30. 有没有办法让CodeDom在命名空间之前放置语句
不严格相关foreach循环,但类似的问题:HTTP://计算器.com/questions/79693/getting-all-types-in-a-namespace-via-reflection – Utaal 2009-07-16 10:01:58