Using Activator with a non-public constructor

rbulph

Junior Contributor
Joined
Feb 17, 2003
Messages
397
I'd like to use Activator.CreateInstance with a non-public constructor for a class of mine. Here's my code:

Code:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        Activator.CreateInstance(GetType(X1), Reflection.BindingFlags.NonPublic, Nothing, New Object() {8})

        Dim j As New X1(8)
    End Sub
End Class
Public Class X1
    Friend Sub New(ByVal p As Long)
        MsgBox(p)
    End Sub
End Class

but in spite of the use of the NonPublivc flag, I get the message that the constructor is not found. Maybe I need to set the binder parameter to something, but I've no idea what. Anyone know how to go about this?
 
Try:
Code:
Activator.CreateInstance(GetType(X1), Reflection.BindingFlags.NonPublic OR Reflection.BindingFlags.Instance OR Reflection.BindingFlags.Static,...
 
Try:
Code:
Activator.CreateInstance(GetType(X1), Reflection.BindingFlags.NonPublic OR Reflection.BindingFlags.Instance OR Reflection.BindingFlags.Static,...

I still get the error "Constructor on type 'WindowsApplication1.x1' not found."
 
Got it. Just need to specify the argument for the culture parameter - Nothing will do.
 
Back
Top