Download multiple dlls from bin folder using reflection

I am writing an application where I need a collection of all commandIds. They are present in several 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 identifiers from all DLLs. The Bin folder contains files other than DLLs.

-one
source share
2 answers

It looks like you just need to iterate over the dlls in the directory.

You also need to make sure that you are not downloading an already downloaded assembly.

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; } } 
+2
source

Try to get all the catalog files using Directory.GetFiles . After that, according to http://msdn.microsoft.com/en-us/library/ms173100.aspx , define the assemblies and use your own method for each assembly.

0
source

Source: https://habr.com/ru/post/1014616/