简体   繁体   English

获取加载程序集的根命名空间(程序集命名空间)

[英]Get root namespace (assembly namespace) of loaded assembly

I'm using the GetTypeInfo() method to get the TypeInfo of a class from an assembly. 我正在使用GetTypeInfo()方法从程序TypeInfo获取类的TypeInfo

Through that I'm trying to get the root namespace of that assembly (or is it called assembly namespace?). 通过这个我试图获得该程序集的根命名空间(或称为程序集命名空间?)。 But I can't find a property in there that gives me that namespace. 但我找不到那个给我命名空间的属性。 There is AssemblyQualifiedName which is a string that has the root namespace in it. AssemblyQualifiedName ,它是一个包含根命名空间的string But there are also a lot of other things in there like version number etc. 但是那里还有很多其他东西,如版本号等。

How can I get the root namespace of an assembly in .NET Core? 如何在.NET Core中获取程序集的根命名空间?

Assemblies don't have namespaces themselves only the types within the assembly. 程序集本身没有名称空间,只有程序集中的类型。 The thing you could be thinking of is the "Assembly Name", that is often the same name as the "Default Namespace" which most types within the assembly will use. 你可以想到的是“程序集名称”,它通常与程序集中大多数类型将使用的“默认命名空间”同名。

Assembly assembly = //..
string name = assembly.GetName().Name;

The GetName() returns a AssemblyName object that contains the pieces that are used to build up the AssemblyQualifiedName . GetName()返回一个AssemblyName对象,其中包含用于构建AssemblyQualifiedName This function is available in .Net Standard 1.0 so is available on all versions of .NET Core 此功能在.Net Standard 1.0中可用,因此可在所有版本的.NET Core上使用

由于程序集可能有多个名称空间,因此您只能尝试获取所有类型的名称空间:

.GetTypeInfo().Assembly.GetTypes().Select(t => t.Namespace).Distinct();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM