Find Types in All Assemblies

后端 未结 4 1012
南笙
南笙 2020-12-05 06:27

I need to look for specific types in all assemblies in a web site or windows app, is there an easy way to do this? Like how the controller factory for ASP.NET MVC looks acr

4条回答
  •  悲&欢浪女
    2020-12-05 06:40

    LINQ solution with check to see if the assembly is dynamic:

    /// 
    /// Looks in all loaded assemblies for the given type.
    /// 
    /// 
    /// The full name of the type.
    /// 
    /// 
    /// The  found; null if not found.
    /// 
    private static Type FindType(string fullName)
    {
        return
            AppDomain.CurrentDomain.GetAssemblies()
                .Where(a => !a.IsDynamic)
                .SelectMany(a => a.GetTypes())
                .FirstOrDefault(t => t.FullName.Equals(fullName));
    }
    

提交回复
热议问题