C
C#4mo ago
tolik518

✅ Reflection independent from .Net version and bitness?

I'm working on a tool (https://github.com/tolik518/JackTheEnumRipper) that dumps all the enums from a (.Net) binary and I figured out, pretty early in the developement, that the reflection on the assembly only works if the bitness and somewhat the .Net (framework) version corresponds to the binary. is there a way for me to detect the bitness and the needed framework early? Are there better ways then the way I'm currently doing it? In the first release I shipped 4 binaries, 2 of them are x64 and the other 2 are x86 - while one of them is always in* .Net 8.0 *and the other one is compiled with .Net Framework 4.8. But currently its a trial and error to find out which version one needs to run.
GitHub
GitHub - tolik518/JackTheEnumRipper: A CLI tool that extarcts Enums...
A CLI tool that extarcts Enums from an .exe/.dll which was written using C# - tolik518/JackTheEnumRipper
13 Replies
tolik518
tolik5184mo ago
My current idea would be to create a new solution with reads the pe header to determine the bitness and probably find out the needed .Net version and forward it to the correct executable. Otherwise I'm out of ideas
reflectronic
reflectronic4mo ago
yes, the problem is that you are loading the assembly. but you do not need to do that if you are just going to do reflection
reflectronic
reflectronic4mo ago
System.Reflection.MetadataLoadContext 8.0.0
Provides read-only reflection on assemblies in an isolated context with support for assemblies that target different processor architectures and runtimes. Using MetadataLoadContext enables you to inspect assemblies without loading them into the main execution context. Assemblies in MetadataLoadContext are treated only as metadata, that is, you c...
reflectronic
reflectronic4mo ago
the README on the package explains you would not need to change your code because it uses the same System.Type, System.Reflection.FieldInfo, etc. classes
tolik518
tolik5184mo ago
Looks very promising, I'll gotta read into it, thank you :)
reflectronic
reflectronic4mo ago
ok, well, actually there is at least one issue, so your code will have to change slightly Enum.GetValues will not work, because that returns instances of the enum type, which means the enum type would need to be loaded
Cali_West
Cali_West4mo ago
bitness? Like, endianness? what is bitness
reflectronic
reflectronic4mo ago
the architecture, presumably
Cali_West
Cali_West4mo ago
oh oh gotcha
reflectronic
reflectronic4mo ago
what you will need is something like
IEnumerable<(string Name, object Value)> GetEnumValues(Type type)
{
return type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static).Select(f => (f.Name, f.GetRawConstantValue());
}
IEnumerable<(string Name, object Value)> GetEnumValues(Type type)
{
return type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static).Select(f => (f.Name, f.GetRawConstantValue());
}
tolik518
tolik5184mo ago
Thanks for being so helpful and not only pushing me into the right direction but straight out giving me solutions to my problem :) I couldn't get up with a better term, but yeah, architecture makes sense
Cali_West
Cali_West4mo ago
yeah it's a bit late here too, I couldn't think of the name of it either hehe I think the info you're looking for is available, in some form, packed in the assembly metadata you'll have to just do some digging around. I get lost whenever I try and look through all that stuff
tolik518
tolik5184mo ago
Yeah but then I still would have to have multiple executable, I think I'll try the solution which @reflectronic proposed - this could solve all my issues at once I just did some quick test and it looks like I'll have to adjust some parts since it doesn't load all the dependencies like LoadFrom. Because I'm getting that exception when loading an assembly thats using xna
The specified assembly was not found.
Could not find assembly 'Microsoft.Xna.Framework.Game, Version=4.0.0.0, Culture=neutral, PublicKeyToken=842cf8be1de50553'. Either explicitly load this assembly using a method such as LoadFromAssemblyPath() or use a MetadataAssemblyResolver that returns a valid assembly.
The specified assembly was not found.
Could not find assembly 'Microsoft.Xna.Framework.Game, Version=4.0.0.0, Culture=neutral, PublicKeyToken=842cf8be1de50553'. Either explicitly load this assembly using a method such as LoadFromAssemblyPath() or use a MetadataAssemblyResolver that returns a valid assembly.
Okay I used Mono.Cecil and it worked like a charm!
Want results from more Discord servers?
Add your server
More Posts
Authentication error responsesWhat's the best way for ASP.NET Core Web Api to send authentication error responses to a frontend, s✅ Entityframework for framework 4.8 instead of .net / coreHello, I'm trying to use EF with framework 4.8. There is a version 6.4.4. When using this, I cant fAutoMapper: Ignoring Nested Property in Mapping Configuration Results in Null ValueI'm encountering an issue with AutoMapper where I'm trying to ignore a nested property during mappinAny Methods to Obtain POSIX Thread Handle for System.Threading.Thread in LinuxSo I am trying to find a way to obtain the pthread (POSIX thread) handles for my threads. I found a trying to deploy my app with docker with my problemsSo i have my container running, and when I go to localhost:8080 where the docker is running, it take.NET tool/library for communication/network loggingHye guys, I was asked to do some "research" to find some fancy/cool way to logging communication betstruggling with objects and methods```cs class Circle { float radius; int id; int nextID; static void initCircle() .runsettings Coverage Pattern Not WorkingHello can someone please help me fix my runsettings to only collect from the "Controllers" folder: `✅ How can I convert/flatten an 8-Digit Hex color (Alpha) to a 6-Digit Hex color (Assuming white BG)Does anyone here know how to convert an 8-Digit HEX color code to a 6-Digit HEX color code (AssumingAzure video analyzer:blue_siren: :blue_siren: Is there any azure service that I can use to analyze a video's content? I