il

Create a copy of method from IL

老子叫甜甜 提交于 2021-01-27 03:50:39
问题 I am trying to create a copy of a method during runtime using reflection. I have the following code. public static R CopyMethod<T, R>(Func<T, R> f, T t) { AppDomain currentDom = Thread.GetDomain(); AssemblyName asm = new AssemblyName(); asm.Name = "DynamicAssembly"; AssemblyBuilder abl = currentDom.DefineDynamicAssembly(asm, AssemblyBuilderAccess.Run); ModuleBuilder mbl = abl.DefineDynamicModule("Module"); TypeBuilder tbl = mbl.DefineType("Type"); var info = f.GetMethodInfo(); MethodBuilder

How to detect source code changes in async method body

丶灬走出姿态 提交于 2020-06-18 11:56:10
问题 I'm trying to detect during runtime if the source code of a method of a class has been changed. Basically I retrieve the method body (IL), hash it with md5 and store it in the database. Next time I check the method, I can compare the hashes. public class Changed { public string SomeValue { get; set; } public string GetSomeValue() { return SomeValue + "add something"; } public async Task<string> GetSomeValueAsync() { return await Task.FromResult(SomeValue + "add something"); } } I'm using Mono

Why is 'box' instruction emitted for generic?

柔情痞子 提交于 2020-02-27 15:57:11
问题 Here is fairly simple generic class. Generic parameter is constrained to be reference type. IRepository and DbSet also contain the same constraint. public class Repository<TEntity> : IRepository<TEntity> where TEntity : class, IEntity { protected readonly DbSet<TEntity> _dbSet; public void Insert(TEntity entity) { if (entity == null) throw new ArgumentNullException("entity", "Cannot add null entity."); _dbSet.Add(entity); } } Compiled IL contains box instruction. Here is the release version

Emitting delegate function call

♀尐吖头ヾ 提交于 2020-02-20 07:39:21
问题 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

Convert IL code to C# at runtime

邮差的信 提交于 2020-02-03 02:26:07
问题 Is there any free library that allows to transform IL code to C# at runtime? Thanks, Christian EDIT Here is an example of what I have: In my program, at some point I have a list of strings which resemble the IL code of a class that lies in some assembly: // =============== CLASS MEMBERS DECLARATION =================== .class public auto ansi beforefieldinit Probant.Arithmetics extends [mscorlib]System.Object { .method public hidebysig instance int32 Sum(int32 a, int32 b) cil managed { // Code

What is a better way to check that a given object is a particular value type? [closed]

人盡茶涼 提交于 2020-01-15 05:28:09
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 6 years ago . Below are the 2 commonly used approaches to check before unbox. myObject.GetType() == typeof(MyValueType) IL_0001: callvirt System.Object.GetType IL_0006: ldtoken UserQuery.MyValueType IL_000B: call System.Type.GetTypeFromHandle IL_0010: call System.Type.op_Equality myObject

Execute .NET IL code in C#

若如初见. 提交于 2020-01-12 06:38:14
问题 Is there any way to execute an array of IL codes in C# like shell codes in C/C++? I want to create a method, convert it to IL code, obfuscate it and store in an array of bytes and finally want to execute it decrypt the array content and execute IL code. For example this is my C# code: static int MyMethod(string A, int B) { B += 10; if (A.Equals("A")) B = 0; return B; } Now I convert it to IL code : private static int MyMethod(string A, int B) { locals: int local_0, bool local_1 /* 0000025C 00

Why c# compiler in some cases emits newobj/stobj rather than 'call instance .ctor' for struct initialization

只愿长相守 提交于 2020-01-12 01:34:13
问题 here some test program in c#: using System; struct Foo { int x; public Foo(int x) { this.x = x; } public override string ToString() { return x.ToString(); } } class Program { static void PrintFoo(ref Foo foo) { Console.WriteLine(foo); } static void Main(string[] args) { Foo foo1 = new Foo(10); Foo foo2 = new Foo(20); Console.WriteLine(foo1); PrintFoo(ref foo2); } } and here disassembled compiled version of method Main: .method private hidebysig static void Main (string[] args) cil managed { /

IronPython: How to call a function that expects an array of value-types?

点点圈 提交于 2020-01-05 02:13:04
问题 I have come across a problem with IronPython that I can't solve. I need to call a function that takes a parameter of type array to value-type. The function-signature (in C++/CLI notation) is this: static int PLGServiceInterface::PLGService::MapLowSpeedRealtimeNames(cli::array<System::String ^> ^ SignalNames, int timeout_ms, cli::array<PLGServiceInterface::LVCluster_1> ^% Channels, System::String ^% ReportText) When I call the function from IronPython import clr clr.AddReferenceToFileAndPath(

Is it OK to remove .property statements from ILAsm files for production use?

…衆ロ難τιáo~ 提交于 2020-01-04 17:19:10
问题 Still working on my obfuscation program based on modifying ILAsm files. (Ref. Which C# method names should not be obfuscated? ) In looking at the ILAsm code, I get the impression that the .property statements aren't used for anything, at least not for program execution. Am I right in thinking they are only there to provide metadata for Visual Studio? And if so, is it OK to simply remove them when the ILAsm code will only be used to produce production modules that will never be used with