HOW TO:枚举程序集静态方法

Author:水如烟

现在发现,熟悉NET提供的静态方法是很重要的,不熟悉不熟知,会走好多弯路.
当然,走弯路也不是坏事,只是时间有限,经验和技能不能在弯路中积累.

下面的功能是看看指定类所在的程序集究竟公布了哪些静态方法.

Imports  System.Reflection

Public   Class  Form1

    
Private   Sub  Button1_Click( ByVal  sender  As  System.Object,  ByVal  e  As  System.EventArgs)  Handles  Button1.Click
        
Dim  mNode1  As  TreeNode  =  AssemblyStaticMethodNode( GetType ( Object ))
        
Dim  mNode2  As  TreeNode  =  AssemblyStaticMethodNode( GetType (Microsoft.VisualBasic.Strings))
        
With   Me .TreeView1
            .BeginUpdate()
            .Nodes.Clear()
            .Nodes.AddRange(
New  TreeNode() {mNode1, mNode2})
            .Sort()
            .EndUpdate()
        
End   With
    
End Sub

    
Public   Function  AssemblyStaticMethodNode( ByVal  t  As  Type)  As  TreeNode

        
Dim  mAssembly  As   Assembly   =   Assembly .GetAssembly(t)

        
Dim  mRootNode  As   New  TreeNode(mAssembly.GetName.Name)
        
Dim  mSecondNode  As  TreeNode  =   Nothing

        
Dim  mCurrentNode  As  TreeNode  =   Nothing
        
Dim  mCurrentTypeChanged  As   Boolean   =   False

        
Dim  mBinding  As  BindingFlags  =  BindingFlags.Public  &  BindingFlags.Static

        
For   Each  mModule  As  [ Module In  mAssembly.GetModules
            mSecondNode 
=  mRootNode.Nodes.Add(mModule.ScopeName)

            
For   Each  mType  As  Type  In  mModule.GetTypes

                
If  mType.IsClass  Then

                    mCurrentTypeChanged 
=   True
                    
For   Each  mMethod  As  MethodInfo  In  mType.GetMethods(mBinding)

                        
If  mCurrentTypeChanged  Then
                            mCurrentNode 
=  mSecondNode.Nodes.Add(mType.FullName)

                            mCurrentTypeChanged 
=   False
                        
End   If

                        mCurrentNode.Nodes.Add(mMethod.ToString)
                    
Next

                
End   If

            
Next
        
Next

        
Return  mRootNode
    
End Function
End Class

你可能感兴趣的:(静态方法)