C# 有没有办法在foreach循环中检查名称空间中的所有类型?
我有一个函数,它接收类型并返回true或false。 我需要找出某个命名空间中的所有类型,该函数将为它们返回true。C# 有没有办法在foreach循环中检查名称空间中的所有类型?,c#,reflection,C#,Reflection,我有一个函数,它接收类型并返回true或false。 我需要找出某个命名空间中的所有类型,该函数将为它们返回true。 谢谢。这里有一个函数可以获取命名空间中的所有类: using System.Reflection; using System.Collections.Generic; /// <summary> /// Method to populate a list with all the class /// in the namespace provided by the
谢谢。这里有一个函数可以获取命名空间中的所有类:
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;
}
使用系统反射;
使用System.Collections.Generic;
///
///方法来填充包含所有类的列表
///在用户提供的命名空间中
///
///用户希望搜索的命名空间
///
静态列表GetAllClass(字符串命名空间)
{
//创建程序集并使用其GetExecutionGassembly方法
//http://msdn2.microsoft.com/en-us/library/system.reflection.assembly.getexecutingassembly.aspx
Assembly asm=Assembly.getExecutionGassembly();
//创建名称空间的列表
列表名称空间列表=新列表();
//创建一个包含所有类的列表
//所提供的命名空间正在执行
List returnList=新列表();
//使用循环遍历程序集中的所有“类型”
//GetType方法:
//http://msdn2.microsoft.com/en-us/library/system.reflection.assembly.gettypes.aspx
foreach(在asm.GetTypes()中键入Type)
{
if(type.Namespace==名称空间)
名称空间列表.Add(type.Name);
}
foreach(名称空间列表中的字符串类名称)
returnList.Add(className);
退货清单;
}
[更新]
这里有一种更简洁的方法,但这需要.Net 3.5(来自):
公共静态IEnumerable GetTypesFromNamespace(程序集,
字符串(需要名称空间)
{
返回assembly.GetTypes()
.Where(type=>type.Namespace==desiredNamespace);
}
System.Reflection.Assembly.GetTypes():获取此程序集中定义的所有类型*
应执行作业=)添加所有要搜索并实现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;
}
}
类程序
{
静态void Main(字符串[]参数)
{
List assembliesToSearch=新列表();
//在此处添加要搜索的程序集:
assembliesToSearch.Add(Assembly.Load(“mscorlib”);//示例程序集,您可能需要Assembly.getExecutionGassembly
foreach(assembliesToSearch中的程序集)
{
var typesForThatTheFunctionReturnedTrue=assembly.GetTypes()。其中(type=>MyFunction(type)==true);
foreach(在函数ReturnedTrue的Types中键入Types)
{
Console.WriteLine(type.Name);
}
}
}
静态布尔函数(t型)
{
//把你的逻辑放在这里
返回true;
}
}
与foreach循环没有严格的关系,但有一个类似的问题:你是说“所有类型”是指任何地方的所有类型,还是仅仅指给定程序集中的所有类型?因为如果你指的是前者,你怎么知道我现在没有在我的机器上编写一个程序集,它的名称空间中有你感兴趣的类型?我指的是运行时可用的所有类型。定义“可用”。假设我把我的集会放在一个公共互联网站点上,但不告诉你。它“可用”吗?您可以免费下载它并在运行时使用它,但是如果您不知道它在那里,那么在您感兴趣的名称空间中搜索它的类型将是很困难的。我之所以问这个问题,是因为到目前为止所有的解决方案都只给出给定程序集中名称空间中的类型,这不是您要问的问题。记住,名称空间比程序集大;可以在一千个不同的程序集中有一个命名空间。
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;
}
}