Oct 31, 2019 06:50 AM
(This post was last modified: Oct 31, 2019 06:51 AM by fisica638.)
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.)
Thank you
Nov 2, 2019 11:44 AM
(This post was last modified: Nov 2, 2019 12:17 PM by MavenGradle.)
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:
Nov 2, 2019 02:06 PM
(This post was last modified: Nov 2, 2019 02:07 PM by 0xDEAD10CC.)
(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:
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
Nov 2, 2019 02:17 PM
(This post was last modified: Nov 2, 2019 05:32 PM by MavenGradle.)
(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
(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?
Nov 2, 2019 04:59 PM
(This post was last modified: Nov 2, 2019 05:17 PM by MavenGradle.)
(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))
Nov 2, 2019 05:25 PM
(This post was last modified: Nov 2, 2019 05:32 PM by 0xDEAD10CC.)
(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.
Nov 2, 2019 05:31 PM
(This post was last modified: Nov 2, 2019 05:45 PM by MavenGradle.)
(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!
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
|