Solved

Using Reflection to Call a Friend Function

Posted on 2008-10-21
4
1,092 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 63

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 63

Expert Comment

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
I need help adding validation to my MVC.Net view 6 23
VB.Net Data Class 1 22
CHANGE FORM TEXT BEFORE IT SHOW TO USER 13 19
aspx ascx, c# 7 18
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

733 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