dnlib | Reads and writes .NET assemblies and modules
kandi X-RAY | dnlib Summary
kandi X-RAY | dnlib Summary
.NET module/assembly reader/writer library.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of dnlib
dnlib Key Features
dnlib Examples and Code Snippets
Community Discussions
Trending Discussions on dnlib
QUESTION
I'm trying to generate c# code from IL instructions using the dnlib library.
The code I'm trying to generate is this one:
...ANSWER
Answered 2021-Oct-31 at 00:13while trying some stuff, I saw that I needed to be using the main object (from dnlib) that has the .NET module loaded (ModuleDefMD
).
So I did it this way:
QUESTION
What I am doing (partly for fun and learning, partly hopefully as a serious virtualization effort someday) is merging my VM dll with a target assembly via ILMerge.
Only afterwards I modify the newly created file with dnlib to replace the method bodies of selected methods with a call to my VM function. I pass the required metadata that is now absent in the method itself via a base64 encoded binary string and obviously the arguments and the old method body too (in the future I want to implement my own bytecode instruction set for that, but so far its just the original code base64-encoded).
Since .initlocals is from my experience always set in a .NET method, what I want to do is save the type of every local as data in such a way that I can initialize my local array in the Virtualizer runtime with it.
My current approach is just to save the MDToken writer.Write(local.Type.ToTypeDefOrRef().GetNonNestedTypeRefScope().MDToken.ToInt32());
I write my changes to the assembly with the PreserveAll flag opts.MetadataOptions.Flags = dnlib.DotNet.Writer.MetadataFlags.PreserveAll;
and in the runtime resolve the MDToken via
...ANSWER
Answered 2021-May-16 at 10:18However, a metadata token is not a persistent identifier. Rather it is scoped to a specific metadata binary.
What it's saying is that the metadata token is only meaningful within the scope of the module, you can't take the metadata token from one module and use it in another or even within a modified version of the same module (or at least not reliably).
The reason for this makes more sense when you consider what the metadata token really is. The metadata token is a reference to a record in a metadata table within the module, and that record contains further details; the high-order byte of the metadata token indicates the type of token (and thus the table containing the record), while the remaining 3 bytes indicate the row number.
If you take a metadata token from one module and try to use it in another module, you are assuming that the same record in each module represents the same thing. If you compile the same code with the same compiler, then this assumption may hold; but if you change the source or use a different compiler (or different version of the same compiler) then the row numbers may change for any number of reasons.
And why does this problem not occur at all in the method body instructions? Many instructions encode an InlineType and Module.ResolveType has never failed there.
Because the compiler emitting the IL with the metadata token is also emitting the tables into the same file. The compiler is able to keep these things in sync.
And, more importantly, how to fix? How do I save a reliable type identifier in a binary form for the locals of a method?
The only reliable way to reference a type between modules, is with the full type name and scope (the containing assembly, module or type in the case of a nested type).
QUESTION
I am using dnLib to generate MSIL assemblies dynamically from a custom language I'm writing, named CSASM:
...ANSWER
Answered 2021-Mar-17 at 18:32After a very thorough examination of a dnLib DLL, the problem was due to me using Importer.ImportDeclaringType(Type).ToTypeDefOrRef()
, which caused value-type TypeSig
s to be registered as class-type TypeSig
s instead.
Even though the docs say to use Importer.ImportDeclaringType(Type)
over Importer.ImportAsTypeSig(Type)
for method and field declarations, you really shouldn't be using it.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install dnlib
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page