C# 如何检索程序集的限定类型名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/441680/
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
How can I retrieve an assembly's qualified type name?
提问by dance2die
How can I generate a assembly qualified type name?
如何生成程序集限定类型名称?
For an example, when configuring a membership provider, I would have to provide a assembly qualified type name for "SqlMembershipProvider" (in this example, i have copied the below configuration from somewhere) in "type" attribute.
例如,在配置成员资格提供程序时,我必须在“type”属性中为“SqlMembershipProvider”提供程序集限定类型名称(在本例中,我从某处复制了以下配置)。
How do you generate that assembly qualified type name? Does it have to be typed manually everytime by examining an assembly type?
您如何生成该程序集限定类型名称?是否每次都必须通过检查程序集类型手动输入?
<membership> <providers> <clear /> <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="LocalSqlServer"/> </providers> </membership>
<membership> <providers> <clear /> <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="LocalSqlServer"/> </providers> </membership>
[UPDATE]: Simpler PowerShell version
[更新]:更简单的 PowerShell 版本
PS>([System.String]).AssemblyQualifiedName
System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
采纳答案by Gulzar Nazim
This is a nice handy tool(shell extension with source code) for copying the fully qualified name to clipboard by right clicking on any assembly.
这是一个非常方便的工具(带有源代码的外壳扩展),用于通过右键单击任何程序集将完全限定名称复制到剪贴板。
Update: After seeing the comment from dance2die, thought of putting together a sample powershell script to export the type name to a csv file.
更新:在看到来自 dance2die 的评论后,想到将示例 powershell 脚本放在一起以将类型名称导出到 csv 文件。
> [System.Reflection.Assembly]::LoadWithPartialName("System.Web")
> [System.Web.Security.SqlMembershipProvider] | select {$_.UnderlyingSystemType.AssemblyQualifiedName } | export-csv c:\typenames.csv
Using C#, if you want to generate the assembly qualified type name with all the references set, it is easy to build a test script using reflection..
使用 C#,如果要生成具有所有引用集的程序集限定类型名称,可以使用反射轻松构建测试脚本。
using System;
using System.Reflection;
........
Type ty = typeof(System.Web.Security.SqlMembershipProvider);
string fullname = ty.AssemblyQualifiedName;
//"System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
回答by Andrew Hare
The easiest way to find that information is to use Reflector, load the assembly, and copy the Name
out of the assembly's manifest. The manifest information will be in the bottom pane of Reflector when your assembly is selected in the window above.
找到该信息的最简单方法是使用Reflector,加载程序集,然后Name
从程序集的清单中复制出来。在上面的窗口中选择您的程序集时,清单信息将显示在 Reflector 的底部窗格中。
回答by Ironicnet
AFAIK the Fully qualified type name consists in the Class name and the assembly signature. I don't think that you can generate it...
AFAIK 完全限定类型名称包含在类名称和程序集签名中。我不认为你可以生成它......
The type is the namespace.
类型是命名空间。
The version you can set it in the AssemblyInfo.cs (remove the * for keep it the same)
您可以在 AssemblyInfo.cs 中设置的版本(删除 * 以保持不变)
The public key token you set it in the Project properties.
您在项目属性中设置的公钥令牌。
That's from what i remember
那是我记得的
other way to obtain the assembly qualified name could be:
获取程序集限定名称的其他方法可能是:
class Program
{
static void Main(string[] args)
{
string assemblyPath = args[0];
Assembly loadedAssembly = Assembly.LoadFrom(assemblyPath);
Module[] modules = loadedAssembly.GetModules();
Console.WriteLine("Assembly: " + loadedAssembly.GetType().Name);
foreach (Module module in modules)
{
Console.WriteLine("Module: {0}\nFullyQualifiedName: {1}", module.Name, module.FullyQualifiedName);
Type[] types = module.GetTypes();
foreach (Type type in types)
{
Console.WriteLine("Type: {0}\n FullName: {1}", type.Name, type.FullName);
}
}
Console.ReadLine();
}
}
回答by Ahmed Samir
ok just use it like Gulzar Nazim wrote
好的,就像 Gulzar Nazim 写的那样使用它
$ String typeName = typeof(AssemblyName.ClassName).AssemblyQualifiedName);
$ String typeName = typeof(AssemblyName.ClassName).AssemblyQualifiedName);
that give you the full name you can use it in type.getType(); That works with me and no null any more
给你全名,你可以在 type.getType() 中使用它;这对我有用,不再为空
回答by Ahmed Samir
use add type in your command :
在您的命令中使用添加类型:
add-type -assemblyname "system.example"