Using Reflection to Call a Friend Function

Posted on 2008-10-21
Last Modified: 2013-12-17
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?

Question by:fhdigital
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 63

Accepted Solution

Fernando Soto earned 500 total points
ID: 22771856
Hi fhdigital;

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


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()
    End Sub
    Friend Function GetMessage(ByVal name As String) As String
        Return "Hello " & name
    End Function
End Class

Open in new window


Author Comment

ID: 22777358
Thanks Fernando. I'll check it out.

Author Comment

ID: 22777533
That worked great! Thanks. I had tried the NonPublic and Instance BindingFlags previously but never together. Works like a charm!
LVL 63

Expert Comment

by:Fernando Soto
ID: 22781843
Not a problem, glad I was able to help.  ;=)

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL ( and MongoDB (…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question