2010-08-26 74 views
1

我正在编程查看一个.aspx文件并获取在其CodeBehind中声明的文件类名称。例如,在分析myFile.aspx时,我在其页面指令中读取并发现它的CodeBehind等于“myApplication \ myPage.aspx.vb”。然后我使用下面的代码:如何从namespace.class名称找到文件路径?

[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)。然而,要阅读这个文件,我需要得到它的完整路径,而不是它的命名空间/类hiearchy。在这种情况下,BasePage被包装在不同的名称空间声明中,因此我不能只更改'。'至 '\'。

如何获取BasePage的路径以便我可以读取它?谢谢你 - 弗兰克

回答

0

我认为你需要把重点放在通过的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

+0

thanx的答复, 不幸的是,这只能让我.dll文件的位置。 我想以编程方式跟踪调用的方法的顺序/层次结构,并列出(和文件名)。基本上,我正在尝试创建一个桌面应用程序,它将查看我的asp.net应用程序(通过它的dll或pdb),并告诉我方法的结构和它们的路径位置。该显示应该类似于您在出现错误时看到的堆栈跟踪(在asp.net中)。 – Jason 2010-09-01 17:58:59

相关问题