C# 如何获取程序集的命名空间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/653128/
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 to get Namespace of an Assembly?
提问by SyncMaster
Consider i have an assembly(class library dll) which i have loaded using the following code,
考虑我有一个程序集(类库 dll),我使用以下代码加载了它,
Assembly a = Assembly.LoadFrom(@"C:\Documents and Settings\E454935\My Documents\Visual Studio 2005\Projects\nunit_dll_hutt\for_hutt_proj\bin\Debug\asdf.dll");
and i need to get the type of the Assembly. In order to get the typei need the namespace of the assembly.
我需要获取大会的类型。为了获得类型,我需要程序集的命名空间。
Type t = asm.GetType("NAMESPACE.CLASSNAME",false,true);
how can i get the Namespace in the above line.?! as inorder to get the Namespace, i need to get the type..?
我怎样才能在上面的行中获得命名空间。?!为了获得命名空间,我需要获得类型..?
Type.Namespace;
i.e i need to get the Namespace of the assembly which can be used to get its Type.
即我需要获取可用于获取其类型的程序集的命名空间。
Thanks in advance
提前致谢
采纳答案by sharptooth
Use Assembly.GetTypes() - this will get you a collection of all types and then you can get the Namespace property for each of them.
使用 Assembly.GetTypes() - 这将为您提供所有类型的集合,然后您可以获得每个类型的 Namespace 属性。
Then I guess you can simply check that all the types have same Namespace value and use this value. Otherwise add some other logic to detect what namespace to consider primary.
然后我想您可以简单地检查所有类型是否具有相同的 Namespace 值并使用此值。否则,添加一些其他逻辑来检测要考虑主要的命名空间。
回答by eglasius
An assembly can contain multiple namespaces. I think what you really want to ask is how to get a type from an assembly without specifying the namespace.
一个程序集可以包含多个命名空间。我认为您真正想问的是如何在不指定名称空间的情况下从程序集中获取类型。
I don't know if there is a better way, but you can try looking for the specific type like this (add - using linq;):
我不知道是否有更好的方法,但您可以尝试寻找这样的特定类型(添加 - 使用 linq;):
myassembly.GetTypes().SingleOrDefault(t => t.Name == "ClassName")
This will effectively throw if there is more than 1 class with that name under different namespaces (because the Single method ensures there is only 1).
如果在不同的命名空间下有 1 个以上具有该名称的类,这将有效地抛出(因为 Single 方法确保只有 1 个)。
For a list of the namespaces for that class you can:
有关该类的命名空间列表,您可以:
Assembly.Load("ClassName").GetTypes().Select(t => t.Namespace).Distinct();
回答by Lance H
Assembly.GetName().Name
will get you the default namespace
Assembly.GetName().Name
将为您提供默认命名空间
回答by RAM
Updated:
更新:
IFthe assembly name
& assembly namespace
are samein your project and you sure keep theme same ANDyou want get the namespace of the current executed Assemblythenyou can use this:
IF的assembly name
&assembly namespace
是相同的项目,并确定保留的主题相同和你想要得到的命名空间大会执行电流,然后你可以使用这个:
var namespace = Assembly.GetExecutingAssembly().GetName().Name;
And for your loaded assembly:
对于您加载的程序集:
var namespace = myAssembly.GetName().Name;
But IFthe assembly name
& assembly namespace
are notsame in your project then you can use this way:
但是,如果在assembly name
与assembly namespace
不在您的项目相同的,那么你可以用这样的方式:
// Like @eglasius answer >>
// Find all namespaces in the target assembly where a class with the following name is exists:
var namespaceList=Assembly.Load("MyClassName").GetTypes().Select(t => t.Namespace).Distinct();
回答by Tiago S
To take only the namespace follows the code below:
要仅使用命名空间,请遵循以下代码:
var assembly = System.Reflection.Assembly.GetAssembly(this.GetType());//Get the assembly object
var nameSpace = assembly.GetType().Namespace;//Get the namespace
OR
或者
public string GetNamespace(object obj)
{
var nameSpace = obj.GetType().Namespace;//Get the namespace
return nameSpace;
}
回答by Jean
Using Mono/Xamarin, you don't have access to the "NameSpace" property. You may use the following instead:
使用 Mono/Xamarin,您无权访问“NameSpace”属性。您可以改用以下内容:
var str = typeof(ATypeInTheAssembly).AssemblyQualifiedName;
return str.Split(',')[1].Trim();
回答by Davide Cannizzo
Getting that an assembly must contains almost a class (or any other type, such as an interface) and they must be contained in a namespace, which has to start within the assembly namespace, the shortest of them will be the last one.
程序集必须包含几乎一个类(或任何其他类型,例如接口),并且它们必须包含在命名空间中,命名空间必须在程序集命名空间内开始,其中最短的将是最后一个。
So, here is the solution I found:
所以,这是我找到的解决方案:
public string GetAssemblyNamespace(Assembly asm)
{
string ns = @"";
foreach (Type tp in asm.Modules.First().GetTypes()) //Iterate all types within the specified assembly.
if (ns.Length == 0 ? true : tp.Namespace.Length < ns.Length) //Check whether that's the shortest so far.
ns = tp.Namespace; //If it's, set it to the variable.
return ns; //Now it is the namespace of the assembly.
}
I just find all types within the required assembly and I search for which is contained in the namespace having the shortest name.
我只是在所需的程序集中找到所有类型,并搜索包含在名称最短的命名空间中的所有类型。