Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在所有程序集中查找类型_C#_.net_Asp.net_Reflection_Types - Fatal编程技术网

C# 在所有程序集中查找类型

C# 在所有程序集中查找类型,c#,.net,asp.net,reflection,types,C#,.net,Asp.net,Reflection,Types,我需要在网站或windows应用程序中的所有程序集中查找特定类型,有没有简单的方法?类似于ASP.NET MVC的控制器工厂在控制器的所有程序集中的外观 谢谢。实现这一点需要两个步骤: AppDomain.CurrentDomain.GetAssemblys()提供当前应用程序域中加载的所有程序集 Assembly类提供了一个GetTypes()方法来检索该特定程序集中的所有类型 因此,您的代码可能如下所示: foreach (Assembly a in AppDomain.CurrentD

我需要在网站或windows应用程序中的所有程序集中查找特定类型,有没有简单的方法?类似于ASP.NET MVC的控制器工厂在控制器的所有程序集中的外观


谢谢。

实现这一点需要两个步骤:

  • AppDomain.CurrentDomain.GetAssemblys()
    提供当前应用程序域中加载的所有程序集
  • Assembly
    类提供了一个
    GetTypes()
    方法来检索该特定程序集中的所有类型
因此,您的代码可能如下所示:

foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies())
{
    foreach (Type t in a.GetTypes())
    {
        // ... do something with 't' ...
    }
}
要查找特定类型(例如,实现给定接口、从公共祖先继承或其他类型),您必须过滤掉结果。如果您需要在应用程序中的多个位置执行此操作,最好构建一个提供不同选项的帮助器类。例如,我常用名称空间前缀过滤器、接口实现过滤器和继承过滤器

有关详细文档,请查看MSDN和。

易用Linq:

IEnumerable<Type> types =
            from a in AppDomain.CurrentDomain.GetAssemblies()
            from t in a.GetTypes()
            select t;

foreach(Type t in types)
{
    ...
}
IEnumerable类型=
从AppDomain.CurrentDomain.GetAssemblys()中的
从a.GetTypes()中的t开始
选择t;
foreach(类型中的类型t)
{
...
}

LINQ解决方案,检查组件是否动态:

/// <summary>
/// Looks in all loaded assemblies for the given type.
/// </summary>
/// <param name="fullName">
/// The full name of the type.
/// </param>
/// <returns>
/// The <see cref="Type"/> found; null if not found.
/// </returns>
private static Type FindType(string fullName)
{
    return
        AppDomain.CurrentDomain.GetAssemblies()
            .Where(a => !a.IsDynamic)
            .SelectMany(a => a.GetTypes())
            .FirstOrDefault(t => t.FullName.Equals(fullName));
}
//
///查找给定类型的所有已加载程序集。
/// 
/// 
///类型的全名。
/// 
/// 
///发现的;如果未找到,则为null。
/// 
私有静态类型FindType(字符串全名)
{
返回
AppDomain.CurrentDomain.GetAssemblys()
.其中(a=>!a.IsDynamic)
.SelectMany(a=>a.GetTypes())
.FirstOrDefault(t=>t.FullName.Equals(FullName));
}

通常,您只对从外部可见的程序集感兴趣。因此,您需要调用GetExportedTypes(),但除此之外,还可以抛出ReflectionTypeLoadException。下面的代码处理这些情况

public static IEnumerable<Type> FindTypes(Func<Type, bool> predicate)
{
    if (predicate == null)
        throw new ArgumentNullException(nameof(predicate));

    foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
    {
        if (!assembly.IsDynamic)
        {
            Type[] exportedTypes = null;
            try
            {
                exportedTypes = assembly.GetExportedTypes();
            }
            catch (ReflectionTypeLoadException e)
            {
                exportedTypes = e.Types;
            }

            if (exportedTypes != null)
            {
                foreach (var type in exportedTypes)
                {
                    if (predicate(type))
                        yield return type;
                }
            }
        }
    }
}
公共静态IEnumerable FindTypes(Func谓词)
{
if(谓词==null)
抛出新ArgumentNullException(nameof(谓词));
foreach(AppDomain.CurrentDomain.GetAssemblys()中的变量程序集)
{
如果(!assembly.IsDynamic)
{
类型[]exportedTypes=null;
尝试
{
exportedTypes=assembly.GetExportedTypes();
}
捕获(ReflectionTypeLoadException e)
{
exportedTypes=e.类型;
}
if(exportedTypes!=null)
{
foreach(exportedTypes中的变量类型)
{
if(谓词(类型))
收益型;
}
}
}
}
}

当然,如果要对这些类型和程序集进行过滤,您应该使用LINQ,对吗?;)@Niall Connaughton这是个人偏好的问题。我发现这样做会导致异常,结果是因为我的应用程序正在生成动态程序集,这是一个需要注意的问题。您可以识别动态程序集并像这样跳过它们-如果它确实导致问题请注意,程序集仅在需要时加载,即首次使用时加载。实际上,我仍然会收到
无法加载类型异常。