Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

Read .NET assembly classes

Hi, I need help with the use of:

github.com/0xd4d/dnlib

Basically I want to list all the classes of a .Net assembly.

I want to get all the names of the classes and their methods and list them in a listbox.

But for some reason I can't do it.

This is what I want to get: (Only the names of the classes.)

[Image: 1asd.png]

Thank you
If I understand your inquiry correctly, something like this is what you'd be looking for:
Code
   Private Function GetMethods() as List(Of String)
       Return _
           (From type In Assembly.GetExecutingAssembly.GetTypes
               From _
                   method In _
                       type.GetMethods(
                           BindingFlags.Public Or BindingFlags.NonPublic Or BindingFlags.Instance Or
                           BindingFlags.Static) Select type.FullName & ":" & method.Name).Distinct.ToList
   End Function

If all you want is just the classes, just use GetTypes on an Assembly object:
Code
Assembly.GetTypes()
[Image: vp6iypF.gif]
(Nov 2, 2019 11:44 AM)MavenGradle Wrote:  If I understand your inquiry correctly, something like this is what you'd be looking for:
Code
   Private Function GetMethods() as List(Of String)
       Return _
           (From type In Assembly.GetExecutingAssembly.GetTypes
               From _
                   method In _
                       type.GetMethods(
                           BindingFlags.Public Or BindingFlags.NonPublic Or BindingFlags.Instance Or
                           BindingFlags.Static) Select type.FullName & ":" & method.Name).Distinct.ToList
   End Function

If all you want is just the classes, just use GetTypes on an Assembly object:
Code
Assembly.GetTypes()

That's not an implementation of dnlib usage:

Quote:Hi, I need help with the use of:

github.com/0xd4d/dnlib


@OP: What's wrong with the examples in the repo? https://github.com/0xd4d/dnlib/blob/mast...xample1.cs
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
(Nov 2, 2019 02:06 PM)0xDEAD10CC Wrote:  That's not an implementation of dnlib usage:


@OP: What's wrong with the examples in the repo? https://github.com/0xd4d/dnlib/blob/mast...xample1.cs

This should suffice for dnlib
Code
Private Function GetTypes() as List(Of String)
    Dim modDef As ModuleDefMD = ModuleDefMD.Load(Assembly.GetExecutingAssembly)
    Return (From t In modDef.GetTypes Select T.Name.String).ToList
  End Function

& If you want method names:
Code
   Dim modDef As ModuleDefMD = ModuleDefMD.Load(Assembly.GetExecutingAssembly)
   Return (From t In modDef.GetTypes From m In t.Methods Select t.Name.String & ":" & m.Name.String).ToList
[Image: vp6iypF.gif]
(Nov 2, 2019 02:17 PM)MavenGradle Wrote:  This should suffice for dnlib
Code
 Private Function GetTypes() as List(Of String)
   Dim modDef As ModuleDefMD = ModuleDefMD.Load(Assembly.GetExecutingAssembly)
   Return (From t In modDef.GetTypes Select CType(t.Name, String)).ToList
 End Function

& If you want method names:
Code
   Dim modDef As ModuleDefMD = ModuleDefMD.Load(Assembly.GetExecutingAssembly)
   Return (From t In modDef.GetTypes From m In t.Methods Select t.Name.ToString & ":" & m.Name.ToString).ToList

Ugh, VB.net looks so ugly. Why are you using Ctype though in the first?
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
(Nov 2, 2019 04:57 PM)0xDEAD10CC Wrote:  Ugh, VB.net looks so ugly. Why are you using Ctype though in the first?

I absolutely 100% agree with you. This is the first time I've written VB.NET in approximately 12 years.
Unfortunately the Name property is an UTF-8 string and has to be converted to UTF-16 in this particular case because I'm returning a list of type String which by default is UTF-16 encoded. You could use the ToString method as well if that is preferred if it wasn't for the fact that a range variable name cannot match the name of a member of the 'Object' class.

You'd have to use something like:

Code
(From t In modDef.GetTypes() Select t.Name).Select(Function(dummy) CType(dummy, String))
[Image: vp6iypF.gif]
(Nov 2, 2019 04:59 PM)MavenGradle Wrote:  I absolutely 100% agree with you. This is the first time I've written VB.NET in approximately 12 years.
Unfortunately the Name property is an UTF-8 string and has to be converted to UTF-16 in this particular case because I'm returning a list of type String which by default is UTF-16 encoded. You could use the ToString method as well if that is preferred if it wasn't for the fact that a range variable name cannot match the name of a member of the 'Object' class.

You'd have to use something like:

Code
(From t In modDef.GetTypes() Select t.Name).Select(Function(dummy) CType(dummy, String))

Yeah but the UTF8String type has a String property which is what gets returned via ToString() since it's overriden.

The implementation already takes care of this.
Code
public string String
{
    get
    {
        if (this.asString == null)
        {
            this.asString = UTF8String.ConvertFromUTF8(this.data);
        }
        return this.asString;
    }
}

I don't see why you're using CType at all. There's even an implicit conversion in there which eventually leads to a System.String but adds an additional check in there to return null before returning that String property; it calls ToSystemString() -- a static function of that UTF8String class.
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
(Nov 2, 2019 05:25 PM)0xDEAD10CC Wrote:  Yeah but the UTF8String type has a String property which is what gets returned via ToString() since it's overriden.

The implementation already takes care of this.
Code
public string String
{
   get
   {
       if (this.asString == null)
       {
           this.asString = UTF8String.ConvertFromUTF8(this.data);
       }
       return this.asString;
   }
}

I don't see why you're using CType at all.

Aha, good catch. You're right! This would be the correct way to do it:
Code
 Private Function GetTypes() as List(Of String)
   Dim modDef As ModuleDefMD = ModuleDefMD.Load(Assembly.GetExecutingAssembly)
   Return (From t In modDef.GetTypes Select T.Name.String).ToList
 End Function

Code has been updated. dnlib thought of everything!
[Image: vp6iypF.gif]
Thanks for the answers.

I could use System.Reflection but it fails with executables protected with, for example LizGuard.

That's why I tried to use dnlib but I got a little confused and could not use it correctly.

The end I found a library called Mono.Cecil and it works perfectly.

Anyway thanks for answering. I already tried the code with dnlib and it works thanks.




Code you use with Mono.cecil :

PHP Code

<?php 
Dim allTypesDefinitaion 
= Mono.Cecil.ModuleDefinition.ReadModule(FileDir).Types 


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by gigajewNov 17, 2019 11:47 AM 17 1,549 Nov 17, 2019 11:47 AM
Last Post: gigajew
[C#] by gigajewJul 26, 2018 07:38 PM 10 802 Jul 26, 2018 07:38 PM
Last Post: techcrunch
[.NET] by UijaJul 21, 2018 02:38 AM 13 699 Jul 21, 2018 02:38 AM
Last Post: Ajumma