HOW TO:获取执行代码所处环境信息(应用)

Author:水如烟

HOW TO:获取执行代码所处环境信息(小结)

现在做一个方法运行耗时测试类

Imports  System.ComponentModel

Namespace  uReflection
    
Public   Class  CurrentMethodInfo

        
Private  gMethodInformation  As  currentMethodInformation

        
Public   Sub   Input ( ByVal  methodbase  As  System.Reflection.MethodBase)
            gMethodInformation 
=   New  currentMethodInformation(methodbase)
        
End Sub

        
Public   Sub   Input ( ByVal  stackframe  As  System.Diagnostics.StackFrame)
            gMethodInformation 
=   New  currentMethodInformation(stackframe)
        
End Sub

        
' 下以为测试方法耗时用

        
Private  gStopwatch  As  System.Diagnostics.Stopwatch
        
Private  gIsCheck  As   Boolean
        
Private  gUsetime  As   String
        
Public   Property  CheckRuntime()  As   Boolean
            
Get
                
Return  gIsCheck
            
End   Get
            
Set ( ByVal  value  As   Boolean )
                
If  value  <>  gIsCheck  Then
                    
If  value  Then
                        gStopwatch 
=   New  System.Diagnostics.Stopwatch
                    
End   If
                    gIsCheck 
=  value
                
End   If
            
End   Set
        
End Property

        
Public   Sub  Start()
            
If  CheckRuntime  Then  gStopwatch.Start()
        
End Sub

        
Public   Sub  [ Stop ]()
            
If  CheckRuntime  Then
                gStopwatch.Stop()
                gUsetime 
=  gStopwatch.Elapsed.ToString
                gStopwatch.Reset()
            
End   If
        
End Sub

        
Public   Overrides   Function  ToString()  As   String
            
Dim  b  As   New  System.Text.StringBuilder
            b.Append(appInformation.ToString)
            b.Append(gMethodInformation.ToString)
            
If  CheckRuntime  Then
                b.Append(System.Environment.NewLine)
                b.AppendFormat(
" **    测试用时 :{0} " Me .gUsetime)
                b.Append(System.Environment.NewLine)
                b.AppendFormat(
" **    测试时间 :{0} " , Now.ToString)
                b.Append(System.Environment.NewLine)
            
End   If
            b.Append(
New   String ( " * " c,  80 ))
            
Return  b.ToString

        
End Function


        
Private   Class  currentMethodInformation
            
Private  gMethodbase  As  System.Reflection.MethodBase
            
Sub   New ( ByVal  methodbase  As  System.Reflection.MethodBase)
                gMethodbase 
=  methodbase
            
End Sub

            
Sub   New ( ByVal  stackFrame  As  System.Diagnostics.StackFrame)
                gMethodbase 
=  stackFrame.GetMethod
            
End Sub

            
< Description( " 程序集名称 " ) >  _
            
Public   ReadOnly   Property  AssemblyName()  As   String
                
Get
                    
Return  gMethodbase.DeclaringType.Assembly.GetName.Name
                
End   Get
            
End Property

            
< Description( " 程序集版本 " ) >  _
            
Public   ReadOnly   Property  AssemblyVersion()  As   String
                
Get
                    
Return  gMethodbase.DeclaringType.Assembly.GetName.Version.ToString
                
End   Get
            
End Property

            
< Description( " 类名 " ) >  _
            
Public   ReadOnly   Property   TypeName ()  As   String
                
Get
                    
Return  gMethodbase.DeclaringType.FullName
                
End   Get
            
End Property

            
< Description( " 方法名 " ) >  _
            
Public   ReadOnly   Property  Name()  As   String
                
Get
                    
Return  gMethodbase.ToString
                
End   Get
            
End Property

            
Public   Overloads   Function  ToString()  As   String
                
Dim  b  As   New  System.Text.StringBuilder
                b.Append(System.Environment.NewLine)
                b.AppendFormat(
" **  程序集名称 :{0} " Me .AssemblyName)
                b.Append(System.Environment.NewLine)
                b.AppendFormat(
" **  程序集版本 :{0} " Me .AssemblyVersion)
                b.Append(System.Environment.NewLine)
                b.AppendFormat(
" **        类名 :{0} " Me .TypeName)
                b.Append(System.Environment.NewLine)
                b.AppendFormat(
" **      方法名 :{0} " Me .Name)
                b.Append(System.Environment.NewLine)
                
Return  b.ToString
            
End Function

        
End Class

        
Private   Class  appInformation
            
< Description( " 程序名称 " ) >  _
            
Public   ReadOnly   Property  Name()  As   String
                
Get
                    
Return  System.AppDomain.CurrentDomain.SetupInformation.ApplicationName
                
End   Get
            
End Property

            
< Description( " 程序目录 " ) >  _
            
Public   ReadOnly   Property  Path()  As   String
                
Get
                    
Return  System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase
                
End   Get
            
End Property

            
< Description( " 配置文件 " ) >  _
            
Public   ReadOnly   Property  Config()  As   String
                
Get
                    
Return  System.AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
                
End   Get
            
End Property

            
< Description( " 程序入口 " ) >  _
            
Public   ReadOnly   Property  entryPoint()  As   String
                
Get
                    
Return  System.Reflection.Assembly.GetEntryAssembly.EntryPoint.ToString
                
End   Get
            
End Property

            
Public   Shared   Shadows   Function  ToString()  As   String
                
Dim  mInfo  As   New  appInformation
                
Dim  b  As   New  System.Text.StringBuilder
                b.Append(
New   String ( " * " c,  80 ))
                b.Append(System.Environment.NewLine)
                b.AppendFormat(
" **  程序名称 :{0} " , mInfo.Name)
                b.Append(System.Environment.NewLine)
                b.AppendFormat(
" **  程序目录 :{0} " , mInfo.Path)
                b.Append(System.Environment.NewLine)
                b.AppendFormat(
" **  配置文件 :{0} " , mInfo.Config)
                b.Append(System.Environment.NewLine)
                b.AppendFormat(
" **  程序入口 :{0} " , mInfo.entryPoint)
                b.Append(System.Environment.NewLine)
                b.Append(
New   String ( " * " c,  80 ))
                
Return  b.ToString
            
End Function
        
End Class
    
End Class
End Namespace


应用

     Dim  MethodTest  As   New  LzmTW.uReflection.CurrentMethodInfo
    
Public   Sub  Load( ByVal  mainform  As  System.Windows.Forms.Form)  Implements  LzmTW.AddIns.Base.IPackage.Load
        MethodTest.Input(System.Reflection.MethodBase.GetCurrentMethod)
        MethodTest.CheckRuntime 
=   True
        MethodTest.Start()

        gMainForm 
=   CType (mainform, LzmTW.ApplicationBase.MainForm)
        Initialize()

        MethodTest.Stop()
        Console.WriteLine(MethodTest.ToString)
        Console.WriteLine()
    
End Sub


     Private   Sub  OnUILoad( ByVal  sender  As   Object ByVal  e  As  EventArgs)
        MethodTest.Input(
New  System.Diagnostics.StackFrame)
        MethodTest.Start()

        gMenuFactory.GetMenuItem(MENU_UNLOAD).Enabled 
=   True
        gMenuFactory.GetMenuItem(MENU_LOAD).Enabled 
=   False
        gUI.Initialize()

        MethodTest.Stop()
        Console.WriteLine(MethodTest.ToString)
        Console.WriteLine()
    
End Sub


输出结果:

********************************************************************************
**   程序名称 :MyComputer.vshost.exe
**   程序目录 :G: / Documents  and  Settings / LzmTW / My Documents / Visual Studio  2005 / Projects / MyComputer / MyComputer / bin / Debug /
**   配置文件 :G: / Documents  and  Settings / LzmTW / My Documents / Visual Studio  2005 / Projects / MyComputer / MyComputer / bin / Debug / MyComputer.vshost.exe.config
**   程序入口 :Void Main(System.String[])
********************************************************************************
**   程序集名称 :ComputerAddIns
**   程序集版本 : 1.0 . 0.0
**         类名 :ComputerAddIns.wmiQueryApplication
**       方法名 :Void Load(System.Windows.Forms.Form)

**     测试用时 : 00 : 00 : 00.0488375
**     测试时间 : 2006 - 8 - 17   0 : 03 : 08
********************************************************************************

********************************************************************************
**   程序名称 :MyComputer.vshost.exe
**   程序目录 :G: / Documents  and  Settings / LzmTW / My Documents / Visual Studio  2005 / Projects / MyComputer / MyComputer / bin / Debug /
**   配置文件 :G: / Documents  and  Settings / LzmTW / My Documents / Visual Studio  2005 / Projects / MyComputer / MyComputer / bin / Debug / MyComputer.vshost.exe.config
**   程序入口 :Void Main(System.String[])
********************************************************************************
**   程序集名称 :ComputerAddIns
**   程序集版本 : 1.0 . 0.0
**         类名 :ComputerAddIns.wmiQueryApplication
**       方法名 :Void OnUILoad(System.Object, System.EventArgs)

**     测试用时 : 00 : 00 : 01.3316949
**     测试时间 : 2006 - 8 - 17   0 : 03 : 12
********************************************************************************
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值