Solved

Using Reflection to Call a Friend Function

Posted on 2008-10-21
4
1,088 Views
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?

Thanks!
Jeff
0
Comment
Question by:fhdigital
  • 2
  • 2
4 Comments
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 22771856
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
 
LVL 1

Author Comment

by:fhdigital
ID: 22777358
Thanks Fernando. I'll check it out.
0
 
LVL 1

Author Comment

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

Expert Comment

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now