获取组件不实例化他们 [英] Get Assemblies Without Instantiating Them

查看:123
本文介绍了获取组件不实例化他们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用进去 CurrentDomain 所有程序集 AppDomain.CurrentDomain.GetAssemblies()来写自己的全名的DropDownList ,但是如果我不实例化它们,它们不会在返回数组由<$ C可见$ C> GetAssemblies()在 CurrentDomain

I am trying to get all assemblies in CurrentDomain using AppDomain.CurrentDomain.GetAssemblies() to write their FullName into a DropDownList, however if I don't instantiate them, they are not seen in returned array from GetAssemblies() in CurrentDomain.

它们都添加为参考,并在参考解决方案的文件夹。只有一次,我从 GetAssemblies让他们()是当我第一次实例化它们。

They are all added as Reference and in Reference folder of the solution. Only time I can get them from the GetAssemblies() is when I first instantiate them.

如何解决这个问题提供一种方便,更通用的方法,而不是实例化它们每次当我添加新的组件等。

How to overcome this problem with an easy and more generic way instead of instantiate them everytime when I add new Assembly, etc.

由于公司政策,我得模糊图像的某些部分:

Due to company policy, I have to obfuscate some parts of the images:

所有assembilies在参考文件夹中引用:

All the assembilies are referenced in Reference folder:

推荐答案

其实是有这种情况发生在这里,除了别人的评论微妙的东西。

There is actually something subtle that happens here in addition to the other people's comments.

VisualStudio中实际执行的没有的补充.DLL引用到你的内置组件的反射信息的程序集清单,除非它们被实际使用。

VisualStudio actually does not add .dll references to the assembly manifest of your built assembly's reflection info unless they are actually used.

作为一个样本,做一个新的项目,并引用程序集。我用 nunit.framework.dll中作为一个例子。

As a sample, make a new project, and reference an assembly. I used nunit.framework.dll as an example.

但实际上并不使用它。我的code是简单的:

But don't actually use it. My code is simply:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

但该项目的确实的有一个参考的NUnit在VisualStudio中。现在建立它,并在程序Ildasm.exe 打开装配和清单的顶部是:

But the Project does have a reference to NUnit in VisualStudio. Now build it, and open the assembly in ildasm.exe and the top of the manifest is:

// Metadata version: v4.0.30319
.assembly extern mscorlib
{
  .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .z\V.4..
  .ver 4:0:0:0
}
.assembly ConsoleApplication1
{
    ...etc...

现在,在code,刚刚从NUnit的使用任何东西:

Now, in the code, just use anything from NUnit:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Assert.IsTrue(true);
        }
    }
}

再次重建,并检查程序集清单中程序Ildasm.exe

// Metadata version: v4.0.30319
.assembly extern mscorlib
{
  .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .z\V.4..
  .ver 4:0:0:0
}
.assembly extern nunit.framework
{
  .publickeytoken = (96 D0 9A 1E B7 F4 4A 77 )                         // ......Jw
  .ver 2:6:0:12051
}
.assembly ConsoleApplication1
{
    ...etc...

请注意,现在有一个在IL一个外部。这也反映饲料,所以它知道什么是依赖程序集将被加载。

Note that now there is an extern in the IL. This also feeds reflection, so it knows what the dependent assemblies are to be loaded.

过去的那个小小的细节,其他都表示,运行时实际上并没有加载程序集,直到需要它的第一次,因此你的AppDomain没有加载的程序集,直到实例化或使用的东西从组装

Past that little detail, as other have stated, the runtime doesn't actually load an assembly until it is needed for the first time, hence your AppDomain doesn't have the assemblies loaded until you instantiate or use something from that assembly.

这上面详细地发挥作用,当你开始尝试使用 Assembly.GetReferencedAssemblies()方法。

That detail above comes into play when you start to try to use the Assembly.GetReferencedAssemblies() method.

因此​​,在VisualStudio中,我有一个项目,有这些引用:

So in VisualStudio, I have a project that has these references:

Microsoft.CSharp
nunit.framework
System
System.Core
System.Data
System.Xml
System.Xml.Linq

然后,我有C#code:

Then I have the C# code:

using System;
using System.Reflection;
using NUnit.Framework;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var assemblies = Assembly.GetExecutingAssembly().GetReferencedAssemblies();
            foreach (var assemblyName in assemblies)
            {
                Console.WriteLine(assemblyName.FullName);
            }
            Console.ReadKey();
        }
    }
}

请注意,我甚至有一个使用NUnit.Framework; 语句!但是,我并没有真正的使用的NUnit的任何地方,所以它的没有的引用程序集。运行此的输出是:

Note that I even have a using NUnit.Framework; statement! But I don't actually use NUnit anywhere, so it is not a referenced assembly. The output of running this is:

mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

这就是它。 如果我加入这行到我的测试应用程序:

That is it. If I add this line into my test app:

Assert.IsTrue(true);

现在一些实际的使用的NUnit的,一个次我的控制台输出是:

Now something actually uses NUnit,a nd my console output is:

mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
nunit.framework, Version=2.6.0.12051, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77

这篇关于获取组件不实例化他们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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