简体   繁体   中英

Mono.Cecil - Wrong mscorlib version

I am having issues trying to get Mono.Cecil to use the correct "mscorlib" version when using a TypeReference. I am adding a new variable to a Method and doing something along the following. However, it is adding a reference to the 4.0 mscorlib instead of the 2.0 mscorlib. I am having to run this application using the .NET Framework 4.5 so I am not able to use a lower version unless I re-write other people's code that I don't have access to.

TypeReference typeReference = CSharpModule.Import(Type.GetType("System.Nullable`1[[System.Boolean, mscorlib, Version=2.0.0.0]], mscorlib, Version=2.0.0.0"));
methodBody.Variables.Add(new VariableDefinition(typeReference));

I have tried to search around for an answer on how to get the correct version and I apparently have to use the Cecil Type System, but I am not able to find any examples of how to do this.

You should attempt to load the type by using TypeReference constructor,

https://github.com/lextm/obfuscar/blob/master/Obfuscar/Obfuscator.cs#L1204

My project Obfuscar shows an example of how to do so. You need to pay much attention to the library.MainModule.TypeSystem.Corlib part, so that the type is loaded from the proper mscorlib assembly.

So I was able to fix this by doing the following. Create a TypeReference using what is available to the MainModule.

assemblyDefinition.MainModule.GetType("System.Nullable`1[[System.Boolean]]", true)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM