【问题标题】:How do I find the file path from a namespace.class name?如何从 namespace.class 名称中找到文件路径?
【发布时间】:2010-08-26 16:12:50
【问题描述】:

我正在以编程方式查看 .aspx 文件并获取在其 CodeBehind 中声明的文件类名。例如,在分析 myFile.aspx 时,我阅读了它的 Page Directive,发现它的 CodeBehind 等于“myApplication\myPage.aspx.vb”。然后我使用下面的代码:

[Code]
Dim Assm As System.Reflection.Assembly = System.Reflection.Assembly.LoadFrom("myApplication\bin\myApplication.dll")
Dim ClassType As Type = Assm.GetType("myApplication\myPage.aspx.vb")

' myBaseType = "myApplication.Forms.BasePage"
Dim myBaseType As System.Type = ClassType.BaseType
[/Code]

现在我想读取 BaseFile (class= myApplication.Forms.BasePage)。但是,要读入这个文件,我需要获取它的完整路径而不是它的命名空间/类层次结构。在这种情况下,BasePage 被包装在不同的命名空间声明中,因此我不能只更改“。”到'\'。

如何获取 BasePage 的路径以便阅读?谢谢 - 弗兰克

【问题讨论】:

    标签: .net vb.net namespaces


    【解决方案1】:

    我认为您需要专注于通过 GetType() 方法获取位置类的“类型”无论,然后对该类型应用反射以获取其位置。所以不要专注于命名空间,而是评估 BaseFile 命名空间中的类型。这是我从 MSDN 翻译的一些代码,用于获取我的“客户”类的物理位置。您应该能够使用它来根据应用程序中的类型获取任何二进制文件的位置:

        Dim SampleAssembly As [Assembly]
        ' Instantiate a target object.
        Dim myType As New Customer()
        Dim Type1 As Type
        ' Set the Type instance to the target class type.
        Type1 = myType.GetType()
        ' Instantiate an Assembly class to the assembly housing the Integer type.  
        SampleAssembly = [Assembly].GetAssembly(myType.GetType())
        ' Gets the location of the assembly using file: protocol.
        Console.WriteLine(("CodeBase=" + SampleAssembly.CodeBase))
    

    您可以通过以下链接了解更多信息:

    Assembly.CodeBase 属性:
    http://msdn.microsoft.com/en-us/library/system.reflection.assembly.codebase.aspx

    【讨论】:

    • 感谢您的回复,不幸的是,这只允许我获取 .dll 的位置。我正在尝试以编程方式跟踪被调用的方法的序列/层次结构......并将其列出(以及文件名)。基本上,我正在尝试创建一个桌面应用程序,它将查看我的 asp.net 应用程序(通过它的 dll 或 pdb)并告诉我方法的层次结构及其路径位置。显示应该类似于出现错误时看到的堆栈跟踪(在 asp.net 中)。
    猜你喜欢
    • 1970-01-01
    • 2013-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-29
    • 2011-01-25
    • 1970-01-01
    相关资源
    最近更新 更多