views:

51

answers:

2

I want to retrieve private (implementation and other) methods of a class which implements an interface and also is derived from (inherits) a base class.

  1. How can I achieve this using reflection?
  2. Is there anyother way to achieve this?

This is wat m tryin to do. I need to view these private methods and their contents, I don't want to invoke them.

Dim assembly As System.Reflection.Assembly 
Dim assemblyName As String assemblyName = System.IO.Path.GetFullPath("xyz.dll") 
assembly = System.Reflection.Assembly.LoadFile(assemblyName) 

assembly.GetType("myClass").Getmethods(Bindings.NonPublic)

assembly.GetType("myClass").GetMethods(BindingFlags.NonPublic) isn't working

+3  A: 

Making the assumption that you're trying to use the private methods of a 3rd party assembly:

I'd urge caution in using private methods, they have been made private for a reason and might rely on some state that you won't be able to anticipate; Also, if the assembly gets updated in the future, the private behavior may be changed or removed causing you to have to re-write your code to compensate.

However, there are two methods at your disposal, if you want to continue. One way us using the new Dynamic keyword in .net 4:

http://igoro.com/archive/use-c-dynamic-typing-to-conveniently-access-internals-of-an-object/

To use reflection, this question may help you:

http://stackoverflow.com/questions/135443/how-do-i-use-reflection-to-invoke-a-private-method-in-c

Hope this helps.

Russ C
+1  A: 

BindingFlags

You must specify Instance or Static along with Public or NonPublic or no members will be returned.

Anshul