Skip to content

Commit 2c43140

Browse files
authored
Clean up ClrFacade.cs - remove ClrFacade.SizeOf<T>() (PowerShell#4377)
1 parent 4eeeb45 commit 2c43140

9 files changed

Lines changed: 24 additions & 33 deletions

File tree

src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleControl.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3327,7 +3327,7 @@ private static HostException CreateHostException(
33273327
internal static void MimicKeyPress(INPUT[] inputs)
33283328
{
33293329
Dbg.Assert(inputs != null && inputs.Length > 0, "inputs should not be null or empty");
3330-
var numberOfSuccessfulEvents = NativeMethods.SendInput((uint)inputs.Length, inputs, ClrFacade.SizeOf<INPUT>());
3330+
var numberOfSuccessfulEvents = NativeMethods.SendInput((uint)inputs.Length, inputs, Marshal.SizeOf<INPUT>());
33313331

33323332
if (numberOfSuccessfulEvents == 0)
33333333
{

src/Microsoft.PowerShell.LocalAccounts/LocalAccounts/Sam.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,7 +1065,7 @@ private void OpenHandles()
10651065
// Open the "BuiltIn" domain
10661066
SecurityIdentifier sid = new SecurityIdentifier("S-1-5-32");
10671067
byte[] bSid = new byte[sid.BinaryLength];
1068-
int size = ClrFacade.SizeOf<byte>() * bSid.Length;
1068+
int size = Marshal.SizeOf<byte>() * bSid.Length;
10691069

10701070
pSid = Marshal.AllocHGlobal(size);
10711071

@@ -2247,7 +2247,7 @@ private void SetUserData(IntPtr userHandle,
22472247
if ((which & SamApi.USER_ALL_USERACCOUNTCONTROL) != 0)
22482248
info.UserAccountControl = uac;
22492249

2250-
buffer = Marshal.AllocHGlobal(ClrFacade.SizeOf<USER_ALL_INFORMATION>());
2250+
buffer = Marshal.AllocHGlobal(Marshal.SizeOf<USER_ALL_INFORMATION>());
22512251
Marshal.StructureToPtr<USER_ALL_INFORMATION>(info, buffer, false);
22522252

22532253
status = SamApi.SamSetInformationUser(userHandle,
@@ -2389,7 +2389,7 @@ private void SetSamDacl(IntPtr objectHandle, RawAcl rawAcl)
23892389

23902390
// create a new security descriptor
23912391
var sd = new SECURITY_DESCRIPTOR() { Revision = 1 };
2392-
ipsd = Marshal.AllocHGlobal(ClrFacade.SizeOf<SECURITY_DESCRIPTOR>());
2392+
ipsd = Marshal.AllocHGlobal(Marshal.SizeOf<SECURITY_DESCRIPTOR>());
23932393

23942394
if (rawAcl != null && rawAcl.BinaryLength > 0)
23952395
{

src/System.Management.Automation/engine/COM/ComInvoker.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ internal static class ComInvoker
2424
// Alias of GUID_NULL. It's a GUID set to all zero
2525
private static readonly Guid s_IID_NULL = new Guid();
2626
// Size of the Variant struct
27-
private static readonly int s_variantSize = ClrFacade.SizeOf<Variant>();
27+
private static readonly int s_variantSize = Marshal.SizeOf<Variant>();
2828

2929
/// <summary>
3030
/// Make a by-Ref VARIANT value based on the passed-in VARIANT argument

src/System.Management.Automation/engine/COM/ComUtil.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ internal static string GetMethodSignatureFromFuncDesc(COM.ITypeInfo typeinfo, CO
4949
builder.Append(" (");
5050

5151
IntPtr ElementDescriptionArrayPtr = funcdesc.lprgelemdescParam;
52-
int ElementDescriptionSize = ClrFacade.SizeOf<COM.ELEMDESC>();
52+
int ElementDescriptionSize = Marshal.SizeOf<COM.ELEMDESC>();
5353

5454
for (int i = 0; i < funcdesc.cParams; i++)
5555
{
@@ -288,7 +288,7 @@ internal static ParameterInformation[] GetParameterInformation(COM.FUNCDESC func
288288
ParameterInformation[] parameters = new ParameterInformation[cParams];
289289

290290
IntPtr ElementDescriptionArrayPtr = funcdesc.lprgelemdescParam;
291-
int ElementDescriptionSize = ClrFacade.SizeOf<COM.ELEMDESC>();
291+
int ElementDescriptionSize = Marshal.SizeOf<COM.ELEMDESC>();
292292

293293
for (int i = 0; i < cParams; i++)
294294
{

src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4468,7 +4468,7 @@ internal static List<string> GetFileShares(string machine, bool ignoreHidden)
44684468
{
44694469
for (int i = 0; i < numEntries; ++i)
44704470
{
4471-
IntPtr curInfoPtr = (IntPtr)((long)shBuf + (ClrFacade.SizeOf<SHARE_INFO_1>() * i));
4471+
IntPtr curInfoPtr = (IntPtr)((long)shBuf + (Marshal.SizeOf<SHARE_INFO_1>() * i));
44724472
SHARE_INFO_1 shareInfo = ClrFacade.PtrToStructure<SHARE_INFO_1>(curInfoPtr);
44734473

44744474

src/System.Management.Automation/engine/remoting/fanin/WSManNativeAPI.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ internal MarshalledObject(IntPtr dataPtr)
176176
/// <returns>MarshalledObject</returns>
177177
internal static MarshalledObject Create<T>(T obj)
178178
{
179-
IntPtr ptr = Marshal.AllocHGlobal(ClrFacade.SizeOf<T>());
179+
IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf<T>());
180180
Marshal.StructureToPtr(obj, ptr, false);
181181

182182
// Now create the MarshalledObject and return.
@@ -665,7 +665,7 @@ internal WSManData_ManToUn(byte[] data)
665665
_marshalledBuffer = dataToSendPtr; // Stored directly to enable graceful clean up during finalizer scenarios
666666

667667
Marshal.Copy(data, 0, _internalData.binaryOrTextData.data, _internalData.binaryOrTextData.bufferLength);
668-
_marshalledObject = Marshal.AllocHGlobal(ClrFacade.SizeOf<WSManDataStruct>());
668+
_marshalledObject = Marshal.AllocHGlobal(Marshal.SizeOf<WSManDataStruct>());
669669
Marshal.StructureToPtr(_internalData, _marshalledObject, false);
670670
}
671671

@@ -686,7 +686,7 @@ internal WSManData_ManToUn(string data)
686686
// marshal text data
687687
_internalData.binaryOrTextData.data = Marshal.StringToHGlobalUni(data);
688688
_marshalledBuffer = _internalData.binaryOrTextData.data; // Stored directly to enable graceful clean up during finalizer scenarios
689-
_marshalledObject = Marshal.AllocHGlobal(ClrFacade.SizeOf<WSManDataStruct>());
689+
_marshalledObject = Marshal.AllocHGlobal(Marshal.SizeOf<WSManDataStruct>());
690690
Marshal.StructureToPtr(_internalData, _marshalledObject, false);
691691
}
692692

@@ -945,7 +945,7 @@ internal WSManStreamIDSet_ManToUn(string[] streamIds)
945945
{
946946
Dbg.Assert(streamIds != null, "stream ids cannot be null or empty");
947947

948-
int sizeOfIntPtr = ClrFacade.SizeOf<IntPtr>();
948+
int sizeOfIntPtr = Marshal.SizeOf<IntPtr>();
949949
_streamSetInfo = new WSManStreamIDSetStruct();
950950
_streamSetInfo.streamIDsCount = streamIds.Length;
951951
_streamSetInfo.streamIDs = Marshal.AllocHGlobal(sizeOfIntPtr * streamIds.Length);
@@ -965,7 +965,7 @@ internal void Dispose()
965965
{
966966
if (IntPtr.Zero != _streamSetInfo.streamIDs)
967967
{
968-
int sizeOfIntPtr = ClrFacade.SizeOf<IntPtr>();
968+
int sizeOfIntPtr = Marshal.SizeOf<IntPtr>();
969969
for (int index = 0; index < _streamSetInfo.streamIDsCount; index++)
970970
{
971971
IntPtr streamAddress = IntPtr.Zero;
@@ -1028,7 +1028,7 @@ internal static WSManStreamIDSet_UnToMan UnMarshal(IntPtr unmanagedData)
10281028
/*
10291029
* // TODO: Why didn't this work? It looks more efficient
10301030
idsArray = new string[resultInternal.streamIDsCount];
1031-
int sizeInBytes = ClrFacade.SizeOf<IntPtr>();
1031+
int sizeInBytes = Marshal.SizeOf<IntPtr>();
10321032
IntPtr perElementPtr = resultInternal.streamIDs;
10331033
10341034
for (int i = 0; i < resultInternal.streamIDsCount; i++)
@@ -1103,7 +1103,7 @@ internal WSManOptionSet(WSManOption[] options)
11031103
{
11041104
Dbg.Assert(null != options, "options cannot be null");
11051105

1106-
int sizeOfOption = ClrFacade.SizeOf<WSManOption>();
1106+
int sizeOfOption = Marshal.SizeOf<WSManOption>();
11071107
_optionSet = new WSManOptionSetStruct();
11081108
_optionSet.optionsCount = options.Length;
11091109
_optionSet.optionsMustUnderstand = true;
@@ -1187,7 +1187,7 @@ internal static WSManOptionSet UnMarshal(WSManOptionSetStruct resultInternal)
11871187
{
11881188
tempOptions = new WSManOption[resultInternal.optionsCount];
11891189

1190-
int sizeInBytes = ClrFacade.SizeOf<WSManOption>();
1190+
int sizeInBytes = Marshal.SizeOf<WSManOption>();
11911191
IntPtr perElementPtr = resultInternal.options;
11921192

11931193
for (int i = 0; i < resultInternal.optionsCount; i++)
@@ -1230,7 +1230,7 @@ internal WSManCommandArgSet(byte[] firstArgument)
12301230
{
12311231
_internalData = new WSManCommandArgSetInternal();
12321232
_internalData.argsCount = 1;
1233-
_internalData.args = Marshal.AllocHGlobal(ClrFacade.SizeOf<IntPtr>());
1233+
_internalData.args = Marshal.AllocHGlobal(Marshal.SizeOf<IntPtr>());
12341234

12351235
// argument set takes only strings..but powershell's serialized pipeline might contain
12361236
// \0 (null characters) which are unacceptable in WSMan. So we are converting to Base64
@@ -1518,7 +1518,7 @@ internal static WSManEnvironmentVariableSet UnMarshal(IntPtr unmanagedData)
15181518
if (resultInternal.varsCount > 0)
15191519
{
15201520
varsArray = new WSManEnvironmentVariableInternal[resultInternal.varsCount];
1521-
int sizeInBytes = ClrFacade.SizeOf<WSManEnvironmentVariableInternal>();
1521+
int sizeInBytes = Marshal.SizeOf<WSManEnvironmentVariableInternal>();
15221522
IntPtr perElementPtr = resultInternal.vars;
15231523

15241524
for (int i = 0; i < resultInternal.varsCount; i++)
@@ -2251,7 +2251,7 @@ internal static WSManSelectorSet UnMarshal(WSManSelectorSetStruct resultInternal
22512251
if (resultInternal.numberKeys > 0)
22522252
{
22532253
tempKeys = new WSManKeyStruct[resultInternal.numberKeys];
2254-
int sizeInBytes = ClrFacade.SizeOf<WSManKeyStruct>();
2254+
int sizeInBytes = Marshal.SizeOf<WSManKeyStruct>();
22552255
IntPtr perElementPtr = resultInternal.keys;
22562256

22572257
for (int i = 0; i < resultInternal.numberKeys; i++)

src/System.Management.Automation/namespaces/FileSystemProvider.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8144,7 +8144,7 @@ private static string WinInternalGetLinkType(string filePath)
81448144

81458145
using (SafeFileHandle handle = OpenReparsePoint(filePath, FileDesiredAccess.GenericRead))
81468146
{
8147-
int outBufferSize = ClrFacade.SizeOf<REPARSE_DATA_BUFFER_SYMBOLICLINK>();
8147+
int outBufferSize = Marshal.SizeOf<REPARSE_DATA_BUFFER_SYMBOLICLINK>();
81488148

81498149
IntPtr outBuffer = Marshal.AllocHGlobal(outBufferSize);
81508150
bool success = false;
@@ -8378,7 +8378,7 @@ private static string InternalGetTarget(SafeFileHandle handle)
83788378
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2001:AvoidCallingProblematicMethods")]
83798379
private static string WinInternalGetTarget(SafeFileHandle handle)
83808380
{
8381-
int outBufferSize = ClrFacade.SizeOf<REPARSE_DATA_BUFFER_SYMBOLICLINK>();
8381+
int outBufferSize = Marshal.SizeOf<REPARSE_DATA_BUFFER_SYMBOLICLINK>();
83828382

83838383
IntPtr outBuffer = Marshal.AllocHGlobal(outBufferSize);
83848384
bool success = false;
@@ -8546,7 +8546,7 @@ internal static bool DeleteJunction(string junctionPath)
85468546
using (SafeHandle handle = OpenReparsePoint(junctionPath, FileDesiredAccess.GenericWrite))
85478547
{
85488548
bool success = false;
8549-
int inOutBufferSize = ClrFacade.SizeOf<REPARSE_GUID_DATA_BUFFER>();
8549+
int inOutBufferSize = Marshal.SizeOf<REPARSE_GUID_DATA_BUFFER>();
85508550
IntPtr outBuffer = Marshal.AllocHGlobal(inOutBufferSize);
85518551
IntPtr inBuffer = Marshal.AllocHGlobal(inOutBufferSize);
85528552

src/System.Management.Automation/utils/ClrFacade.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,6 @@ internal static SafeHandle GetSafeProcessHandle(Process process)
6363

6464
#region Marshal
6565

66-
/// <summary>
67-
/// Facade for Marshal.SizeOf
68-
/// </summary>
69-
internal static int SizeOf<T>()
70-
{
71-
// Marshal.SizeOf(Type) is obsolete in CoreCLR
72-
return Marshal.SizeOf<T>();
73-
}
74-
7566
/// <summary>
7667
/// Facade for Marshal.PtrToStructure
7768
/// </summary>

src/System.Management.Automation/utils/PlatformInvokes.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ internal static bool EnableTokenPrivilege(string privilegeName, ref TOKEN_PRIVIL
337337
// The specified privilege is not enabled yet. Enable it.
338338
newPrivilegeState.PrivilegeCount = 1;
339339
newPrivilegeState.Privilege.Attributes = SE_PRIVILEGE_ENABLED;
340-
int bufferSize = ClrFacade.SizeOf<TOKEN_PRIVILEGE>();
340+
int bufferSize = Marshal.SizeOf<TOKEN_PRIVILEGE>();
341341
int returnSize = 0;
342342

343343
// enable the specified privilege
@@ -406,7 +406,7 @@ internal static bool RestoreTokenPrivilege(string privilegeName, ref TOKEN_PRIVI
406406
IntPtr tokenHandler = IntPtr.Zero;
407407
if (OpenProcessToken(processHandler, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, out tokenHandler))
408408
{
409-
int bufferSize = ClrFacade.SizeOf<TOKEN_PRIVILEGE>();
409+
int bufferSize = Marshal.SizeOf<TOKEN_PRIVILEGE>();
410410
int returnSize = 0;
411411

412412
// restore the privilege state back to the previous privilege state

0 commit comments

Comments
 (0)