/
githubmirror
/
PowerShell
Обзор
Документация
Войти
/
githubmirror
/
PowerShell
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/System.Management.Automation/engine/ArgumentToVersionTransformationAttribute.cs
58 строк
2 KB
Maksim Ivanyuk
Add `-HttpVersion` parameter to web cmdlets (#15853)
03 ноя 2021, 02:53
Не верифицирован
03 ноя 2021, 02:53
674ef88
Код
Авторство
О чём код?
// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. #nullable enable using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; namespace System.Management.Automation { /// <summary> /// To make it easier to specify a version, we add some conversions that wouldn't happen otherwise: /// * A simple integer, i.e. 2; /// * A string without a dot, i.e. "2". /// </summary> internal class ArgumentToVersionTransformationAttribute : ArgumentTransformationAttribute { /// <inheritdoc/> public override object Transform(EngineIntrinsics engineIntrinsics, object inputData) { object version = PSObject.Base(inputData); if (version is string versionStr) { if (TryConvertFromString(versionStr, out var convertedVersion)) { return convertedVersion; } if (versionStr.Contains('.')) { // If the string contains a '.', let the Version constructor handle the conversion. return inputData; } } if (version is double) { // The conversion to int below is wrong, but the usual conversions will turn // the double into a string, so just return the original object. return inputData; } if (LanguagePrimitives.TryConvertTo<int>(version, out var majorVersion)) { return new Version(majorVersion, 0); } return inputData; } protected virtual bool TryConvertFromString(string versionString, [NotNullWhen(true)] out Version? version) { version = null; return false; } } }