How can I Invoke a private interface implementations of an instance using reflection?

Platform: WinForms| Category: General

Make sure to preface the method/property name with the fully qualified interface name that it belongs to, like this:


	// In C#
	// Where someType implements ISomeInterface’s SomeMethod as a private implementation.
	Type someType = someInstance.GetType();
	MethodInfo mi = someType.GetMethod('System.Windows.Forms.ISomeInterface.SomeMethod', 
			BindingFlags.NonPublic | BindingFlags.Instance);
	if(mi != null)
	{
		mi.Invoke(someInsance, new object[]{});
	}

	’ In VB.Net
	’ Where someType implements ISomeInterface’s SomeMethod as a private implementation.
	Dim someType As Type =  someInstance.GetType() 
	MethodInfo mi = someType.GetMethod('System.Windows.Forms.ISomeInterface.SomeMethod', 
			BindingFlags.NonPublic | BindingFlags.Instance)
	If Not mi Is Nothing Then
		’ Assuming no arguments for SomeMethod.
		mi.Invoke(someInsance, Nothing)
	End If

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.