✅ 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
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
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
what you want is https://www.nuget.org/packages/System.Reflection.MetadataLoadContext
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...
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
Looks very promising, I'll gotta read into it, thank you :)
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 loadedbitness? Like, endianness?
what is bitness
the architecture, presumably
oh oh
gotcha
what you will need is something like
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
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
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
Okay I used
Mono.Cecil
and it worked like a charm!