.net 如何找到特定 dll 的 PublicKeyToken?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1710935/
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-09-03 13:33:39  来源:igfitidea点击:

How do I find the PublicKeyToken for a particular dll?

.netdll.net-assemblypublickeytoken

提问by Matthew Jones

I need to recreate a provider in my web.config file that looks something like this:

我需要在我的 web.config 文件中重新创建一个如下所示的提供程序:

<membership defaultProvider="AspNetSqlMemProvider">
  <providers>
    <clear/>
    <add connectionStringName="TRAQDBConnectionString" applicationName="TRAQ" minRequiredPasswordLength="7" minRequiredNonalphanumericCharacters="0"
         name="AspNetSqlMemProvider"
         type="System.Web.Security.SqlMembershipProvider, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"
    />
  </providers>
</membership>

However, I get a runtime error saying this assembly cannot be loaded, and I think it is because I have the wrong PublicKeyToken. How do I look up the PublicKeyToken for my assembly?

但是,我收到一个运行时错误,提示无法加载此程序集,我认为这是因为我使用了错误的 PublicKeyToken。如何查找我的程序集的 PublicKeyToken?

Alternatively, am I going entirely the wrong way with this?

或者,我是否完全走错了这条路?

回答by danielB

Using PowerShell, you can execute this statement:

使用PowerShell,您可以执行以下语句:

([system.reflection.assembly]::loadfile("c:\MyDLL.dll")).FullName

The output will provide the Version, Cultureand PublicKeyTokenas shown below:

输出将提供VersionCulturePublicKeyToken,如下所示:

MyDLL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a

回答by Darin Dimitrov

Using sn.exeutility:

使用sn.exe实用程序:

sn -T YourAssembly.dll

or loading the assembly in Reflector.

或在Reflector 中加载程序集。

回答by Zanon

If you have the DLL added to your project, you can open the csproj file and see the Reference tag.

如果将 DLL 添加到项目中,则可以打开 csproj 文件并查看 Reference 标记。

Example:

例子:

<Reference Include="System.Web.Mvc, Version=3.0.0.1, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />

回答by Joaquim Rendeiro

sn -T <assembly>in Visual Studio command line. If an assembly is installed in the global assembly cache, it's easier to go to C:\Windows\assemblyand find it in the list of GAC assemblies.

sn -T <assembly>在 Visual Studio 命令行中。如果程序集安装在全局程序集缓存中,则更容易C:\Windows\assembly在 GAC 程序集列表中找到它。

On your specific case, you might be mixing type full name with assembly reference, you might want to take a look at MSDN.

在您的特定情况下,您可能将类型全名与程序集引用混合使用,您可能需要查看MSDN

回答by Prasanna Balajee

Answer is very simple use the .NET Framework tools sn.exe. So open the Visual Studio 2008 Command Prompt and then point to the dll's folder you want to get the public key,

答案很简单,使用 .NET Framework 工具sn.exe。所以打开Visual Studio 2008 Command Prompt,然后指向你想要获取公钥的dll的文件夹,

Use the following command,

使用以下命令,

sn –T myDLL.dll

This will give you the public key token. Remember one thing this only works if the assembly has to be strongly signed.

这将为您提供公钥令牌。请记住一件事,这仅在必须对程序集进行强签名时才有效。

Example

例子

C:\WINNT\Microsoft.NET\Framework\v3.5>sn -T EdmGen.exe

Microsoft (R) .NET Framework Strong Name Utility  Version 3.5.21022.8
Copyright (c) Microsoft Corporation.  All rights reserved.

Public key token is b77a5c561934e089

回答by Mike Honey

I use Windows Explorer, navigate to C:\Windows\assembly , find the one I need. From the Properties you can copy the PublicKeyToken.

我使用 Windows 资源管理器,导航到 C:\Windows\assembly ,找到我需要的那个。从属性中,您可以复制 PublicKeyToken。

This doesn't rely on Visual Studio or any other utilities being installed.

这不依赖于 Visual Studio 或正在安装的任何其他实用程序。

回答by Hiram

Just adding more info, I wasn't able to find sn.exe utility in the mentioned locations, in my case it was in C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\bin

只是添加更多信息,我无法在上述位置找到 sn.exe 实用程序,就我而言,它位于 C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\bin

回答by CRice

Assembly.LoadFile(@"C:\Windows\Microsoft.NET\Framework\v4.0.30319\system.data.dll").FullName

Assembly.LoadFile(@"C:\Windows\Microsoft.NET\Framework\v4.0.30319\system.data.dll").全名

Will result in

会导致

System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

System.Data,版本=4.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089

回答by Vinod Srivastav

As @CRice said you can use the below method to get a list of dependent assembly with publicKeyToken

正如@CRice 所说,您可以使用以下方法获取带有 publicKeyToken 的依赖程序集列表

public static int DependencyInfo(string args) 
{
    Console.WriteLine(Assembly.LoadFile(args).FullName);
    Console.WriteLine(Assembly.LoadFile(args).GetCustomAttributes(typeof(System.Runtime.Versioning.TargetFrameworkAttribute), false).SingleOrDefault());
    try {
        var assemblies = Assembly.LoadFile(args).GetReferencedAssemblies(); 

        if (assemblies.GetLength(0) > 0)
        {
            foreach (var assembly in assemblies)
            {
                Console.WriteLine(" - " + assembly.FullName + ", ProcessorArchitecture=" + assembly.ProcessorArchitecture);             
            }
            return 0;
        }
    }
    catch(Exception e) {
        Console.WriteLine("An exception occurred: {0}", e.Message);
        return 1;
    } 
    finally{}

    return 1;
}

i generally use it as a LinqPadscript you can call it as

我通常将它用作LinqPad脚本,您可以将其称为

DependencyInfo("@c:\MyAssembly.dll");from the code

DependencyInfo("@c:\MyAssembly.dll");从代码

回答by Mohit Verma

You can also check by following method.

您也可以通过以下方法进行检查。

Go to Run : type the path of DLL for which you need public key. You will find 2 files : 1. __AssemblyInfo_.ini 2. DLL file

转到运行:键入您需要公钥的 DLL 的路径。你会发现 2 个文件:1. __AssemblyInfo_.ini 2. DLL 文件

Open this __AssemblyInfo_.ini file in notepad , here you can see Public Key Token.

在记事本中打开这个 __AssemblyInfo_.ini 文件,在这里你可以看到 Public Key Token。