{"id":582,"date":"2018-03-13T13:37:07","date_gmt":"2018-03-13T05:37:07","guid":{"rendered":"https:\/\/blog.freesilo.com\/?p=582"},"modified":"2018-03-13T13:38:03","modified_gmt":"2018-03-13T05:38:03","slug":"marshal-sizeof%e5%92%8csizeof%e7%9a%84%e5%8c%ba%e5%88%ab","status":"publish","type":"post","link":"https:\/\/freesilo.com\/?p=582","title":{"rendered":"Marshal.SizeOf\u548csizeof\u7684\u533a\u522b"},"content":{"rendered":"<p>sizeof\u5728\u975eUnsafe\u73af\u5883\u4e0b\u53ea\u80fd\u7528\u4e8e\u9884\u5b9a\u4e49\u7684\u4e00\u7cfb\u5217\u7c7b\u578b\uff0c\u5982Int\uff0cShort\u7b49\u7b49\u3002\u800c\u5728Unsafe\u73af\u5883\u4e0b\uff0csizeof\u53ef\u4ee5\u88ab\u7528\u4e8e\u503c\u7c7b\u578b\uff0c\u4f46\u662f\u503c\u7c7b\u578b\u4e2d\u4e0d\u53ef\u4ee5\u6709\u5f15\u7528\u7c7b\u578b\uff0c\u5426\u5219C#\u7f16\u8bd1\u5668\u4f1a\u62a5\u9519\uff1a<\/p>\n<p>error CS0208: Cannot take the address of, get the size of, or declare a pointer to a managed type (&#8216;SizeOf.Program.MyStruct&#8217;)<\/p>\n<p>\u800cMarshal.SizeOf\u5219\u662f\u83b7\u5f97\u8be5\u7c7b\u578b\u88abMarshal\uff08\u8f6c\u6362\uff0c\u901a\u5e38\u7ffb\u8bd1\u4e3a\u5217\u96c6\uff0c\u6307\u6570\u636e\u4ece\u4e00\u79cd\u7c7b\u578b\u8f6c\u6362\u5230\u53e6\u5916\u4e00\u79cd\u7c7b\u578b\uff09\u5230\u5bf9\u5e94\u7684\u975e\u6258\u7ba1\u7c7b\u578b\u7684\u5927\u5c0f\u3002\u548csizeof\u4e0d\u540c\uff0cMarshal.SizeOf\u5141\u8bb8\u7528\u5728\u542b\u6709\u5f15\u7528\u7c7b\u578b\u7684\u503c\u7c7b\u578b\u4e0a\uff1a<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&#x5B;StructLayout(LayoutKind.Sequential)]\r\nstruct MyStruct\r\n{\r\n    string s;\r\n}\r\n<\/pre>\n<p>Marshal.SizeOf(MyStruct)\u7ed3\u679c\u4e3a4\u6216\u80058\uff0c\u56e0\u4e3astring\u88abMarshal\u6210char*\u3002<\/p>\n<p>\u5982\u679c\u7528\u5728\u4e0d\u542b\u6709\u5f15\u7528\u7c7b\u578b\u7684\u503c\u7c7b\u578b\u4e0a\uff0c\u5176\u7ed3\u679c\u4e5f\u6709\u53ef\u80fd\u548csizeof\u5b8c\u5168\u4e0d\u4e00\u6837\uff0c\u5982\u5bf9\u4e8e\u4e0b\u9762\u7684\u503c\u7c7b\u578b\uff1a<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nstruct MyStruct\r\n{\r\n   char b;\r\n}\r\n<\/pre>\n<p>sizeof(MyStruct)\u4e3a2\uff0c\u800cMarshal.SizeOf(typeof(MyStruct))\u7ed3\u679c\u5219\u4e3a1\u3002\u8fd9\u662f\u56e0\u4e3a\u5728.NET\u4e2dchar\u603b\u662fUnicode\uff0c\u800c\u7f3a\u7701\u60c5\u51b5\u4e0bchar\u4f1a\u88abMarshal\u62108\u4f4d\u7684Ansi\u5b57\u7b26\uff0c\u56e0\u6b64\u7ed3\u679c\u4e0d\u540c\u3002<\/p>\n<p>\u53cd\u4e4b\uff0c\u5982\u679c\u6211\u4eec\u6307\u5b9a\u8fd9\u4e2achar\u88abMarshal\u6210short\u503c\uff08\u4e5f\u5c31\u662fUTF16\uff09\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&#x5B;StructLayout(LayoutKind.Sequential)]\r\nstruct MyStruct\r\n{\r\n&#x5B;MarshalAs(UnmanagedType.I2)]\r\n    char b;\r\n}\r\n<\/pre>\n<p>\u90a3\u4e48sizeof\u548cMarshal.SizeOf\u7ed3\u679c\u5747\u4e3a2\u3002MarshalAs\u8fd9\u4e2aAttribute\u53ef\u4ee5\u5f71\u54cdMarshal.SizeOf\u7684\u7ed3\u679c\uff0c\u800c\u4e0d\u80fd\u5f71\u54cdsizeof\u7684\u7ed3\u679c\u3002<\/p>\n<p>\u4e00\u4e2a\u6709\u610f\u601d\u7684\u60c5\u51b5\u662f\uff0c\u5982\u679c\u503c\u7c7b\u578b\u4e0d\u542b\u4efb\u4f55\u6210\u5458\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nstruct MyStruct\r\n{\r\n}\r\n<\/pre>\n<p>Sizeof\u548cMarshal.SizeOf\u7ed3\u679c\u5747\u4e3a1\uff0c\u800c\u4e0d\u662f0\u3002\u8fd9\u4e2a\u7ed3\u679c\u548cC++\u7684\u7ed3\u679c\u662f\u4e00\u81f4\u7684\u3002\u539f\u56e0\u5f88\u7b80\u5355\uff1a\u5982\u679c\u58f0\u660e\u4e00\u4e2a\u8fd9\u6837\u7684\u6570\u7ec4\uff0c\u5982\u679c\u5143\u7d20\u5927\u5c0f\u4e3a0\u7684\u8bdd\uff0c\u90a3\u4e48\u6bcf\u4e2a\u5143\u7d20\u90fd\u5177\u6709\u76f8\u540c\u7684\u5730\u5740\uff0c\u8fd9\u662f\u4e0d\u4e3aC++\u6807\u51c6\u6240\u5141\u8bb8\u7684\uff0c\u548c\u6b63\u5e38\u7684\u975e0\u7684\u60c5\u51b5\u4e5f\u4e0d\u4e00\u81f4\u3002.NET\u5728\u8fd9\u91cc\u91c7\u7528\u548cc++\u76f8\u540c\u7684\u89c4\u5219\uff0c\u4e5f\u8ba4\u4e3a\u7a7a\u7684\u503c\u7c7b\u578b\u5927\u5c0f\u4e3a1\u3002<\/p>\n<p>\u6700\u540e\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679cMyStruct\u662f\u6a21\u677f\uff1a<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nstruct MyStruct&lt;T&gt;;\r\n{\r\n    T a;\r\n}\r\n<\/pre>\n<p>\u5982\u679c\u5bf9Marshal.SizeOf\u4f20\u5165MyStruct&lt;&gt;\u6216\u8005MyStruct&lt;int&gt;\u8fd9\u6837\u7684\u7c7b\u578b\uff0c\u5219\u629b\u51faArgumentException\uff0c\u56e0\u4e3aMarshal.SizeOf\u5b8c\u5168\u4e0d\u652f\u6301\u6cdb\u578b\u3002\u8fd9\u4e2a\u662f\u5386\u53f2\u9057\u7559\u95ee\u9898\uff0c\u4ece\u672c\u8d28\u4e0a\u6765\u8bb2\u5b9e\u4f8b\u5316\u7684\u6a21\u677f\u7c7b\u578b\uff08MyStruct&lt;int&gt;\uff09\u5e94\u8be5\u662f\u652f\u6301\u7684\uff0c\u636e\u8bf4\u5f53\u65f6\u4e3b\u8981\u662f\u6ca1\u6709\u65f6\u95f4\u52a0\u4e0a\u5bf9\u6a21\u677f\u7684\u652f\u6301\u3002<\/p>\n<p>\u540c\u6837\u7684\uff0csizeof\u4e5f\u4e0d\u652f\u6301\u6a21\u677f\u7c7b\u578b\uff0c\u800c\u4e14\u8fdeMyStruct&lt;int&gt;\u8fd9\u6837\u5b50\u7684\u7c7b\u578b\u4e5f\u4e0d\u652f\u6301\u3002C#\u7f16\u8bd1\u5668\u4f1a\u5bf9sizeof(MyStruct&lt;int&gt;)\u62a5\u9519\uff1aerror CS0208: Cannot take the address of, get the size of, or declare a pointer to a managed type (&#8216;SizeOf.Program.MyStruct&lt;int&gt;&#8217;)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>sizeof\u5728\u975eUnsafe\u73af\u5883\u4e0b\u53ea\u80fd\u7528\u4e8e\u9884\u5b9a\u4e49\u7684\u4e00\u7cfb\u5217\u7c7b\u578b\uff0c\u5982Int\uff0cShor&#8230;<br \/><a class=\"read-more-button\" href=\"https:\/\/freesilo.com\/?p=582\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-582","post","type-post","status-publish","format-standard","hentry","category-csharp"],"_links":{"self":[{"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/posts\/582","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/freesilo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=582"}],"version-history":[{"count":3,"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/posts\/582\/revisions"}],"predecessor-version":[{"id":600,"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/posts\/582\/revisions\/600"}],"wp:attachment":[{"href":"https:\/\/freesilo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=582"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/freesilo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=582"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/freesilo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=582"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}