home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 June (DVD) / DPPRO0605DVD.iso / dotNETSDK / SETUP.EXE / netfxsd1.cab / FL_Invoke_vb________.3643236F_FC70_11D3_A536_0090278A1BB8 < prev    next >
Encoding:
Text File  |  2002-04-20  |  5.1 KB  |  147 lines

  1. '=====================================================================
  2. '  File:      Invoke.vb
  3. '
  4. '  Summary:   Demonstrates how to use reflection invoke.
  5. '
  6. '---------------------------------------------------------------------
  7. '  This file is part of the Microsoft .NET SDK Code Samples.
  8. '
  9. '  Copyright (C) Microsoft Corporation.  All rights reserved.
  10. '
  11. 'This source code is intended only as a supplement to Microsoft
  12. 'Development Tools and/or on-line documentation.  See these other
  13. 'materials for detailed information regarding Microsoft code samples.
  14. '
  15. 'THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  16. 'KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  17. 'IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  18. 'PARTICULAR PURPOSE.
  19. '=====================================================================
  20.  
  21. Option Explicit On 
  22. Option Strict On
  23.  
  24.  
  25. Imports System
  26. Imports System.Globalization
  27. Imports System.IO
  28. Imports System.Text
  29. Imports System.Threading
  30. Imports System.Reflection
  31. Imports System.Reflection.Emit
  32. Imports Microsoft.VisualBasic
  33.  
  34. Public Class App
  35.     
  36.     Public Shared Sub Main(args() As String)
  37.  
  38.         If args.Length < 3 Then
  39.             Usage()
  40.             Return
  41.         End If
  42.  
  43.         Dim assemblyObj As System.Reflection.Assembly
  44.         Dim typeObj As Type
  45.  
  46.         Try
  47.             ' Load the requested assembly andget the requested type
  48.             assemblyObj = System.Reflection.Assembly.LoadFrom(args(0))
  49.             typeObj = assemblyObj.GetType(args(1), True, True)
  50.         Catch e As FileNotFoundException
  51.             Console.WriteLine("Cannot load assembly: {0}", args(0))
  52.             Return
  53.         Catch e As TypeLoadException
  54.             Console.WriteLine("Cannot load type: {0} from assembly: {1}", args(1), args(0))
  55.             Return
  56.         End Try
  57.  
  58.         ' Get the methods from the type
  59.         Dim methods As MethodInfo() = typeObj.GetMethods()
  60.  
  61.         If methods Is Nothing Then
  62.             Console.WriteLine("No Matching Types Found")
  63.             Return
  64.         End If
  65.  
  66.         ' Create a new array that holds only the args for the call
  67.         Dim newArgs(args.Length - 4) As String
  68.         If newArgs.Length <> 0 Then
  69.             Array.Copy(args, 3, newArgs, 0, newArgs.Length)
  70.         End If
  71.  
  72.         ' Try each of the type's methods for a match
  73.         Dim failureExcuses As StringBuilder = New StringBuilder()
  74.         Dim m As MethodInfo
  75.         For Each m In methods
  76.             Dim obj As Object = Nothing
  77.             Try
  78.                 obj = AttemptMethod(typeObj, m, args(2), newArgs)
  79.             Catch E As CustomException
  80.                 failureExcuses.Append(e.Message & vbCrLf)
  81.             End Try
  82.             If Not obj Is Nothing Then
  83.                 Console.WriteLine(obj)
  84.         Return
  85.             End If
  86.         Next m
  87.  
  88.         Console.WriteLine("Suitable method not found!")
  89.         Console.WriteLine("Here are the reasons:" & vbCrLf & failureExcuses.ToString())
  90.     End Sub 'Main
  91.  
  92.  
  93.     Private Shared Function AttemptMethod(ByVal typeObj As Type, ByVal method As MethodInfo, ByVal name As String, ByVal args() As String) As Object
  94.  
  95.         ' Name does not match?
  96.         If String.Compare(method.Name, name, True, CultureInfo.InvariantCulture) <> 0 Then
  97.             Throw New CustomException(method.DeclaringType.ToString() & "." & method.Name & ": Method Name Doesn't Match!")
  98.         End If
  99.  
  100.         ' Wrong number of parameters?
  101.         Dim param As ParameterInfo() = method.GetParameters()
  102.         If param.Length <> args.Length Then
  103.             Throw New CustomException(method.DeclaringType.ToString() & "." & method.Name & ": Method Signatures Don't Match!")
  104.         End If
  105.  
  106.         ' Can we convert the strings to the right types?
  107.         Dim newArgs(args.Length - 1) As Object
  108.         Dim index As Integer
  109.         For index = 0 To args.Length - 1
  110.             Try
  111.                 newArgs(index) = Convert.ChangeType(args(index), param(index).ParameterType)
  112.             Catch e As Exception
  113.                 Throw New CustomException(method.DeclaringType.ToString() & "." & method.Name & ": Parameter Conversion Failed", e)
  114.             End Try
  115.         Next index
  116.  
  117.         ' Does the type need an instance?
  118.         Dim instance As Object = Nothing
  119.         If Not method.IsStatic Then
  120.             instance = Activator.CreateInstance(typeObj)
  121.         End If
  122.  
  123.         ' Invoke the method
  124.         Dim retVal As Object = method.Invoke(instance, newArgs)
  125.         If Not retVal Is Nothing Then
  126.             Return retVal
  127.         Else
  128.             Return String.Empty
  129.         End If
  130.     End Function 'AttemptMethod
  131.  
  132.     Private Shared Sub Usage()
  133.         Console.WriteLine(("Usage:" & ControlChars.CrLf & "   Invoke [Assembly] [Type] [Method] [Parameters]"))
  134.     End Sub 'Usage
  135.  
  136.     Private Class CustomException
  137.         Inherits Exception
  138.         Public Sub New(ByVal m As String)
  139.             MyBase.New(m)
  140.         End Sub
  141.         Public Sub New(ByVal m As String, ByVal n As Exception)
  142.             MyBase.New(m, n)
  143.         End Sub
  144.     End Class ' CustomException
  145.  
  146. End Class 'App
  147.