怎么样通过HOW TO获取执行代码所处环境信息?

怎么样通过HOW TO获取执行代码所处环境信息?

Author:水如烟

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

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

ImportsSystem.ComponentModel

NamespaceuReflection
PublicClassCurrentMethodInfo

PrivategMethodInformationAscurrentMethodInformation

PublicSubInput(ByValmethodbaseAsSystem.Reflection.MethodBase)
gMethodInformation
=NewcurrentMethodInformation(methodbase)
EndSub

PublicSubInput(ByValstackframeAsSystem.Diagnostics.StackFrame)
gMethodInformation
=NewcurrentMethodInformation(stackframe)
EndSub

'下以为测试方法耗时用

PrivategStopwatchAsSystem.Diagnostics.Stopwatch
PrivategIsCheckAsBoolean
PrivategUsetimeAsString
PublicPropertyCheckRuntime()AsBoolean
Get
ReturngIsCheck
EndGet
Set(ByValvalueAsBoolean)
Ifvalue<>gIsCheckThen
IfvalueThen
gStopwatch=NewSystem.Diagnostics.Stopwatch
EndIf
gIsCheck=value
EndIf
EndSet
EndProperty

PublicSubStart()
IfCheckRuntimeThengStopwatch.Start()
EndSub

PublicSub[Stop]()
IfCheckRuntimeThen
gStopwatch.Stop()
gUsetime
=gStopwatch.Elapsed.ToString
gStopwatch.Reset()

EndIf
EndSub

PublicOverridesFunctionToString()AsString
DimbAsNewSystem.Text.StringBuilder
b.Append(appInformation.ToString)
b.Append(gMethodInformation.ToString)

IfCheckRuntimeThen
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)

EndIf
b.Append(NewString("*"c,80))
Returnb.ToString

EndFunction


PrivateClasscurrentMethodInformation
PrivategMethodbaseAsSystem.Reflection.MethodBase
SubNew(ByValmethodbaseAsSystem.Reflection.MethodBase)
gMethodbase
=methodbase
EndSub

SubNew(ByValstackFrameAsSystem.Diagnostics.StackFrame)
gMethodbase
=stackFrame.GetMethod
EndSub

<Description("程序集名称")>_
PublicReadOnlyPropertyAssemblyName()AsString
Get
ReturngMethodbase.DeclaringType.Assembly.GetName.Name
EndGet
EndProperty

<Description("程序集版本")>_
PublicReadOnlyPropertyAssemblyVersion()AsString
Get
ReturngMethodbase.DeclaringType.Assembly.GetName.Version.ToString
EndGet
EndProperty

<Description("类名")>_
PublicReadOnlyPropertyTypeName()AsString
Get
ReturngMethodbase.DeclaringType.FullName
EndGet
EndProperty

<Description("方法名")>_
PublicReadOnlyPropertyName()AsString
Get
ReturngMethodbase.ToString
EndGet
EndProperty

PublicOverloadsFunctionToString()AsString
DimbAsNewSystem.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)

Returnb.ToString
EndFunction

EndClass

PrivateClassappInformation
<Description("程序名称")>_
PublicReadOnlyPropertyName()AsString
Get
ReturnSystem.AppDomain.CurrentDomain.SetupInformation.ApplicationName
EndGet
EndProperty

<Description("程序目录")>_
PublicReadOnlyPropertyPath()AsString
Get
ReturnSystem.AppDomain.CurrentDomain.SetupInformation.ApplicationBase
EndGet
EndProperty

<Description("配置文件")>_
PublicReadOnlyPropertyConfig()AsString
Get
ReturnSystem.AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
EndGet
EndProperty

<Description("程序入口")>_
PublicReadOnlyPropertyentryPoint()AsString
Get
ReturnSystem.Reflection.Assembly.GetEntryAssembly.EntryPoint.ToString
EndGet
EndProperty

PublicSharedShadowsFunctionToString()AsString
DimmInfoAsNewappInformation
DimbAsNewSystem.Text.StringBuilder
b.Append(
NewString("*"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(
NewString("*"c,80))
Returnb.ToString
EndFunction
EndClass
EndClass
EndNamespace


应用
 

DimMethodTestAsNewLzmTW.uReflection.CurrentMethodInfo
PublicSubLoad(ByValmainformAsSystem.Windows.Forms.Form)ImplementsLzmTW.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()

EndSub

 

PrivateSubOnUILoad(ByValsenderAsObject,ByValeAsEventArgs)
MethodTest.Input(
NewSystem.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()

EndSub


输出结果:

********************************************************************************
**程序名称:MyComputer.vshost.exe
**程序目录:G:/DocumentsandSettings/LzmTW/MyDocuments/VisualStudio2005/Projects/MyComputer/MyComputer/bin/Debug/
**配置文件:G:/DocumentsandSettings/LzmTW/MyDocuments/VisualStudio2005/Projects/MyComputer/MyComputer/bin/Debug/MyComputer.vshost.exe.config
**程序入口:VoidMain(System.String[])
********************************************************************************
**程序集名称:ComputerAddIns
**程序集版本:1.0.0.0
**类名:ComputerAddIns.wmiQueryApplication
**方法名:VoidLoad(System.Windows.Forms.Form)

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

********************************************************************************
**程序名称:MyComputer.vshost.exe
**程序目录:G:/DocumentsandSettings/LzmTW/MyDocuments/VisualStudio2005/Projects/MyComputer/MyComputer/bin/Debug/
**配置文件:G:/DocumentsandSettings/LzmTW/MyDocuments/VisualStudio2005/Projects/MyComputer/MyComputer/bin/Debug/MyComputer.vshost.exe.config
**程序入口:VoidMain(System.String[])
********************************************************************************
**程序集名称:ComputerAddIns
**程序集版本:1.0.0.0
**类名:ComputerAddIns.wmiQueryApplication
**方法名:VoidOnUILoad(System.Object,System.EventArgs)

**测试用时:00:00:01.3316949
**测试时间:2006-8-170:03:12
********************************************************************************