il

How to identify array type?

偶尔善良 提交于 2019-11-30 08:31:25
问题 I have an OutOfMemoryException and I'd like to analyze the size and the type of the array which shall be created. I have created a demo purpose dump for that situation and I'm able to get the following information: 0:000> !pe Exception object: 023f389c Exception type: System.OutOfMemoryException Message: <none> InnerException: <none> StackTrace (generated): SP IP Function 0015EE44 0099007F OOM2!OOM2.Program.Main()+0xf StackTraceString: <none> HResult: 8007000e 0:000> !u 0099007F Normal JIT

Size of generic structure

∥☆過路亽.° 提交于 2019-11-30 05:06:31
问题 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) });

How does protobuf-net handle readonly fields?

回眸只為那壹抹淺笑 提交于 2019-11-30 04:45:38
问题 I use protobuf-net to serialize/deserialize my data. I have some rather simple classes, so that's no real problem. As far as I know, protobuf-net uses IL generation to create serialization/deserialization code. While I have readonly fields in my model, I wonder how is it possible to write to such a field with IL? I can plainly see it works well, but I don't know why... I've tried to spy it in the code, but it's a bit too complicated. My attempts to generate such code myself always result in

Interlocked.CompareExchange<Int> using GreaterThan or LessThan instead of equality

为君一笑 提交于 2019-11-30 03:45:51
The System.Threading.Interlocked object allows for Addition (subtraction) and comparison as an atomic operation. It seems that a CompareExchange that just doesn't do equality but also GreaterThan/LessThan as an atomic comparison would be quite valuable. Would a hypothetical Interlocked.GreaterThan a feature of the IL or is it a CPU-level feature? Both? Lacking any other option, is it possible to create such a feature in C++ or direct IL code and expose that functionality to C#? Update to the later post I made here: we found a better way to make the greater comparison by using additional lock

DynamicMethod is much slower than compiled IL function

醉酒当歌 提交于 2019-11-29 22:41:20
I wrote a simple object copier that copies public properties. I can't figure out why the Dynamic method is a lot slower than the c# version. Durations C# method : 4,963 ms Dynamic method : 19,924 ms Note that - as I run the dynamic method before starting the stopwatch - the duration do not include the compilation phase. I run that in Debug and Release mode, in x86 and x64 mode, and from VS and from the command line with roughly the same result (dynamic method is 400% slower). const int NBRECORDS = 100 * 1000 * 1000; public class Person { private int mSomeNumber; public string FirstName { get;

Creating method dynamically, and executing it

懵懂的女人 提交于 2019-11-29 21:25:14
Background: I want to define few static methods in C# , and generate IL code as byte array, from one of these methods, selected at runtime (on client), and send the byte array over network to another machine (server) where it should be executed after re-generating the IL code from the byte array. My Attempt: ( POC ) public static class Experiment { public static int Multiply(int a, int b) { Console.WriteLine("Arguments ({0}, {1})", a, b); return a * b; } } And then I get the IL code of the method body, as: BindingFlags flags = BindingFlags.Public | BindingFlags.Static; MethodInfo meth = typeof

Are Module initializers supported in Silverlight and Windows Phone 7?

跟風遠走 提交于 2019-11-29 18:08:11
If you are willing to manipulate IL .net supports Module Initializers http://blogs.msdn.com/b/junfeng/archive/2005/11/19/494914.aspx http://tech.einaregilsson.com/2009/12/16/module-initializers-in-csharp/ Are Module initializers supported in Silverlight and Windows Phone 7? Only C++/CLI supports a module initializer. And that's only indirectly, it uses them to get the CRT started and to get unmanaged variables and objects initialized. You have to write on in IL. I tried, it worked just fine on Silverlight 4: .assembly extern mscorlib { .publickeytoken = (7C EC 85 D7 BE A7 79 8E ) .ver 2:0:5:0

IL offsets missing when silverlight assembly is compiled in release mode

試著忘記壹切 提交于 2019-11-29 14:02:33
问题 I followed these instructions to add IL offsets to Silverlight stack traces. This works great when building in DEBUG mode however our production/qa build process compiles everything using RELEASE mode which seems to loose the IL offset information. In release mode all the IL offsets end up being "0xffffffff". Using reflector to compare the debug/release assemblies I noticed the DebuggableAttribute was used differently. DEBUG build: [assembly: AssemblyVersion("1.0.0.0")] [assembly: ComVisible

Is there a race condition in this common pattern used to prevent NullReferenceException?

时光总嘲笑我的痴心妄想 提交于 2019-11-29 01:07:57
I asked this question and got this interesting (and a little disconcerting) answer. Daniel states in his answer (unless I'm reading it incorrectly) that the ECMA-335 CLI specification could allow a compiler to generate code that throws a NullReferenceException from the following DoCallback method. class MyClass { private Action _Callback; public Action Callback { get { return _Callback; } set { _Callback = value; } } public void DoCallback() { Action local; local = Callback; if (local == null) local = new Action(() => { }); local(); } } He says that, in order to guarantee a

Compiler generated sealed class for delegate keyword contains virtual methods

落花浮王杯 提交于 2019-11-28 21:21:20
When delegate keyword is used in C#, the C# compiler automatically generates a class derived from System.MulticastDelegate class. This compiler generated class contains 3 methods as well: Invoke, BeginInvoke and EndInvoke . All these three methods are marked public virtual extern but interestingly the class itself is marked sealed . Virtual methods defined in a sealed class not only strikes as counter-intuitive but are actually illegal in C#. So my question is, is there a specific reason for this or is it just one of those harmless things done keeping in mind some hypothetical future