Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Data Viewer - Form1.vb

Form1.vb

Caricato da: Totem
Scarica il programma completo

  1. Imports System.Reflection
  2. Public Class Form1
  3.     Private TypeDisplayed As New List(Of String)
  4.     Private Relations As New List(Of InheritanceRelation)
  5.  
  6.     Private Function GetRelation(ByVal CBox As ClassBox) As InheritanceRelation
  7.         Dim Base As Type = DirectCast(CBox.Tag, Type)
  8.  
  9.         'If Base Is GetType(Object) Then
  10.         'Return Nothing
  11.         'End If
  12.  
  13.         For Each C As Control In splitMain.Panel1.Controls
  14.             If TypeOf C Is ClassBox Then
  15.                 Dim Box As ClassBox = DirectCast(C, ClassBox)
  16.                 Dim Typ As Type = DirectCast(C.Tag, Type)
  17.                 Dim BaseType As Type = DirectCast(Box.Tag, Type).BaseType
  18.                 If BaseType Is Base Then
  19.                     Dim R As New InheritanceRelation(CBox, Box)
  20.                     R.Color = Color.LightBlue
  21.                     If Not Relations.Contains(R) Then Return R
  22.                 End If
  23.                 If Base.IsInterface And Base.IsAssignableFrom(Typ) Then
  24.                     Dim R As New InheritanceRelation(CBox, Box)
  25.                     R.Color = Color.Fuchsia
  26.                     If Not Relations.Contains(R) Then Return R
  27.                 End If
  28.             End If
  29.         Next
  30.  
  31.         Return Nothing
  32.     End Function
  33.  
  34.     Private Function AddClassBox(ByVal T As Type, Optional ByVal ControlRelation As Boolean = False) As ClassBox
  35.         Dim CBox As New ClassBox
  36.         If Not TypeDisplayed.Contains(T.FullName) Then
  37.             TypeDisplayed.Add(T.FullName)
  38.             CBox.SetTypeName(T)
  39.             CBox.Name = T.Name
  40.             CBox.ForeColor = Color.White
  41.             CBox.Font = New Font("Microsoft Sans Serif", 10, FontStyle.Regular)
  42.             CBox.Tag = T
  43.             CBox.ContextMenuStrip = cntScanType
  44.             splitMain.Panel1.Controls.Add(CBox)
  45.             AddHandler CBox.ItemClicked, AddressOf ClassBoxes_ItemClicked
  46.             AddHandler CBox.DoubleClick, AddressOf ClassBoxes_DoubleClick
  47.             AddHandler CBox.Move, AddressOf ClassBoxes_Move
  48.  
  49.             If ControlRelation Then
  50.                 Dim R As InheritanceRelation = GetRelation(CBox)
  51.                 If R IsNot Nothing Then
  52.                     Relations.Add(R)
  53.                 End If
  54.             End If
  55.  
  56.             Return CBox
  57.         Else
  58.             Return Nothing
  59.         End If
  60.     End Function
  61.  
  62.     Private Sub strLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strLoad.Click
  63.         Dim Open As New OpenFileDialog
  64.         Open.Filter = "Assemblies .NET|*.exe;*.dll"
  65.         If Open.ShowDialog = Windows.Forms.DialogResult.OK Then
  66.             Dim Asm As Assembly
  67.             Dim X, Y As Int32
  68.             Asm = Assembly.LoadFile(Open.FileName)
  69.             X = 20
  70.             Y = 40
  71.             For Each T As Type In Asm.GetTypes
  72.                 Dim CBox As New ClassBox
  73.                 If Not TypeDisplayed.Contains(T.FullName) Then
  74.                     CBox = AddClassBox(T)
  75.                     If X + 5 + CBox.Width > splitMain.Panel1.Width - 10 Then
  76.                         X = 20
  77.                         Y += 80
  78.                     End If
  79.                     CBox.Location = New Point(X, Y)
  80.                     X += CBox.Width + 5
  81.                 End If
  82.             Next
  83.             For Each C As Control In splitMain.Panel1.Controls
  84.                 If TypeOf C Is ClassBox Then
  85.                     Dim R As InheritanceRelation = GetRelation(C)
  86.                     If R IsNot Nothing Then
  87.                         Relations.Add(R)
  88.                     End If
  89.                 End If
  90.             Next
  91.         End If
  92.     End Sub
  93.  
  94.     Private Sub strAnalyze_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strAnalyze.Click
  95.         If Not TypeOf sender Is ToolStripMenuItem Then Exit Sub
  96.  
  97.         Dim ToolStrip As ToolStripMenuItem = DirectCast(sender, ToolStripMenuItem)
  98.         Dim ContextMenu As ContextMenuStrip = ToolStrip.Owner
  99.         Dim Box As ClassBox = ContextMenu.SourceControl
  100.  
  101.         Box.ScanClass(Box.Tag)
  102.     End Sub
  103.  
  104.     Private Sub strCompress_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strCompress.Click
  105.         If Not TypeOf sender Is ToolStripMenuItem Then Exit Sub
  106.  
  107.         Dim ToolStrip As ToolStripMenuItem = DirectCast(sender, ToolStripMenuItem)
  108.         Dim ContextMenu As ContextMenuStrip = ToolStrip.Owner
  109.         Dim Box As ClassBox = ContextMenu.SourceControl
  110.  
  111.         Box.Minimize()
  112.     End Sub
  113.  
  114.     Private Sub strExpand_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strExpand.Click
  115.         If Not TypeOf sender Is ToolStripMenuItem Then Exit Sub
  116.  
  117.         Dim ToolStrip As ToolStripMenuItem = DirectCast(sender, ToolStripMenuItem)
  118.         Dim ContextMenu As ContextMenuStrip = ToolStrip.Owner
  119.         Dim Box As ClassBox = ContextMenu.SourceControl
  120.  
  121.         Box.ScanClass(Box.Tag, True)
  122.     End Sub
  123.  
  124.     Private Sub strClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strClear.Click
  125.         If Not TypeOf sender Is ToolStripMenuItem Then Exit Sub
  126.  
  127.         Dim ToolStrip As ToolStripMenuItem = DirectCast(sender, ToolStripMenuItem)
  128.         Dim ContextMenu As ContextMenuStrip = ToolStrip.Owner
  129.         Dim Box As ClassBox = ContextMenu.SourceControl
  130.  
  131.         Box.ClearAll()
  132.     End Sub
  133.  
  134.     Private Sub ClassBoxes_ItemClicked(ByVal sender As Object, ByVal e As ClassBox.ItemClickedArgs)
  135.         Dim StartType As Type = DirectCast(sender, ClassBox).Tag
  136.         Dim MI As MemberInfo = e.Item.Tag
  137.         Dim Desc As New System.Text.StringBuilder
  138.         Dim RelatedTypes As New List(Of Type)
  139.  
  140.         Desc.AppendLine("Informazioni sul tipo: ")
  141.         Desc.AppendLine(AssemblyScanner.GetTypeInfo(StartType))
  142.         Desc.AppendLine()
  143.         Desc.AppendLine("Informazioni sul membro: ")
  144.         Desc.AppendLine(AssemblyScanner.GetMemberInfo(MI))
  145.         Desc.AppendLine()
  146.         If e.Item.ExposedMembers.Count > 0 Then
  147.             Desc.AppendFormat("Questo membero espone anche {0} overloads:{1}", e.Item.ExposedMembers.Count, vbCrLf)
  148.             Desc.AppendLine()
  149.             For I As Int16 = 0 To e.Item.ExposedMembers.Count - 1
  150.                 Desc.AppendFormat("Informazioni sull'overload [{0}]:{1}", I, vbCrLf)
  151.                 Desc.AppendLine(AssemblyScanner.GetMemberInfo(e.Item.ExposedMembers(I)))
  152.                 Desc.AppendLine()
  153.             Next
  154.         End If
  155.         If MI.GetCustomAttributes(False).Length > 0 Then
  156.             Desc.AppendLine()
  157.             Desc.AppendLine("Inoltre, il tipo espone anche i seguenti attributi:")
  158.             Desc.AppendLine(AssemblyScanner.GetMemberAttributes(MI))
  159.         End If
  160.         txtDescription.Text = Desc.ToString
  161.  
  162.         RelatedTypes = AssemblyScanner.GetMemberRelatedTypes(MI)
  163.         lstRelatedTypes.Items.Clear()
  164.         For Each T As Type In RelatedTypes
  165.             If Not lstRelatedTypes.Items.Contains(T) Then
  166.                 lstRelatedTypes.Items.Add(T)
  167.             End If
  168.         Next
  169.  
  170.         For Each C As Control In splitMain.Panel1.Controls
  171.             If TypeOf C Is ClassBox Then
  172.                 If MI.Equals(C.Tag) Then
  173.                     With DirectCast(C, ClassBox)
  174.                         .Color = Color.SeaGreen
  175.                         .Refresh()
  176.                     End With
  177.                 End If
  178.             End If
  179.         Next
  180.     End Sub
  181.  
  182.     Private Sub ClassBoxes_DoubleClick(ByVal sender As Object, ByVal e As EventArgs)
  183.         Dim StartType As Type = DirectCast(sender, ClassBox).Tag
  184.         Dim Desc As New System.Text.StringBuilder
  185.  
  186.         Desc.AppendLine("Informazioni sul tipo: ")
  187.         Desc.AppendLine(AssemblyScanner.GetTypeInfo(StartType))
  188.         If StartType.GetCustomAttributes(False).Length > 0 Then
  189.             Desc.AppendLine()
  190.             Desc.AppendLine("Inoltre, il tipo espone anche i seguenti attributi:")
  191.             Desc.AppendLine(AssemblyScanner.GetMemberAttributes(StartType))
  192.         End If
  193.         txtDescription.Text = Desc.ToString
  194.     End Sub
  195.  
  196.     Private Sub ClassBoxes_Move(ByVal sender As Object, ByVal e As EventArgs)
  197.         If Relations.Count > 0 Then
  198.             For Each R As InheritanceRelation In Relations
  199.                 If sender Is R.Base Or sender Is R.Derived Then
  200.                     splitMain.Panel1.Refresh()
  201.                 End If
  202.             Next
  203.         End If
  204.     End Sub
  205.  
  206.     Private Sub lstRelatedTypes_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstRelatedTypes.DoubleClick
  207.         If lstRelatedTypes.SelectedIndex >= 0 Then
  208.             Dim CBox As ClassBox = AddClassBox(lstRelatedTypes.SelectedItem, True)
  209.             If CBox Is Nothing Then
  210.                 MessageBox.Show("Questo tipo è già presente nell'explorer!", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  211.                 Exit Sub
  212.             End If
  213.             CBox.Location = New Point(splitMain.Panel1.Width / 2 - CBox.Width / 2, splitMain.Panel1.Height / 2 - CBox.Height / 2)
  214.         End If
  215.     End Sub
  216.  
  217.     Private Sub strDeleteAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strDeleteAll.Click
  218.         splitMain.Panel1.Controls.Clear()
  219.     End Sub
  220.  
  221.     Private Sub strDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strDelete.Click
  222.         If Not TypeOf sender Is ToolStripMenuItem Then Exit Sub
  223.  
  224.         Dim ToolStrip As ToolStripMenuItem = DirectCast(sender, ToolStripMenuItem)
  225.         Dim ContextMenu As ContextMenuStrip = ToolStrip.Owner
  226.         Dim Box As ClassBox = ContextMenu.SourceControl
  227.  
  228.         splitMain.Panel1.Controls.Remove(Box)
  229.  
  230.         RemoveHandler Box.Move, AddressOf ClassBoxes_Move
  231.         RemoveHandler Box.DoubleClick, AddressOf ClassBoxes_DoubleClick
  232.         RemoveHandler Box.ItemClicked, AddressOf ClassBoxes_ItemClicked
  233.  
  234.         Dim ToRemove As New List(Of InheritanceRelation)
  235.         For Each R As InheritanceRelation In Relations
  236.             If R.Derived Is Box Or R.Base Is Box Then
  237.                 ToRemove.Add(R)
  238.             End If
  239.         Next
  240.         For Each R As InheritanceRelation In ToRemove
  241.             Relations.Remove(R)
  242.         Next
  243.  
  244.         Box.Dispose()
  245.         splitMain.Panel1.Refresh()
  246.     End Sub
  247.  
  248.     Private Sub splitMain_Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles splitMain.Panel1.Paint
  249.         If Relations.Count > 0 Then
  250.             Dim Pen As Pen
  251.             Dim BCenter, DCenter As Point
  252.  
  253.             e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
  254.             For Each R As InheritanceRelation In Relations
  255.                 Pen = New Pen(Color.FromArgb(190, R.Color), 6)
  256.                 BCenter = New Point(R.Base.Location.X + R.Base.Width / 2, R.Base.Location.Y + R.Base.Height / 2)
  257.                 DCenter = New Point(R.Derived.Location.X + R.Derived.Width / 2, R.Derived.Location.Y + R.Derived.Height / 2)
  258.                 e.Graphics.DrawLine(Pen, BCenter, DCenter)
  259.             Next
  260.         End If
  261.     End Sub
  262.  
  263.     Private Sub strLimitHeight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strLimitHeight.Click
  264.         If Not TypeOf sender Is ToolStripMenuItem Then Exit Sub
  265.  
  266.         Dim ToolStrip As ToolStripMenuItem = DirectCast(sender, ToolStripMenuItem)
  267.         Dim ContextMenu As ContextMenuStrip = ToolStrip.Owner
  268.         Dim Box As ClassBox = ContextMenu.SourceControl
  269.  
  270.         If Not Box.Collapsed And Box.Height < splitMain.Panel1.Height Then
  271.             Box.Height = splitMain.Panel1.Height * 4 / 5
  272.             Me.Refresh()
  273.         End If
  274.     End Sub
  275. End Class