0

场景:我想在运行时加载解决方案中存在的程序集。

belo 代码将不起作用,因为它不存在于当前应用程序域中。

Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();

此外,如果我将在引用的程序集中搜索,那么也找不到它,因为它没有被引用。所以下面的代码也不起作用:

public static IEnumerable<Assembly> GetAssemblies()
    {
        var list = new List<string>();
        var stack = new Stack<Assembly>();

        stack.Push(Assembly.GetEntryAssembly());

        do
        {
            var asm = stack.Pop();

            yield return asm;

            foreach (var reference in asm.GetReferencedAssemblies())
                if (!list.Contains(reference.FullName))
                {
                    stack.Push(Assembly.Load(reference));
                    list.Add(reference.FullName);
                }

        }
        while (stack.Count > 0);

    }

你有什么建议吗?

4

1 回答 1

0

Sign your assembly, then in your app.config, specify the fully qualified assembly name. Call Assembly.Load(string), passing the assembly name from your app.config.

NOTE: The assembly you are attempting to load must be in a location where the runtime can find it. Your best bet is either to put it into the Global Assembly Cache, or make sure it is in the same folder as your executing assembly.

于 2012-10-11T14:20:20.590 回答