mono.cecil

Mono.Cecil something like Type.GetInterfaceMap?

让人想犯罪 __ 提交于 2019-12-06 01:41:28
问题 System.Reflection.Type contains GetInterfaceMap which help to determine what method implement some method from interface. Does Mono.Cecil contain something like this? Or how to implement such behaviour? 回答1: No, Cecil does not provide such method, because Cecil gives us just CIL metadata as they are. (There is project Cecil.Rocks, which contains some useful extension methods, but not this one) In MSIL methods have an attribute 'overrides', which contains references to methods that this method

How to get source/line number for IL instruction using Mono.Cecil

本小妞迷上赌 提交于 2019-12-05 19:09:50
问题 I'm using Mono.Cecil to write a simple utility that looks for type/method usage within .NET assemblies (ex. calling ToString on enums). I am able to get find the method, but it would be cool to display the source/line information to the user. Is this possible with Mono.Cecil? 回答1: It is possible. First you should read the guide from the Mono.Cecil wiki about debugging symbols. Make sure you have Mono.Cecil.Pdb.dll near Mono.Cecil.dll, set the ReaderParameters to read the symbols as indicated

Is there a generic CIL code to convert any type instance to string?

北战南征 提交于 2019-12-05 10:56:39
Is it possible to write generic CIL instructions which will convert instances of any type (both value and reference) to System.String? In particular, I'm interested in Mono.Cecil code which will inject those instructions into a method. Analyzing a generic method I came up with these Mono.Cecil calls: (it's supposed to convert the i-th method parameter to string) System.Reflection.MethodInfo to_string_method_info = typeof( System.Object ).GetMethod( "ToString" ); Mono.Cecil.MethodReference to_string_reference = injectible_assembly.MainModule.Import( to_string_method_info ); Mono.Cecil

.NET CIL manipulation of evaluation stack

£可爱£侵袭症+ 提交于 2019-12-05 10:51:37
I have this sequence of CIL codes which I injected through the use of Mono.Cecil . However, the modified .NET C# application will not run. Objective: Manually load and pop values from stack to display in Console.WriteLine for (int i = 0; i < 3; i++) { int z = some value popped manually from stack; Console.WriteLine(z); } This is the simple main() program I modified: .method private hidebysig static void Main(string[] args) cil managed { .entrypoint .maxstack 5 .locals init ( [0] int32 num, [1] int32 num2) L_0000: ldc.i4.6 //manually push value 6 to stack L_0001: ldc.i4.5 //manually push value

Mono.Cecil something like Type.GetInterfaceMap?

梦想的初衷 提交于 2019-12-04 06:06:21
System.Reflection.Type contains GetInterfaceMap which help to determine what method implement some method from interface. Does Mono.Cecil contain something like this? Or how to implement such behaviour? No, Cecil does not provide such method, because Cecil gives us just CIL metadata as they are. (There is project Cecil.Rocks, which contains some useful extension methods, but not this one) In MSIL methods have an attribute 'overrides', which contains references to methods that this method overrides (in Cecil there is indeed a property Overrides in the MethodDefinition class). However, this

Emitting delegate function call

懵懂的女人 提交于 2019-12-04 03:45:24
I have the following C# code: public static double f2(Func<double, double> f, double x) { return f(x); } And here it's IL code: .method public hidebysig static float64 f2 ( class [mscorlib]System.Func`2<float64, float64> f, float64 x ) cil managed { // Method begins at RVA 0x20bd // Code size 8 (0x8) .maxstack 8 IL_0000: ldarg.0 IL_0001: ldarg.1 IL_0002: callvirt instance !1 class [mscorlib]System.Func`2<float64, float64>::Invoke(!0) IL_0007: ret } How can I to emit callvirt instance !1 class [mscorlib]System.Func`2<float64, float64>::Invoke(!0) insturction through the System.Reflection.Emit

How to get source/line number for IL instruction using Mono.Cecil

给你一囗甜甜゛ 提交于 2019-12-04 02:48:56
I'm using Mono.Cecil to write a simple utility that looks for type/method usage within .NET assemblies (ex. calling ToString on enums). I am able to get find the method, but it would be cool to display the source/line information to the user. Is this possible with Mono.Cecil? It is possible. First you should read the guide from the Mono.Cecil wiki about debugging symbols . Make sure you have Mono.Cecil.Pdb.dll near Mono.Cecil.dll, set the ReaderParameters to read the symbols as indicated in the guide, and then, instructions who have a sequence point in the pdb file will have their

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

我们两清 提交于 2019-12-03 19:58:15
问题 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

Adding custom attributes using mono.cecil?

时间秒杀一切 提交于 2019-12-03 10:38:12
I can't figure how to add custom attribute to a method using Mono.Cecil The attributes that I would want to add is like this : .custom instance void [mscorlib]System.Diagnostics.DebuggerHiddenAttribute::.ctor() = ( 01 00 00 00 ) Does anyone know how to add custom attributes It's actually very easy. ModuleDefinition module = ...; MethodDefinition targetMethod = ...; MethodReference attributeConstructor = module.Import( typeof(DebuggerHiddenAttribute).GetConstructor(Type.EmptyTypes)); targetMethod.CustomAttributes.Add(new CustomAttribute(attributeConstructor)); module.Write(...); This is my take

Read/Get TypeRef table from assembly metadata

删除回忆录丶 提交于 2019-12-02 05:29:41
问题 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