x

In previous release of .NET Reflector, I was able to get the namespace of an instruction (fonction/sub) inside a function but now I can't and the doc does not exist.

.NET Reflector 6.8.2.5 (last free version)

This line was working but not now : instruction.Value.declaringtype.namespace.ToString

How do I get the namespace of an instruction?

Thx

Private Sub DesassamblerDLL(ByVal strDLLFile As String)
    Dim serviceProvider As New Reflector.ApplicationManager(Nothing)
    Dim assemblyManager As Reflector.CodeModel.IAssemblyManager
    Dim objAssembly As Reflector.CodeModel.IAssembly
    Dim objMod As Reflector.CodeModel.IModule
    Dim typeDeclaration As Reflector.CodeModel.ITypeDeclaration
    Dim methodDeclaration As Reflector.CodeModel.IMethodDeclaration

    assemblyManager = serviceProvider.GetService(GetType(Reflector.CodeModel.IAssemblyManager))
    objAssembly = assemblyManager.LoadFile(strDLLFile)

    For Each objMod In objAssembly.Modules
        For Each typeDeclaration In objMod.Types
            For Each methodDeclaration In typeDeclaration.Methods
                Dim methodBody As Reflector.CodeModel.IMethodBody = methodDeclaration.Body
                If Not methodBody Is Nothing Then
                    Dim instruction As Reflector.CodeModel.IInstruction
                    For Each instruction In methodBody.Instructions
                        str = instruction.Value.declaringtype.namespace.ToString
                    Next
                End If
            Next
        Next
    Next
End Sub
more ▼
asked Apr 17, 2012 at 03:39 PM naster 7 naster gravatar image
Clive gravatar image Clive ♦♦ Apr 17, 2012 at 06:09 PM
You don't say what error you're getting, but I imagine it's failing because the instruction.Value might not be something that supports the DeclaringType property IIRC the Value of the instruction can be all kinds of things - an integer in the case of a load constant il instruction, a string in the case of load string etc What is it you want the code to do?
naster gravatar image naster Apr 17, 2012 at 07:01 PM
If the instruction is a fonction/sub, I want to get the full path to the fonction like System.Data.Odbc.OdbcDataAdapter.Update(). Any documentation? What can I do to learn how reflector works? THX FOR YOUR HELP ;)
Clive gravatar image Clive ♦♦ Apr 17, 2012 at 07:12 PM
Ok. I'll try to put something together to help you.
(comments are locked)
10|600 characters needed characters left

Jason Haley posted a link to some resources that he wrote on the following forum post http://forums.reflector.net/questions/1200/using-net-reflector-api?page=1#1241

One thing to be aware of is the object model's notion of reference and declaration. For example, types can be an ITypeReference, an object that contains properties of the type such as its name and namespace, but to get the underlying type you need to use the Resolve() method to convert the ITypeReference into a ITypeDeclaration. The same applies to a IMethodReference and IMethodDeclaration. The best way to get a feeling for the model is to use Reflector on reflector looking at the reflector.CodelModel namespace.

Another is that the ITypeDeclaration can also contain nested types in the NestedTypes property.

You are probably only interested in the Value of an instruction for certain opcodes, so we might just be looking for virtual method calls in the IL stream.

foreach(IInstruction instruction in methodBody.Instructions)
{
  if (instruction.Code == Instruction.CallVirtual)
  {
    var targetMethodReference = instruction.Value as IMethodReference;
    var method = targetMethodReference.Resolve();
    var typeReference = method.DeclaringType as ITypeReference;
    Console.WriteLine(typeReference.Namespace + " " + method.Name);
  }
 }
We use Reflector programmatically in this way to do various things as part of our build process so the object model is really useful. We do need to do a lot more documentation of it though.
more ▼

answered Apr 18, 2012 at 09:28 AM

Clive gravatar image

Clive ♦♦
1.4k 1 9

naster gravatar image naster Apr 18, 2012 at 01:58 PM
Your rock!
naster gravatar image naster Apr 18, 2012 at 03:19 PM
Hey I just opened Reflector with reflector to see where this code is but I can't find... Instruction.CallVirtual A lot of things are obfuscated and I can't read anything.
Clive gravatar image Clive ♦♦ Apr 18, 2012 at 03:28 PM
Class Instruction in namespace Reflector.Disassembler Public Const CallVirtual As Integer = &H6F
naster gravatar image naster Apr 18, 2012 at 03:59 PM
Thx but I don't have this namespace "Reflector.Disassembler" in Reflector.exe 6.8.2.5.
Clive gravatar image Clive ♦♦ Apr 18, 2012 at 04:09 PM
I added some extra constants (such as the opcodes for the various IL instructions in 7.5). Sorry - I read your comment about 6.8 as saying that the code worked in 6.8 but not in the more recent EAP version.
(comments are locked)
10|600 characters needed characters left

Hi @Naster,

Jason's blog post, and several other resources, are actually linked-to from our introduction to the .NET Reflector add-in model:

http://www.reflector.net/introduction-to-the-net-reflector-add-in-model/

more ▼

answered Apr 18, 2012 at 03:35 PM

camassey gravatar image

camassey ♦♦
53 2 2 8

(comments are locked)
10|600 characters needed characters left

Everythings rock!

Big thank!

Imports system.Reflection.Emit

Select Case instruction.Code
    Case OpCodes.Call.Value, OpCodes.Calli.Value, OpCodes.Callvirt.Value
        Dim targetMethodReference As Reflector.CodeModel.IMethodReference = instruction.Value
        Dim method As Reflector.CodeModel.IMethodDeclaration = targetMethodReference.Resolve
        Dim typeReference As Reflector.CodeModel.ITypeReference = method.DeclaringType
        ...
End Select
more ▼

answered Apr 18, 2012 at 07:44 PM

naster gravatar image

naster
7 3 3 6

(comments are locked)
10|600 characters needed characters left
Your answer
osqa.question.ask.tags.preview.show

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.