Files
PowerShell/src/System.Management.Automation/engine/CodeMethods.cs
T
Steve Lee 4af3068566 Code cleanup: Add space after closing brace where needed (#8530)
* Update TestService

* Update WebListener

* Update Controllers

* Update ExpTest

* Update MyApp

* Update Logic

* Update Logic

* Update MyApp

* Update Microsoft.Management.Infrastructure.CimCmdlets

* Update Microsoft.PowerShell.Commands.Diagnostics

* Update Microsoft.PowerShell.ScheduledJob

* Update Microsoft.WSMan.Management

* Update Microsoft.WSMan.Runtime

* Update ResGen

* Update TypeCatalogGen

* Update commands

* Update Eventing

* Update Reader

* Update utility

* Update ShowCommand

* Update trace

* Update WebCmdlet

* Update Common

* Update CoreCLR

* Update common

* Update format-object

* Update format-wide

* Update out-file

* Update out-printer

* Update out-string

* Update OutGridView

* Update LocalAccounts

* Update Commands

* Update security

* Update CoreCLR

* Update DscSupport

* Update engine

* Update help

* Update logging

* Update namespaces

* Update security

* Update utils

* Update config

* Update perfCounters

* Update tracing

* Update cmdletization

* Update other

* Update cim

* Update xml

* Update CoreCLR

* Update common

* Update DefaultFormatters

* Update out-console

* Update out-textInterface

* Update DisplayDatabase

* Update Utilities

* Update COM

* Update ComInterop

* Update CommandCompletion

* Update debugger

* Update ExperimentalFeature

* Update hostifaces

* Update interpreter

* Update lang

* Update Modules

* Update parser

* Update runtime

* Update client

* Update commands

* Update common

* Update fanin

* Update server

* Update WireDataFormat

* Update Binding

* Update Operations

* Update interface

* Update cmdletization

* Update cim

* Update management

* Update WindowsTaskbarJumpList

* Update msh
2018-12-24 11:20:06 +05:00

136 lines
5.1 KiB
C#

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#if !UNIX
using System;
using System.Collections;
using System.Reflection;
using System.Globalization;
using System.Management.Automation;
using System.Diagnostics.CodeAnalysis;
using System.DirectoryServices;
using Dbg = System.Management.Automation.Diagnostics;
namespace Microsoft.PowerShell
{
/// <summary>
/// Contains auxiliary ToString CodeMethod implementations for some types
/// </summary>
public static partial class ToStringCodeMethods
{
/// <summary>
/// ToString implementation for PropertyValueCollection
/// </summary>
/// <param name="instance">instance of PSObject wrapping a PropertyValueCollection</param>
public static string PropertyValueCollection(PSObject instance)
{
if (instance == null)
return String.Empty;
var values = (PropertyValueCollection)instance.BaseObject;
if (values == null)
return String.Empty;
if (values.Count == 1)
{
if (values[0] == null)
{
return String.Empty;
}
return (PSObject.AsPSObject(values[0]).ToString());
}
return PSObject.ToStringEnumerable(null, (IEnumerable)values, null, null, null);
}
}
/// <summary>
/// Contains CodeMethod implementations for some adapted types like:
///
/// 1. DirectoryEntry Related Code Methods
/// (a) Convert from DE LargeInteger to Int64.
/// (b) Convert from DE Dn-With-Binary to string.
/// </summary>
public static class AdapterCodeMethods
{
#region DirectoryEntry related CodeMethods
/// <summary>
/// Converts instance of LargeInteger to .net Int64.
/// </summary>
/// <param name="deInstance">Instance of PSObject wrapping DirectoryEntry object</param>
/// <param name="largeIntegerInstance">Instance of PSObject wrapping LargeInteger instance</param>
/// <returns>Converted Int64.</returns>
[SuppressMessage("Microsoft.Naming", "CA1720:IdentifiersShouldNotContainTypeNames", MessageId = "integer")]
public static Int64 ConvertLargeIntegerToInt64(PSObject deInstance, PSObject largeIntegerInstance)
{
if (largeIntegerInstance == null)
{
throw PSTraceSource.NewArgumentException("largeIntegerInstance");
}
object largeIntObject = (object)largeIntegerInstance.BaseObject;
Type largeIntType = largeIntObject.GetType();
// the following code might throw exceptions,
// engine will catch these exceptions
int highPart = (int)largeIntType.InvokeMember("HighPart",
BindingFlags.GetProperty | BindingFlags.Public,
null,
largeIntObject,
null,
CultureInfo.InvariantCulture);
int lowPart = (int)largeIntType.InvokeMember("LowPart",
BindingFlags.GetProperty | BindingFlags.Public,
null,
largeIntObject,
null,
CultureInfo.InvariantCulture);
// LowPart is not really a signed integer. Do not try to
// use LowPart as a signed integer or you may get intermittent
// surprises.
// (long)highPart << 32 | (uint)lowPart
byte[] data = new byte[8];
BitConverter.GetBytes(lowPart).CopyTo(data, 0);
BitConverter.GetBytes(highPart).CopyTo(data, 4);
return BitConverter.ToInt64(data, 0);
}
/// <summary>
/// Converts instance of DN-With-Binary to .net String
/// </summary>
/// <param name="deInstance">Instance of PSObject wrapping DirectoryEntry object</param>
/// <param name="dnWithBinaryInstance">Instance of PSObject wrapping DN-With-Binary object</param>
/// <returns>Converted string.</returns>
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "dn", Justification = "DN represents valid prefix w.r.t Active Directory.")]
public static string ConvertDNWithBinaryToString(PSObject deInstance, PSObject dnWithBinaryInstance)
{
if (dnWithBinaryInstance == null)
{
throw PSTraceSource.NewArgumentException("dnWithBinaryInstance");
}
object dnWithBinaryObject = (object)dnWithBinaryInstance.BaseObject;
Type dnWithBinaryType = dnWithBinaryObject.GetType();
// the following code might throw exceptions,
// engine will catch these exceptions
string dnString = (string)dnWithBinaryType.InvokeMember("DNString",
BindingFlags.GetProperty | BindingFlags.Public,
null,
dnWithBinaryObject,
null,
CultureInfo.InvariantCulture);
return dnString;
}
#endregion
}
}
#endif