il

Is IL generated by expression trees optimized?

自作多情 提交于 2019-12-03 07:11:05
Ok this is merely curiosity, serves no real world help. I know that with expression trees you can generate MSIL on the fly just like the regular C# compiler does. Since compiler can decide optimizations, I'm tempted to ask what is the case with IL generated during Expression.Compile() . Basically two questions: Since at compile time the compiler can produce different (may be slightly) IL in debug mode and release mode , is there ever a difference in the IL generated by compiling an expression when built in debug mode and release mode? Also JIT which convert IL to native code at run time should

Can C# 'is' operator suffer under release mode optimization on .NET 4?

十年热恋 提交于 2019-12-03 04:06:37
问题 Below is a simple test fixture. It succeeds in Debug builds and fails in Release builds (VS2010, .NET4 solution, x64): [TestFixture] public sealed class Test { [Test] public void TestChecker() { var checker = new Checker(); Assert.That(checker.IsDateTime(DateTime.Now), Is.True); } } public class Checker { public bool IsDateTime(object o) { return o is DateTime; } } It seems code optimization wreaks some havoc; if I disable it on the Release build, it works as well. That was rather puzzling to

Are there other ways of calling an interface method of a struct without boxing except in generic classes?

丶灬走出姿态 提交于 2019-12-02 19:35:28
see code snippet public interface I0 { void f0(); } public struct S0:I0 { void I0.f0() { } } public class A<E> where E :I0 { public E e; public void call() { e.f0(); } } here is IL code for call() .maxstack 8 L_0000: ldarg.0 L_0001: ldflda !0 Temp.A`1<!E>::e L_0006: constrained !E L_000c: callvirt instance void Temp.I0::f0() L_0011: ret see reference for constrained The constrained prefix can also be used for invocation of interface methods on value types, because the value type method implementing the interface method can be changed using a MethodImpl. If the constrained prefix is not used,

Can C# 'is' operator suffer under release mode optimization on .NET 4?

ぐ巨炮叔叔 提交于 2019-12-02 17:26:07
Below is a simple test fixture. It succeeds in Debug builds and fails in Release builds (VS2010, .NET4 solution, x64): [TestFixture] public sealed class Test { [Test] public void TestChecker() { var checker = new Checker(); Assert.That(checker.IsDateTime(DateTime.Now), Is.True); } } public class Checker { public bool IsDateTime(object o) { return o is DateTime; } } It seems code optimization wreaks some havoc; if I disable it on the Release build, it works as well. That was rather puzzling to me. Below, I've used ILDASM to disassemble the 2 versions of the build: Debug IL: .method public

Traverse a c# method and anazlye the method body

半城伤御伤魂 提交于 2019-12-01 16:08:02
Whats the easiest way to traverse a methodinfo in c#? I want to traverse the method body and find field-references and such and retrieves the types. In System.Reflection there is: mi.GetMethodBody().GetILAsByteArray() which is kinda low-level and would require "some" work before I would be able to traverse the body. I know Cecil exists, but there's a problem in loading an in memory assembly with cecil. The assembly i'm working with is't always "on disk" it can be an in memory assembly compiled from eg. Boo, and I wan't a clean solution without writing the assembly temporary to disk. What other

CA2202: Do not dispose objects multiple times

你离开我真会死。 提交于 2019-12-01 08:32:04
问题 I have a class like so... public class Class1 { public Class1() { byte[] plainText = new byte[1024]; using (MemoryStream msEncrypt = new MemoryStream()) { using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) { csEncrypt.Write(plainText, 0, plainText.Length); csEncrypt.FlushFinalBlock(); csEncrypt.Flush(); encrypted = msEncrypt.ToArray(); } } } public ICryptoTransform encryptor { get; set; } public byte[] encrypted { get; set; } } Code analysis throws

How to generate IL source code with csc (C# compiler) or dmcs (mono C# compiler)?

六眼飞鱼酱① 提交于 2019-12-01 06:56:39
gcc has an option of -s to generate assembly source code. Does csc (MS C# compiler) or dmcs (mono C# compiler) have equivalence? I mean do those compilers provide an option to generate IL source code that can be read not the execution binary? It's very easy to get to the IL: just use ildasm . The /text option prints the result to the console, which you can divert to a file. A C# compiler always generates IL ( CIL in the case for .net), because thats the way .net works, but if you mean you want to precompile this in to machine code to speed up execution you can use ngen.exe (see HERE ). If you

Calling method of non-assigned class

怎甘沉沦 提交于 2019-12-01 06:04:08
I have doubt about these two aspects; First one; Test test = new Test(); result = test.DoWork(_param); Second one; result = new Test().DoWork(_param); What happens if we dont assign the newly created instance to a variable and directly call the method? I see some difference between two way on IL code. This one below is IL output of first c# code IL_0000: ldstr "job " IL_0005: stloc.0 IL_0006: newobj instance void Works.Test::.ctor() IL_000b: stloc.1 IL_000c: ldloc.1 IL_000d: ldloc.0 IL_000e: callvirt instance string Works.Test::DoWork(string) IL_0013: pop IL_0014: ret And this one is the IL

How to generate IL source code with csc (C# compiler) or dmcs (mono C# compiler)?

你说的曾经没有我的故事 提交于 2019-12-01 05:12:10
问题 gcc has an option of -s to generate assembly source code. Does csc (MS C# compiler) or dmcs (mono C# compiler) have equivalence? I mean do those compilers provide an option to generate IL source code that can be read not the execution binary? 回答1: It's very easy to get to the IL: just use ildasm. The /text option prints the result to the console, which you can divert to a file. 回答2: A C# compiler always generates IL (CIL in the case for .net), because thats the way .net works, but if you mean

Size of generic structure

余生颓废 提交于 2019-11-30 20:27:50
I need to find out a size of a generic structure (I can not do it like sizeof(T) or using Marshal.SizeOf(...) 0> gives me an error) So I wrote: public static class HelperMethods { static HelperMethods() { SizeOfType = createSizeOfFunc(); } public static int SizeOf<T>() { return SizeOfType(typeof(T)); } public static readonly Func<Type, int> SizeOfType = null; private static Func<Type, int> createSizeOfFunc() { var dm = new DynamicMethod("SizeOfType", typeof(int), new Type[] { typeof(Type) }); ILGenerator il = dm.GetILGenerator(); il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Sizeof); //needs to be