gpt4 book ai didi

.net - 如何从 namespace.class 名称中找到文件路径?

转载 作者:行者123 更新时间:2023-12-04 16:58:21 25 4
gpt4 key购买 nike

我正在以编程方式查看 .aspx 文件并获取在其 CodeBehind 中声明的文件类名。例如,在分析 myFile.aspx 时,我阅读了它的页面指令,发现它的 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 的路径以便我可以阅读它?谢谢 - 弗兰克

最佳答案

我认为您需要专注于通过 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

关于.net - 如何从 namespace.class 名称中找到文件路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3577082/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com