Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在.Net中获取给定类型的程序集(System.Reflection.assembly)?_C#_.net_Reflection_.net Assembly - Fatal编程技术网

C# 如何在.Net中获取给定类型的程序集(System.Reflection.assembly)?

C# 如何在.Net中获取给定类型的程序集(System.Reflection.assembly)?,c#,.net,reflection,.net-assembly,C#,.net,Reflection,.net Assembly,在.Net中,如果给定类型名,是否有方法告诉我在哪个程序集中(System.Reflection.assembly的实例)定义了该类型 我假设我的项目已经有了对该程序集的引用,只需要知道它是哪一个 Type.GetType(typeNameString).Assembly 将System.Int32替换为您需要的任何类型。因为它接受类型参数,所以您可以通过这种方式执行任何操作,例如: string GetAssemblyLocationOfObject(object o) { retu

在.Net中,如果给定类型名,是否有方法告诉我在哪个程序集中(System.Reflection.assembly的实例)定义了该类型

我假设我的项目已经有了对该程序集的引用,只需要知道它是哪一个

Type.GetType(typeNameString).Assembly
System.Int32
替换为您需要的任何类型。因为它接受
类型
参数,所以您可以通过这种方式执行任何操作,例如:

string GetAssemblyLocationOfObject(object o) {
    return Assembly.GetAssembly(o.GetType()).Location;
}

Assembly.GetAssembly假定您有该类型的实例,type.GetType假定您有包含程序集名称的完全限定类型名称

如果只有基类型名称,则需要执行以下操作:

public static String GetAssemblyNameContainingType(String typeName) 
{
    foreach (Assembly currentassembly in AppDomain.CurrentDomain.GetAssemblies()) 
    {
        Type t = currentassembly.GetType(typeName, false, true);
        if (t != null) {return currentassembly.FullName;}
    }

    return "not found";
}

这还假定您的类型是在根目录中声明的。您需要在名称中提供名称空间或封闭类型,或以相同的方式进行迭代。

如果可以使用,此语法是最短/最干净的:

typeof(int).Assembly

我已经根据自己的目的修改了接受的答案(返回assembly对象而不是assembly名称),并重构了VB.NET和LINQ的代码:

Public Function GetAssemblyForType(typeName As String) As Assembly
    Return AppDomain.CurrentDomain.GetAssemblies.FirstOrDefault(Function(a) a.GetType(typeName, False, True) IsNot Nothing)
End Function

如果其他人想要一个可接受答案的关键解决方案,我就在这里分享。

该方法需要一个程序集限定名。如果不使用程序集名称限定它,您确定它可以跨程序集工作吗?作为旁注,在使用CSharpCodeProvider动态编译C#时,我使用该函数作为填充ReferencedAssemblys列表的一种方法。这是一个有用的提示,但原始海报只有类型名称,不是类型实例。如果ArgumentException找不到您要查找的内容,我可能会抛出ArgumentException。假设这是一个例外情况,然后您也可以假设您找到了它(或将错误处理代码放在catch语句中),但是Assembly.GetAssembly不需要该类型的实例,它只需要该类型,因此如果您正在寻找在编译时知道该类型的内容,可以使用typeof(type)就像我的第一个例子。谢谢你的回答,这对我来说很有吸引力。我没有类型,只有类型名,并且知道对包含它的程序集的引用是可用的。在发布这样的一般性答案之前,您是否阅读了OPs问题?OP没有类型,只有字符串名称。这个答案完全忽略了这个事实。@JoshuaHayes你做的假设和我做的一样多。在编写泛型答案时,我只从一个类型名(
int
)开始,并编写了一个表达式来获取定义该类型的程序集。在整个线程中,OP没有一次说他将类型名作为字符串,事实上他是这样做的。查看方法签名(string typeName)。OP说他只有类型名称。因此我引用了字符串类型名称。
Public Function GetAssemblyForType(typeName As String) As Assembly
    Return AppDomain.CurrentDomain.GetAssemblies.FirstOrDefault(Function(a) a.GetType(typeName, False, True) IsNot Nothing)
End Function