简体   繁体   English

C#如何获取非系统程序集

[英]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. 我需要实例化仅在程序程序集或mscorlib中定义的对象,而不在任何其他系统程序集中定义的对象。 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)? 有没有一种方法可以优化此循环以跳过系统程序集(但不包括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. 一旦有了“ loadedAssemblies”,就可以执行此操作。

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(). 您可以调用Load()。 From the remarks: 从备注:

http://msdn.microsoft.com/en-us/library/36az8x58.aspx 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). 但是,在幕后,我不确定这是否比您的O(n)循环快(甚至不同)。 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. 您也可以加载Mscorlib.dll,而不必遍历每个程序集。 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 http://ondotnet.com/pub/a/dotnet/excerpt/prog_csharp_ch18/index.html?page=5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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