Comments (3)
I'm not the Cecil author but I believe I can help here. I see people asking about this here quite often, so I suppose this should be added to the docs.
When you write something like module.ImportReference (typeof (MulticastDelegate))
, you're telling Cecil to import the MulticastDelegate
type from the runtime the code calling Cecil is running under.
In .NET (Core+), this means you'll get a reference to the runtime assembly defining MulticastDelegate
, aka System.Private.CoreLib
.
This happens to "work" in your case, but suppose you were editing a .NET Framework assembly from a .NET 7 app using Cecil, you'd still get a reference to System.Private.CoreLib
, which wouldn't work under .NET Framework at all.
If you'd like to end up with a reference to System.Runtime
, you'll need to use the reference assemblies instead of the implementation assemblies.
Which means you can't use the ImportReference (Type)
overload, because reflection will always reference the runtime type. You need to use ImportReference (TypeReference)
to do the proper thing.
I believe using ImportReference
overloads with the reflection overloads such as Type
/MethodReference
etc is almost always a mistake, unless you're going to load the modified assembly immediately after you're done editing it. These overloads were OK when the .NET Framework was the only framework around, and the issue I described here simply couldn't occur.
So I indirectly answered your question: you can't use DefaultReflectionImporter
since, as its name implies, it uses reflection, which will always get you a reference to the runtime type.
As for question 2, the root cause is the same. You're adding a reference to a type loaded at runtime to the edited assembly. Forget that the reflection overloads of ImportReference
exist, and your life will get a lot easier.
from cecil.
Related Issues (20)
- Add support for newer runtimes HOT 1
- System.IO.IOException : The process cannot access the file 'xxx.dll' because it is being used by another process.
- System.ArgumentException: "Member 'System.Threading.Tasks.Task' is declared in another module and needs to be imported"
- Assembly roundtrip discards documents from portable PDB HOT 2
- Problem adding sequence points to a method with portable PDB HOT 2
- Provide virtual `ElementType` property at the `TypeReference` level
- `MethodCallingConvention` lacks entry for "default unmanaged calling convention" HOT 3
- Failed to add mono-cecil package to unity HOT 1
- Get method signature location (line/column numbers) through mono.cecil HOT 5
- Embedded PDBs not readable by VisualStudio HOT 3
- Subsequent write operations create different outputs HOT 6
- Type resolution depends on the program that is running, not the assembly being processed. HOT 7
- Chat room HOT 3
- Constant type misidentified as primitive
- [Question] OpCodes.Conv_U
- System.IndexOutOfRangeException: Index was outside the bounds of the array HOT 1
- Constant serialization
- Update readme discussion info
- Release a new version supporting RVA field alignment HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cecil.