简体   繁体   中英

C# how to get non-system assemblies

I need to instantiate an object that is defined only in the program assemblies or mscorlib, and not in any other system assemblies. Currently I'm doing this:

Type type;
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (Assembly assembly in assemblies)
{
    string fullName = name + "," + assembly.FullName;
    type = Type.GetType(fullName);
    if (type != null) break;
}
if (type != null) 
    object obj = Activator.CreateInstance(type);

Is there a way to optimize this loop to skip the system assemblies (but not mscorlib)? I need to call this multiple times.

Thanks!

You can try something like this:

  loadedAssemblies = (from assembly in AppDomain.CurrentDomain.GetAssemblies()
                   where
                     assembly.ManifestModule.Name != "<In Memory Module>"                         
                     && !assembly.FullName.StartsWith("System")
                     && !assembly.FullName.StartsWith("Microsoft")
                     && assembly.Location.IndexOf("App_Web") == -1
                     && assembly.Location.IndexOf("App_global") == -1
                     && assembly.FullName.IndexOf("CppCodeProvider") == -1
                     && assembly.FullName.IndexOf("WebMatrix") == -1
                     && assembly.FullName.IndexOf("SMDiagnostics") == -1
                     && !String.IsNullOrEmpty(assembly.Location)
                   select assembly).ToList();

Once you have "loadedAssemblies" you can do what you need to do with this.

If you're calling this multiple times, then store the loaded assemblies in a variable so you don't query each time but rather just iterate over the list.

You can call Load(). From the remarks:

http://msdn.microsoft.com/en-us/library/36az8x58.aspx

If a version of the requested assembly is already loaded, this method returns the loaded assembly, even if a different version is requested.

However, under the hood, I'm not sure if this is faster than your O(n) loop (or even any different). You might want to test them both out. If you need to run this loop for multiple assemblies then I suggest running the loop once and finding all the assemblies you need in that go. If you are running this loop multiple times for a same assembly then I suggest a singleton type pattern where you can get the reference once and never look again.

If you know all of your program assemblies you could just make a collection of those. You can also load Mscorlib.dll without having to iterate through every assembly. In essence I'm proposing whitelisting as opposed to blacklisting.

Assembly assembly = Assembly.Load("Mscorlib.dll");

http://ondotnet.com/pub/a/dotnet/excerpt/prog_csharp_ch18/index.html?page=5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM