科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航



ZDNet>软件频道>中间件-zhiding>获取执行代码所处环境的信息

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

   现在做一个方法运行耗时测试类 Imports System.ComponentModel Namespace uReflection Public Class CurrentMethodInfo Private gMethodInformati

来源:中国IT实验室 2007年10月02日

关键字:编程 获取

  

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

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

_
Public ReadOnly Property AssemblyName() As String
Get
Return gMethodbase.DeclaringType.Assembly.GetName.Name
End Get
End Property

_
Public ReadOnly Property AssemblyVersion() As String
Get
Return gMethodbase.DeclaringType.Assembly.GetName.Version.ToString
End Get
End Property

_
Public ReadOnly Property TypeName() As String
Get
Return gMethodbase.DeclaringType.FullName
End Get
End Property

_
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
_
Public ReadOnly Property Name() As String
Get
Return System.AppDomain.CurrentDomain.SetupInformation.ApplicationName
End Get
End Property

_
Public ReadOnly Property Path() As String
Get
Return System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase
End Get
End Property

_
Public ReadOnly Property Config() As String
Get
Return System.AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
End Get
End Property

_
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
********************************************************************************

查看本文来源