• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1095
  • Last Modified:

Using Reflection to Call a Friend Function

In my .NET 3.5 VB project, I'm trying to use reflection to call a friend subroutine on an object. Unfortunately, I haven't been able to get any of the Friend Subs or Friend Functions to return from any of Type.GetXXXX functions.

Can Friend functions/Subs be accessed with reflection? How?

Thanks!
Jeff
0
fhdigital
Asked:
fhdigital
  • 2
  • 2
1 Solution
 
Fernando SotoRetiredCommented:
Hi fhdigital;

Here is a code snippet to show how to do it.

Fernando

Imports System.Reflection
 
Public Class Form1
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 
        Dim tc As New TestClass()
        Dim tcSayHello As MethodInfo = tc.GetType().GetMethod("SayHello", _
                                                    BindingFlags.NonPublic Or BindingFlags.Instance)
        tcSayHello.Invoke(tc, Nothing)
 
        Dim tcGetMessage As MethodInfo = tc.GetType().GetMethod("GetMessage", _
                                                      BindingFlags.NonPublic Or BindingFlags.Instance)
        Dim params() As Object = {"Fernando Soto"}
        MessageBox.Show(tcGetMessage.Invoke(tc, params))
 
    End Sub
End Class
 
==========================================================
 
Public Class TestClass
 
    Friend Sub SayHello()
        Console.WriteLine("Hello")
    End Sub
 
    Friend Function GetMessage(ByVal name As String) As String
        Return "Hello " & name
    End Function
 
End Class

Open in new window

0
 
fhdigitalAuthor Commented:
Thanks Fernando. I'll check it out.
0
 
fhdigitalAuthor Commented:
That worked great! Thanks. I had tried the NonPublic and Instance BindingFlags previously but never together. Works like a charm!
0
 
Fernando SotoRetiredCommented:
Not a problem, glad I was able to help.  ;=)
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now