How to get the assembly (System.Reflection.Assembly) for a given type in .Net?

前端 未结 5 1264
天涯浪人
天涯浪人 2020-12-09 14:41

In .Net, given a type name, is there a method that tells me in which assembly (instance of System.Reflection.Assembly) that type is defined?

I assume that my project

5条回答
  •  感动是毒
    2020-12-09 15:40

    I've adapted the accepted answer for my own purposes (returning the assembly object instead of the assembly name), and refactored the code for VB.NET and 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
    

    I'm just sharing it here if anyone else would like a LINQy solution to the accepted answer.

提交回复
热议问题