2007年6月21日木曜日

[VB2005] メソッド名を取得する

トレースログなんかで、自メソッドを呼んだのは誰?なんて事を知りたいときが良くあります。
そんなときに使えそうな処理がわかりました。
クラス名とメソッド名が取得されます。

これから、Reflection関係を勉強してみたいです。

---------------------------

Dim st As StackTrace = New StackTrace(False)
Dim sf As StackFrame
Dim mb As System.Reflection.MethodBase

Dim sf0 As StackFrame = st.GetFrame(0)
Dim mb0 As System.Reflection.MethodBase = sf0.GetMethod()

Dim stackLoop As Int32 = 1

Dim className As String = String.Empty
Dim methodName As String = String.Empty
Do

sf = st.GetFrame(stackLoop)
mb = sf.GetMethod()

If (mb.ReflectedType.Name = mb0.ReflectedType.Name) Then
stackLoop += 1
Else
className = mb.ReflectedType.FullName
methodName = mb.Name
Exit Do
End If
'まぁ50も見とけば良いでしょう
If (50 < stackLoop) Then
Exit Do
End If
Loop

2007年6月8日金曜日

[SQL Server 2005] SQL Serverでテーブル定義取得

SQL Server 2005で、テーブル定義らしいものを取得するクエリです。
これを使って、テーブル定義書を自動生成させてます。

select
Col.name as '名称'
,(Select top 1 name From systypes Where systypes.xtype = Col.xtype) as '型'
,Col.length as '桁数'
,case isnull(Col.scale,0)
 when 0 then ' '
 else cast( Col.scale as char(10) )
 end '小数部'
,case  Col.isnullable
 when 0 then '○'
 else ' '
 end 'NN'
,isnull((select case colid when 0 then '' else '○' end  from sysindexkeys as keys where col.id = keys.id  and col.colid = keys.colid and keys.indid = 1),' ') as 'PK'
,isnull((select top 1 ex.value from sys.extended_properties as ex where col.id = ex.major_id and ex.minor_id = col.colid and ex.name = 'MS_Description' ),' ') as 'コメント'
From syscolumns as col
inner join sysobjects as obj on col.id = obj.id
Where obj.name = '<テーブル名>'