structlayout

Should changing the contents of a string like this cause an exception?

这一生的挚爱 提交于 2019-11-29 10:53:05
Consider the following code: using System; using System.Runtime.InteropServices; namespace Demo { class Program { static void Main(string[] args) { const string test = "ABCDEF"; // Strings are immutable, right? char[] chars = new StringToChar{str=test}.chr; chars[0] = 'X'; // On an x32 release or debug build or on an x64 debug build, // the following prints "XBCDEF". // On an x64 release build, it prints "ABXDEF". // In both cases, we have changed the contents of 'test' without using // any 'unsafe' code... Console.WriteLine(test); } } [StructLayout(LayoutKind.Explicit)] public struct

C# StructLayout.Explicit Question

北慕城南 提交于 2019-11-29 01:58:56
I'm trying to understand why the second example below works with no issues, but the first example gives me the exception below. It seems to me that both examples should give an exception based on the description. Can anyone enlighten me? Unhandled Exception: System.TypeLoadException: Could not load type 'StructTest.OuterType' from assembly 'StructTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' because it contains an object field at offset 0 that is incorrectly aligned or overlapped by a non-object field. at StructTest.Program.Main(String[] args) Press any key to continue . . .

Should changing the contents of a string like this cause an exception?

感情迁移 提交于 2019-11-28 04:25:14
问题 Consider the following code: using System; using System.Runtime.InteropServices; namespace Demo { class Program { static void Main(string[] args) { const string test = "ABCDEF"; // Strings are immutable, right? char[] chars = new StringToChar{str=test}.chr; chars[0] = 'X'; // On an x32 release or debug build or on an x64 debug build, // the following prints "XBCDEF". // On an x64 release build, it prints "ABXDEF". // In both cases, we have changed the contents of 'test' without using // any

C# StructLayout.Explicit Question

懵懂的女人 提交于 2019-11-27 21:46:26
问题 I'm trying to understand why the second example below works with no issues, but the first example gives me the exception below. It seems to me that both examples should give an exception based on the description. Can anyone enlighten me? Unhandled Exception: System.TypeLoadException: Could not load type 'StructTest.OuterType' from assembly 'StructTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' because it contains an object field at offset 0 that is incorrectly aligned or

Why does LayoutKind.Sequential work differently if a struct contains a DateTime field?

大憨熊 提交于 2019-11-27 20:08:30
Why does LayoutKind.Sequential work differently if a struct contains a DateTime field? Consider the following code (a console app which must be compiled with "unsafe" enabled): using System; using System.Runtime.InteropServices; namespace ConsoleApplication3 { static class Program { static void Main() { Inner test = new Inner(); unsafe { Console.WriteLine("Address of struct = " + ((int)&test).ToString("X")); Console.WriteLine("Address of First = " + ((int)&test.First).ToString("X")); Console.WriteLine("Address of NotFirst = " + ((int)&test.NotFirst).ToString("X")); } } } [StructLayout

Why does LayoutKind.Sequential work differently if a struct contains a DateTime field?

时光总嘲笑我的痴心妄想 提交于 2019-11-26 19:59:18
问题 Why does LayoutKind.Sequential work differently if a struct contains a DateTime field? Consider the following code (a console app which must be compiled with "unsafe" enabled): using System; using System.Runtime.InteropServices; namespace ConsoleApplication3 { static class Program { static void Main() { Inner test = new Inner(); unsafe { Console.WriteLine("Address of struct = " + ((int)&test).ToString("X")); Console.WriteLine("Address of First = " + ((int)&test.First).ToString("X")); Console