Replace Marshal.SizeOf for primitive types (#33020)
authorNext Turn <45985406+NextTurn@users.noreply.github.com>
Sun, 1 Mar 2020 04:41:44 +0000 (12:41 +0800)
committerGitHub <noreply@github.com>
Sun, 1 Mar 2020 04:41:44 +0000 (20:41 -0800)
src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/common/utils.cs
src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/ldap/LdapConnection.cs
src/libraries/System.DirectoryServices/src/System/DirectoryServices/ActiveDirectory/ActiveDirectoryReplicationMetaData.cs
src/libraries/System.DirectoryServices/src/System/DirectoryServices/ActiveDirectory/ReplicationCursorCollection.cs
src/libraries/System.DirectoryServices/src/System/DirectoryServices/ActiveDirectory/ReplicationFailureCollection.cs
src/libraries/System.DirectoryServices/src/System/DirectoryServices/ActiveDirectory/ReplicationNeighborCollection.cs
src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/EncoderParameters.cs
src/libraries/System.Drawing.Common/src/System/Drawing/Printing/PrinterSettings.Windows.cs

index c798bdc..b1c1cdc 100644 (file)
@@ -52,10 +52,10 @@ namespace System.DirectoryServices.Protocols
         {
             checked
             {
-                IntPtr intPtrArray = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr)) * size);
+                IntPtr intPtrArray = Marshal.AllocHGlobal(IntPtr.Size * size);
                 for (int i = 0; i < size; i++)
                 {
-                    IntPtr tempPtr = (IntPtr)((long)intPtrArray + Marshal.SizeOf(typeof(IntPtr)) * i);
+                    IntPtr tempPtr = (IntPtr)((long)intPtrArray + IntPtr.Size * i);
                     Marshal.WriteIntPtr(tempPtr, IntPtr.Zero);
                 }
                 return intPtrArray;
index 79ed875..7f14b88 100644 (file)
@@ -1298,7 +1298,7 @@ namespace System.DirectoryServices.Protocols
                             managedControls[i].ldctl_value = new berval
                             {
                                 bv_len = byteControlValue.Length,
-                                bv_val = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(byte)) * byteControlValue.Length)
+                                bv_val = Marshal.AllocHGlobal(sizeof(byte) * byteControlValue.Length)
                             };
                             Marshal.Copy(byteControlValue, 0, managedControls[i].ldctl_value.bv_val, managedControls[i].ldctl_value.bv_len);
                         }
index 6aa2c3c..7ad1d02 100644 (file)
@@ -65,14 +65,14 @@ namespace System.DirectoryServices.ActiveDirectory
             {
                 if (advanced)
                 {
-                    addr = IntPtr.Add(info, Marshal.SizeOf(typeof(int)) * 2 + i * Marshal.SizeOf(typeof(DS_REPL_ATTR_META_DATA_2)));
+                    addr = IntPtr.Add(info, sizeof(int) * 2 + i * Marshal.SizeOf(typeof(DS_REPL_ATTR_META_DATA_2)));
 
                     AttributeMetadata managedMetaData = new AttributeMetadata(addr, true, _server, _nameTable);
                     Add(managedMetaData.Name, managedMetaData);
                 }
                 else
                 {
-                    addr = IntPtr.Add(info, Marshal.SizeOf(typeof(int)) * 2 + i * Marshal.SizeOf(typeof(DS_REPL_ATTR_META_DATA)));
+                    addr = IntPtr.Add(info, sizeof(int) * 2 + i * Marshal.SizeOf(typeof(DS_REPL_ATTR_META_DATA)));
 
                     AttributeMetadata managedMetaData = new AttributeMetadata(addr, false, _server, _nameTable);
                     Add(managedMetaData.Name, managedMetaData);
index 033ecb6..1048751 100644 (file)
@@ -56,7 +56,7 @@ namespace System.DirectoryServices.ActiveDirectory
             {
                 if (advanced)
                 {
-                    addr = IntPtr.Add(info, Marshal.SizeOf(typeof(int)) * 2 + i * Marshal.SizeOf(typeof(DS_REPL_CURSOR_3)));
+                    addr = IntPtr.Add(info, sizeof(int) * 2 + i * Marshal.SizeOf(typeof(DS_REPL_CURSOR_3)));
                     DS_REPL_CURSOR_3 cursor = new DS_REPL_CURSOR_3();
                     Marshal.PtrToStructure(addr, cursor);
 
@@ -70,7 +70,7 @@ namespace System.DirectoryServices.ActiveDirectory
                 }
                 else
                 {
-                    addr = IntPtr.Add(info, Marshal.SizeOf(typeof(int)) * 2 + i * Marshal.SizeOf(typeof(DS_REPL_CURSOR)));
+                    addr = IntPtr.Add(info, sizeof(int) * 2 + i * Marshal.SizeOf(typeof(DS_REPL_CURSOR)));
                     DS_REPL_CURSOR cursor = new DS_REPL_CURSOR();
                     Marshal.PtrToStructure(addr, cursor);
 
index bc660d9..0bc4a28 100644 (file)
@@ -53,7 +53,7 @@ namespace System.DirectoryServices.ActiveDirectory
 
             for (int i = 0; i < count; i++)
             {
-                addr = IntPtr.Add(info, Marshal.SizeOf(typeof(int)) * 2 + i * Marshal.SizeOf(typeof(DS_REPL_KCC_DSA_FAILURE)));
+                addr = IntPtr.Add(info, sizeof(int) * 2 + i * Marshal.SizeOf(typeof(DS_REPL_KCC_DSA_FAILURE)));
 
                 ReplicationFailure managedFailure = new ReplicationFailure(addr, _server, _nameTable);
 
index b14f06b..1b4f715 100644 (file)
@@ -53,7 +53,7 @@ namespace System.DirectoryServices.ActiveDirectory
 
             for (int i = 0; i < count; i++)
             {
-                addr = IntPtr.Add(info, Marshal.SizeOf(typeof(int)) * 2 + i * Marshal.SizeOf(typeof(DS_REPL_NEIGHBOR)));
+                addr = IntPtr.Add(info, sizeof(int) * 2 + i * Marshal.SizeOf(typeof(DS_REPL_NEIGHBOR)));
 
                 ReplicationNeighbor managedNeighbor = new ReplicationNeighbor(addr, _server, _nameTable);
 
index 9ce092b..ac864d0 100644 (file)
@@ -57,11 +57,11 @@ namespace System.Drawing.Imaging
             int size = Marshal.SizeOf(typeof(EncoderParameter));
 
             int length = _param.Length;
-            IntPtr memory = Marshal.AllocHGlobal(checked(length * size + Marshal.SizeOf(typeof(IntPtr))));
+            IntPtr memory = Marshal.AllocHGlobal(checked(length * size + IntPtr.Size));
 
             Marshal.WriteIntPtr(memory, (IntPtr)length);
 
-            long arrayOffset = checked((long)memory + Marshal.SizeOf(typeof(IntPtr)));
+            long arrayOffset = checked((long)memory + IntPtr.Size);
 
             for (int i = 0; i < length; i++)
             {
@@ -86,7 +86,7 @@ namespace System.Drawing.Imaging
 
             EncoderParameters p = new EncoderParameters(count);
             int size = Marshal.SizeOf(typeof(EncoderParameter));
-            long arrayOffset = (long)memory + Marshal.SizeOf(typeof(IntPtr));
+            long arrayOffset = (long)memory + IntPtr.Size;
 
             for (int i = 0; i < count; i++)
             {
index fce1dd4..d1da46e 100644 (file)
@@ -180,11 +180,11 @@ namespace System.Drawing.Printing
                 // PRINTER_INFO_4 is 12 or 24 bytes in size depending on the architecture.
                 if (IntPtr.Size == 8)
                 {
-                    sizeofstruct = (IntPtr.Size * 2) + (Marshal.SizeOf(typeof(int)) * 1) + Padding64Bit;
+                    sizeofstruct = (IntPtr.Size * 2) + (sizeof(int) * 1) + Padding64Bit;
                 }
                 else
                 {
-                    sizeofstruct = (IntPtr.Size * 2) + (Marshal.SizeOf(typeof(int)) * 1);
+                    sizeofstruct = (IntPtr.Size * 2) + (sizeof(int) * 1);
                 }
 
                 int bufferSize;
@@ -454,7 +454,7 @@ namespace System.Drawing.Printing
                 HandleRef hdc = new HandleRef(dc, dc.Hdc);
                 try
                 {
-                    isDirectPrintingSupported = SafeNativeMethods.ExtEscape(hdc, SafeNativeMethods.QUERYESCSUPPORT, Marshal.SizeOf(typeof(int)), ref nEscape, 0, out outData) > 0;
+                    isDirectPrintingSupported = SafeNativeMethods.ExtEscape(hdc, SafeNativeMethods.QUERYESCSUPPORT, sizeof(int), ref nEscape, 0, out outData) > 0;
                 }
                 finally
                 {
@@ -495,10 +495,10 @@ namespace System.Drawing.Printing
                         HandleRef hdc = new HandleRef(dc, dc.Hdc);
                         try
                         {
-                            bool querySupported = SafeNativeMethods.ExtEscape(hdc, SafeNativeMethods.QUERYESCSUPPORT, Marshal.SizeOf(typeof(int)), ref nEscape, 0, out outData) > 0;
+                            bool querySupported = SafeNativeMethods.ExtEscape(hdc, SafeNativeMethods.QUERYESCSUPPORT, sizeof(int), ref nEscape, 0, out outData) > 0;
                             if (querySupported)
                             {
-                                isDirectPrintingSupported = (SafeNativeMethods.ExtEscape(hdc, nEscape, pvImageLen, pvImage, Marshal.SizeOf(typeof(int)), out outData) > 0)
+                                isDirectPrintingSupported = (SafeNativeMethods.ExtEscape(hdc, nEscape, pvImageLen, pvImage, sizeof(int), out outData) > 0)
                                                             && (outData == 1);
                             }
                         }