科技行者

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

知识库

知识库 安全导航

至顶网软件频道基础软件获取执行代码所处环境的信息

获取执行代码所处环境的信息

  • 扫一扫
    分享文章到微信

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

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

作者:水如烟 来源:中国IT实验室 2008年6月9日

关键字: 信息 获取 VB vb.net Windows

  • 评论
  • 分享微博
  • 分享邮件

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

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(Syst