使用反射从bin文件夹加载多个dll [英] Load multiple dlls from bin folder using reflection

查看:234
本文介绍了使用反射从bin文件夹加载多个dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个应用程序,在这里我需要一个所有命令的集合。这些存在于多个DLL中。我可以访问bin文件夹。



我使用了反射功能,一次可以为一个dll执行此操作。

 code> Assembly a = System.Reflection.Assembly.LoadFrom(@T:\Bin\Commands.dll); 

IEnumerable< Type> types = Helper.GetLoadableTypes(a);
foreach(类型类型)
{
FieldInfo ID = type.GetField(ID);

if(ID!= null)
{
string fromValue =(ID.GetRawConstantValue()ToString());

ListFromCSFiles.Add(fromValue);
}
}

我的问题是我需要获取所有的ID ,从所有的dll。 Bin文件夹还包含除dll之外的文件。

解决方案

听起来你只需要在目录中循环访问dll。



您还需要确保您没有加载已加载的程序集。



例如:

  string bin =c:\YourBin; 

DirectoryInfo oDirectoryInfo = new DirectoryInfo(bin);

//检查目录是否存在
if(oDirectoryInfo.Exists)
{
// Foreach Assembly with dll作为扩展名
foreach(FileInfo oFileInfo在oDirectoryInfo.GetFiles(* .dll,SearchOption.AllDirectories))
{

Assembly tempAssembly = null;

//加载程序集之前,请检查所有当前加载的程序集,以防止已加载
//已经加载为另一个程序集的引用
//加载程序集两次可能会导致重大问题
foreach(Assembly LoadAssembly in AppDomain.CurrentDomain.GetAssemblies())
{
//检查程序集是否动态生成,因为我们对这些
不感兴趣if(loadedAssembly.ManifestModule.GetType()。Namespace!=System.Reflection.Emit)
{
//获取加载的汇编文件名
string sLoadedFilename =
loadedAssembly。 CodeBase.Substring(loadedAssembly.CodeBase.LastIndexOf('/')+ 1);

//如果文件名匹配,请将程序集设置为已加载的程序集
if(sLoadedFilename.ToUpper()== oFileInfo.Name.ToUpper())
{
tempAssembly = loadedAssembly;
break;
}
}
}

//如果程序集不是aleady加载的,手动加载
if(tempAssembly == null)
{
tempAssembly = Assembly.LoadFile(oFileInfo.FullName);
}

装配a = tempAssembly;
}

}


I am writing an application where in I need a collection of all commandIds. These are present across multiple dlls. I have access to the bin folder.

I used reflection , and was able to do this for one dll at a time

Assembly a = System.Reflection.Assembly.LoadFrom(@"T:\Bin\Commands.dll");

IEnumerable<Type> types = Helper.GetLoadableTypes(a);
foreach (Type type in types)
{
    FieldInfo ID = type.GetField("ID");

    if (ID != null)
    {
        string fromValue = (ID.GetRawConstantValue().ToString());

        ListFromCSFiles.Add(fromValue);
    }
}

My problem is that I need to get all the IDs, from all dlls. The Bin folder contains files other than dlls as well.

解决方案

Sounds like you just need to loop through dlls in the directory.

You also need to make sure you are not loading an assembly that is already loaded.

eg:

  string bin = "c:\YourBin";

    DirectoryInfo oDirectoryInfo = new DirectoryInfo( bin );

    //Check the directory exists
    if ( oDirectoryInfo.Exists )
    {
       //Foreach Assembly with dll as the extension
       foreach ( FileInfo oFileInfo in oDirectoryInfo.GetFiles( "*.dll", SearchOption.AllDirectories ) )
       {

                        Assembly tempAssembly = null;

                        //Before loading the assembly, check all current loaded assemblies in case talready loaded
                        //has already been loaded as a reference to another assembly
                        //Loading the assembly twice can cause major issues
                        foreach ( Assembly loadedAssembly in AppDomain.CurrentDomain.GetAssemblies() )
                        {
                            //Check the assembly is not dynamically generated as we are not interested in these
                            if ( loadedAssembly.ManifestModule.GetType().Namespace != "System.Reflection.Emit" )
                            {
                                //Get the loaded assembly filename
                                string sLoadedFilename =
                                    loadedAssembly.CodeBase.Substring( loadedAssembly.CodeBase.LastIndexOf( '/' ) + 1 );

                                //If the filenames match, set the assembly to the one that is already loaded
                                if ( sLoadedFilename.ToUpper() == oFileInfo.Name.ToUpper() )
                                {
                                    tempAssembly = loadedAssembly;
                                    break;
                                }
                            }
                        }

                        //If the assembly is not aleady loaded, load it manually
                        if ( tempAssembly == null )
                        {
                            tempAssembly = Assembly.LoadFile( oFileInfo.FullName );
                        }

                        Assembly a = tempAssembly;
       }

     }

这篇关于使用反射从bin文件夹加载多个dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆