If you notice my comment on your question, it will be obvious that I do not know exactly how you want or need to do this, but until we have a more detailed description, I can only offer you this in the hope that it fits well for your situation (the key is in the "search" of assemblies):
var className = "System.Boolean"; var assemblyName = "mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"; var assemblies = AppDomain.CurrentDomain.GetAssemblies(); var assembly = (from a in assemblies where a.FullName == assemblyName select a).SingleOrDefault(); if (assembly != null) { System.Runtime.Remoting.ObjectHandle obj = System.Activator.CreateInstance(assemblyName, className); }
Compatible .NET 2.0 Code
Assembly assembly = null; var className = "System.Boolean"; var assemblyName = "mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"; foreach (var a in AppDomain.CurrentDomain.GetAssemblies()) { if (a.FullName == assemblyName) { assembly = a; break; } } if (assembly != null) { System.Runtime.Remoting.ObjectHandle obj = System.Activator.CreateInstance(assemblyName, className); }
If you want to determine if a file exists before downloading (good practice), then, given that you have its name and you know the desired location, just try to find the file with the assembly permission:
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve); var className = "StackOverflowLib.Class1"; var assemblyName = "StackOverflowLib.dll"; var currentAssemblyPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); var obj = Activator.CreateInstance(Path.Combine(currentAssemblyPath, assemblyName), className); static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) { var currentAssemblyPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); if (File.Exists(Path.Combine(currentAssemblyPath, args.Name))) { return Assembly.LoadFile(Path.Combine(currentAssemblyPath, args.Name)); } return null; }
Grant thomas
source share