clr

Why does a const int implicitly cast to a byte, but a variable int does not?

蹲街弑〆低调 提交于 2020-01-14 09:08:48
问题 The following program will not compile: class Program { static void Main(string[] args) { int x = 50; Byte[] y = new Byte[3] { x, x, x }; } } Not surprisingly, I will get the error Cannot implicitly convert type 'int' to 'byte' However, if I make x a const, then it will compile: class Program { public const int x = 50; static void Main(string[] args) { Byte[] y = new Byte[3] { x, x, x }; } } I'm curious as to what's going on here. If an int cannot be implicitly cast to a byte, does the

LNK2028 Managed C++ DLL calling function in another Managed C++ DLL

二次信任 提交于 2020-01-13 06:36:09
问题 I'm using VS2010 with a managed C++ DLL calling a function in another managed C++ DLL and I'm getting many LNK2028 link errors that look like this. 1>udpPkt.obj : error LNK2028: unresolved token (0A0000AA) "unsigned short __cdecl ComputeCrc16(void const *,unsigned int)" (?ComputeCrc16@@$$FYAGPBXI@Z) referenced in function "public: short __thiscall CPrivateUdpPkt::ComputeCrc(void)const " (?ComputeCrc@CPrivateUdpPkt@@$$FQBEFXZ) When I use dumpbin /export on the called DLL I see the unresolved

Reference an unmanaged C++ DLL from Managed C++

隐身守侯 提交于 2020-01-13 06:23:06
问题 This question follows from my previous question: Create DLL from unmanaged C++, but you would not have to read it to understand this new question. I now have a DLL that contains unmanaged C++ code consisting of a few functions, of which I only export one for outside use. Next, I need to use this DLL in a Managed C++ project (built with Common Language Runtime support). So far, I have added a reference to the existing unmanaged project's header file by setting the Additional Include

How to get available virtual and physical memory size under Mono?

会有一股神秘感。 提交于 2020-01-12 08:05:35
问题 Is there any way to get available virtual and physical memory size when running under Mono? 回答1: You could use this implementation of the "free" command (UNIX) to find out used and available physical memory (it is the best option IMHO): using System; using System.Text.RegularExpressions; using System.IO; namespace FreeCSharp { class MainClass { public static void Main(string[] args) { FreeCSharp free = new FreeCSharp(); free.GetValues(); long buffersPlusCached = free.Buffers + free.Cached;

Is there an updated version of “Writing Faster Managed Code: Know What Things Cost”?

谁说我不能喝 提交于 2020-01-12 04:42:05
问题 The MSDN "Writing Faster Managed Code: Know What Things Cost" is pretty nice, but it was written for CLR v1 in 2003. Is there an updated version of this somewhere? 回答1: No. I never updated it, but I think taken with appropriate grains of salt, the general advice and most rules of thumb in the article still hold up well today. (That said, it would be interesting to repeat the experiment today to see how primitive times have changed, how the generated code has changed, and how microprocessors

When does the CLR try to load a referenced assembly?

人走茶凉 提交于 2020-01-11 05:38:09
问题 I want to write a small installer app that installs a web site and creates IIS virtual directories. The app should run on Windows XP/Server 2003 (IIS 6) as well as on Vista/2008 (IIS 7). The problem is: for IIS 6 we create virt dirs by calling WMI/Metabase API, for IIS 7 there is a much better API: Microsoft.Web.Administration, but its assembly is available only on IIS 7 systems. Naive approach: ... if (OperatingSystem == old) { call metabase API... } else { call Microsoft.Web.Administration.

When does the CLR try to load a referenced assembly?

怎甘沉沦 提交于 2020-01-11 05:38:09
问题 I want to write a small installer app that installs a web site and creates IIS virtual directories. The app should run on Windows XP/Server 2003 (IIS 6) as well as on Vista/2008 (IIS 7). The problem is: for IIS 6 we create virt dirs by calling WMI/Metabase API, for IIS 7 there is a much better API: Microsoft.Web.Administration, but its assembly is available only on IIS 7 systems. Naive approach: ... if (OperatingSystem == old) { call metabase API... } else { call Microsoft.Web.Administration.

.NET events special methods (add/remove/raise/other)

为君一笑 提交于 2020-01-10 18:42:54
问题 I was wondering about the EventInfo.GetRaiseMethod and EventInfo.GetOtherMethods methods. Apparently, the CLR supports 4 kinds of methods associated with events: add, remove, raise, and "others". But events created in C# only have add and remove ... I assumed that raise was used in VB, since you have to specify a RaiseEvent method when you declare a custom event, but apparently it's not the case: GetRaiseMethod always returns null. So, does anyone know: what's the point in having a raise

Why is memory access in the lowest address space (non-null though) reported as NullReferenceException by .NET?

China☆狼群 提交于 2020-01-09 09:57:48
问题 This causes an AccessViolationException to be thrown: using System; namespace TestApplication { internal static class Program { private static unsafe void Main() { ulong* addr = (ulong*)Int64.MaxValue; ulong val = *addr; } } } This causes a NullReferenceException to be thrown: using System; namespace TestApplication { internal static class Program { private static unsafe void Main() { ulong* addr = (ulong*)0x000000000000FF; ulong val = *addr; } } } They're both invalid pointers and both

Difference between Task and async

不想你离开。 提交于 2020-01-09 05:22:20
问题 C# provides two ways of creating asynchronous methods: Method 1: static Task<string> MyAsyncTPL() { Task<string> result = PerformWork(); return result.ContinueWith(t => MyContinuation()); } Method 2: static async Task<string> MyAsync() { string result = await PerformWork(); return MyContinuation(); } Both the above methods are async and achieves the same thing. So, when should I choose one method over the other? Are there any guidelines or advantages of using one over the other? 回答1: I