mono.cecil

Method import using Mono.Cecil

雨燕双飞 提交于 2019-12-02 05:15:38
问题 Help me please with method import. I want to weave assembly and inject method call reference from base class defined in the other assembly (in fact it's the assembly where weaving code is defined). private void InsertCallSetReference() { //Get the load instruction to replace var ilProcessor = Property.SetMethod.Body.GetILProcessor(); var argumentLoadInstructions = ilProcessor.Body.Instructions.ToList(); MethodReference methodReference = ImportMethod("SetReference"); foreach (var instruction

Read/Get TypeRef table from assembly metadata

…衆ロ難τιáo~ 提交于 2019-12-01 23:46:26
This is a follow-up question to THIS one: To analyze an assembly (or the types it ueses) I would like to read the TypeRef table of such assembly. I got the hint to use Mono.Cecil to do this, but I only found examples reading TypeDef information. Also browsing the source-code of cecil I only found internal classes which seems to me are responsible for reading the metadata, but I found no "public interface". I also found THIS article which uses some COM-library to read metadata, but I couldn't figure out how to use that for my purpose either. Can anyone tell me how I can read the TypeRef table

Method import using Mono.Cecil

蓝咒 提交于 2019-12-01 23:31:22
Help me please with method import. I want to weave assembly and inject method call reference from base class defined in the other assembly (in fact it's the assembly where weaving code is defined). private void InsertCallSetReference() { //Get the load instruction to replace var ilProcessor = Property.SetMethod.Body.GetILProcessor(); var argumentLoadInstructions = ilProcessor.Body.Instructions.ToList(); MethodReference methodReference = ImportMethod("SetReference"); foreach (var instruction in argumentLoadInstructions) { if (instruction.OpCode == OpCodes.Stfld) { ilProcessor.InsertAfter

Mono.Cecil, Missing compiler required member 'System.Runtime.CompilerServices.ExtensionAttribute..ctor'

江枫思渺然 提交于 2019-12-01 19:58:29
I downloaded the latest Mono.Cecil and now whenever I start up my project it gives me that error. It goes away if I remove and add mono.cecil. But that is a pain to do every time I open my project. This issue is a well know problem for all .NET 2.0 Projects that want to use Extension Methods internally and therefore declare their own internal System.Runtime.CompilerServices.ExtensionAttribute to allow this. There are various ways around this problem, if you're using .NET 3.5 or higher, simply compile Mono.Cecil with NET_3_5 or NET_4_0 defined. If you're targeting .NET 2 you might be lucky and

What is the “<Module>” type?

我只是一个虾纸丫 提交于 2019-12-01 17:38:53
问题 I am using Mono.Cecil to read an assembly generated by Regex.CompileToAssembly() . When I iterate through the types, there is one type in the root namespace named <Module> . The type has no base type. What is this type? Is this some Mono.Cecil artifact or something that is actually a real part of .NET assemblies? What role does it play? 回答1: The <Module> type is a place-holder for declarations that do not fit the CLI model. Normally relevant only in assemblies that are mixed-mode, containing

Detect whether the assembly was built for .NET Compact Framework

强颜欢笑 提交于 2019-12-01 07:05:03
问题 Having a .NET assembly, how can I detect whether it was built for .NET CF or a full framework? 回答1: It's quite simple: public enum AssemblyType { CompactFramework, FullFramework, NativeBinary } public AssemblyType GetAssemblyType(string pathToAssembly) { try { Assembly asm = Assembly.LoadFrom(pathToAssembly); var mscorlib = asm.GetReferencedAssemblies().FirstOrDefault(a => string.Compare(a.Name, "mscorlib", true) == 0); ulong token = BitConverter.ToUInt64(mscorlib.GetPublicKeyToken(), 0);

Can Mono.Cecil modify code already loaded in the AppDomain?

廉价感情. 提交于 2019-11-30 18:51:40
I want to add some behavior to a certain class at runtime. I know how to subclass at runtime using Reflection.Emit but thats not enough, Depending on some external configuration I need to inject opcodes in a method on a type T so all classes that inherit from it automatically gain this behavior.(I cant use the .NET Profiling API) Can something like this be done with Mono.Cecil? If it isnt possible to modify code on a loaded assembly, It is fine If I can make the modifications before the assembly is loaded and then load the modified assembly in memory, but I dont know how I can control assembly

How to create an override method using Mono.Cecil?

拈花ヽ惹草 提交于 2019-11-30 18:42:39
I'm using Mono.Cecil to generate an assembly that contains a derived class that overrides a specific method in an imported base class. The override method is an 'implicit' override. The problem is that I cannot figure out how to designate it as an override. I'm using the following code to create the override method. void CreateMethodOverride(TypeDefinition targetType, TypeDefinition baseClass, string methodName, MethodInfo methodInfo) { // locate the matching base class method, which may // reside in a different module MethodDefinition baseMethod = baseClass .Methods.First(method => method

Can Mono.Cecil modify code already loaded in the AppDomain?

巧了我就是萌 提交于 2019-11-30 16:54:13
问题 I want to add some behavior to a certain class at runtime. I know how to subclass at runtime using Reflection.Emit but thats not enough, Depending on some external configuration I need to inject opcodes in a method on a type T so all classes that inherit from it automatically gain this behavior.(I cant use the .NET Profiling API) Can something like this be done with Mono.Cecil? If it isnt possible to modify code on a loaded assembly, It is fine If I can make the modifications before the

Emit call to System.Lazy<T> constructor with Mono.Cecil

泪湿孤枕 提交于 2019-11-30 13:42:40
I'm trying to emit a method that instantiates a System.Lazy and failing with a PEVerify error of "Invalid token", at the line newobj instance void class [mscorlib]System.Lazy`1<class Example.ExpensiveType>::.ctor(class [mscorlib]System.Func`1<class Example.ExpensiveType>) Looking elsewhere with ILDasm, I see that a proper call would look like this: newobj instance void class [mscorlib]System.Lazy`1<class Example.IHeater>::.ctor(class [mscorlib]System.Func`1<!0>) Unfortunately, I'm at a loss as to how to reproduce this with the Mono.Cecil API. Can someone help with the generics? Here's what I