C#:如何从 GAC 加载程序集?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2279341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:58:54  来源:igfitidea点击:

C#: How to load assembly from GAC?

c#.netassembliesgac

提问by Artem

I have "mycomp.myassembly.dll" in GAC but Load and LoadFrom throws file not found exception and LoadWithPartialName returns null. I'm doing the following:

我在 GAC 中有“mycomp.myassembly.dll”,但 Load 和 LoadFrom 抛出文件未找到异常并且 LoadWithPartialName 返回 null。我正在做以下事情:

    AssemblyName name = new AssemblyName();
    name.Name = "mycomp.myassembly.dll";

    Assembly assembly = Assembly.Load(name);

fails with FileNotFound for mycomp.myassembly.dll file, and

对于 mycomp.myassembly.dll 文件, FileNotFound 失败,并且

    Assembly assembly = Assembly.Load("mycomp.myassembly.dll");

fails with exactly the same message.

失败并显示完全相同的消息。

I double checked that assembly is in GAC (and even did gacutil /if for it again) and it does work in all other cases, I just unable to load it myself.

我仔细检查了程序集是否在 GAC 中(甚至再次检查了 gacutil /if)并且它在所有其他情况下都有效,我只是无法自己加载它。

What am I doing wrong here? Do I miss something?

我在这里做错了什么?我错过了什么吗?

采纳答案by Arnold Zokas

Have you tried using the fully qualified assembly name? (e.g. "ycomp.myassembly.dll, Version=1.0.2004.0, Culture=neutral, PublicKeyToken=8744b20f8da049e3")

您是否尝试过使用完全限定的程序集名称?(例如"ycomp.myassembly.dll, Version=1.0.2004.0, Culture=neutral, PublicKeyToken=8744b20f8da049e3"

回答by Sky Sanders

try simply loading with the full name

尝试简单地加载全名

 // You must supply a valid fully qualified assembly name.            
        Assembly SampleAssembly = Assembly.Load
            ("SampleAssembly, Version=1.0.2004.0, Culture=neutral, PublicKeyToken=8744b20f8da049e3");

I can usually load without the version, culture and public key.

我通常可以在没有版本、文化和公钥的情况下加载。