clr

Escape Catch-22 with extension attributes in .NET 2.0

好久不见. 提交于 2019-12-17 19:38:54
问题 How can a single .NET assembly, targeting 2.0, 3.0, 3.5, 4.0, and 4.5 concurrently, support extension methods for both C# and VB.NET consumers? The standard suggestion is to add this: namespace System.Runtime.CompilerServices { public sealed class ExtensionAttribute : Attribute { } } This the approach suggested by more than one Microsoft employee and was even featured in MSDN magazine. It's widely hailed by many bloggers as having 'no ill effects'. Oh, except it will cause a compiler error

String interning?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-17 18:45:34
问题 The second ReferenceEquals call returns false. Why isn't the string in s4 interned? (I don't care about the advantages of StringBuilder over string concatenation.) string s1 = "tom"; string s2 = "tom"; Console.Write(object.ReferenceEquals(s2, s1)); //true string s3 = "tom"; string s4 = "to"; s4 += "m"; Console.Write(object.ReferenceEquals(s3, s4)); //false When I do String.Intern(s4); , I still get false. Here, both s3 and s4 are interned but their references are not equal? string s3 = "tom";

Why Must I Initialize All Fields in my C# struct with a Non-Default Constructor?

删除回忆录丶 提交于 2019-12-17 18:20:59
问题 I would like to try this code: public struct Direction { private int _azimuth; public int Azimuth { get { return _azimuth; } set { _azimuth = value; } } public Direction(int azimuth) { Azimuth = azimuth } } But it fails on compilation, I understand that struct need to init all its fields. but i am trying to understand what happens under the CLR\IL hoods. why it need all the fields before any other method\property\this etc. Thanks. 回答1: Value Types are created on the stack (unless nested

Spaces in C# Enums

霸气de小男生 提交于 2019-12-17 18:07:15
问题 Is there any way to put spaces in a C# enum constant? I've read that you can do it in VB by doing this: Public Enum EnumWithSpaces ConstantWithoutSpaces [Constant With Spaces] End Enum ...and then access it like this: Public Sub UsingEnumWithSpaces() Dim foo As EnumWithSpaces = EnumWithSpaces.[Constant With Spaces] End Sub That implies to me that the CLR can handle an enum with spaces. Is there any way to do this in C#? 回答1: This blog post might help you: http://blog.spontaneouspublicity.com

Force x86 CLR on an 'Any CPU' .NET assembly

喜你入骨 提交于 2019-12-17 17:25:56
问题 In .NET, the 'Platform Target: Any CPU' compiler option allows a .NET assembly to run as 64 bit on a x64 machine, and 32 bit on an x86 machine. It is also possible to force an assembly to run as x86 on an x64 machine using the 'Platform Target: x86' compiler option. Is it possible to run an assembly with the 'Any CPU' flag, but determine whether it should be run in the x86 or x64 CLR? Normally this decision is made by the CLR/OS Loader (as is my understanding) based on the bitness of the

Is there a possibility of there ever being a PHP.NET?

风格不统一 提交于 2019-12-17 16:28:10
问题 Sorry if this is a silly and/or stupid question but... Will there ever be, or would it even be possible to have a PHP.NET? Or have I got the wrong end of the stick? It seemed to me that one of the main points of .NET was that you could write your code in one of a bunch of the .NET languages and have it compile into CLR. Could this happen with PHP, or is there something about PHP that makes this impossible? Or is there more political reasons? 回答1: As an aside: I don't believe .Net support for

Why Nullable<T> is a struct?

半腔热情 提交于 2019-12-17 16:14:19
问题 I was wondering why Nullable<T> is a value type, if it is designed to mimic the behavior of reference types? I understand things like GC pressure, but I don't feel convinced - if we want to have int acting like reference, we are probably OK with all the consequences of having real reference type. I can see no reason why Nullable<T> is not just boxed version of T struct. As value type: it still needs to be boxed and unboxed, and more, boxing must be a bit different than with "normal" structs

Why a machine with .NET 4 installed on it cannot run an exe that targeted .NET 4.5 while if they use the same CLR version?

社会主义新天地 提交于 2019-12-17 16:11:55
问题 In Common Language Runtime (CLR) Microsoft page, it says that both .Net Framework 4 and 4.5 uses the CLR version 4. However in this page (.NET Framework Versions and Dependencies) it writes '.Net Framework version 4.5 Included an updated version of CLR 4' Also writes: ' An executable that targets the .NET Framework 4.5.1 will be blocked from running on a computer that only has the .NET Framework 4.5 installed, and the user will be prompted to install the .NET Framework 4.5.1. In addition,

Converting .NET App to x86 native code

邮差的信 提交于 2019-12-17 15:41:20
问题 There's a program written entirely in C# that targets .NET Framework 2.0. Is there a way I could somehow compile (translate) managed EXE to a native one so it could be .NET-agnostic? I know there are probably commercial products for that purpose... but they are a bit expensive. The problem is that we are to deploy the program on computers running Windows XP with no .NET Framework installed. There's also a requirement that the program's size must not exceed 500Kb (1Mb maximum) for it is

Why is an assembly .exe file?

走远了吗. 提交于 2019-12-17 15:36:01
问题 Assembly in .net Framework is, as I understand, intermediate language file + some metadata, manifest and maybe something else. CLR translates an assembly to the machine code, which can be executed on the given local machine. That means that assembly shouldn't be executable by the machine before being processed by CLR. If it's so, then why does it have .exe extension, which is executable on Windows machines? 回答1: Since Windows needs to create a process and the first thing .exe will do is to