MegaBites-AI/Windows-powershell
0308
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System.Dynamic;5using System.Management.Automation.Language;6using System.Runtime.CompilerServices;7 8namespace System.Management.Automation9{10 /// <summary>11 /// Define type for a reference object in PowerShell scripting language.12 /// </summary>13 /// <remarks>14 /// This class is used to describe both kinds of references:15 /// a. reference to a value: _value will be holding the value being referenced.16 /// b. reference to a variable: _value will be holding a PSVariable instance for the variable to be referenced.17 ///18 /// A reference is created in following ways,19 /// a. value reference20 /// $a = [ref] 321 /// [ref] $a = 322 /// [ref] $a = $b23 /// b. variable reference24 /// $a = [ref] $b25 /// </remarks>26 public class PSReference27 {28 private object _value;29 30 /// <summary>31 /// Create an instance of PSReference.32 /// </summary>33 /// <param name="value"></param>34 public PSReference(object value)35 {36 _value = value;37 }38 39 /// <summary>40 /// Get and set value of PSReference.41 /// </summary>42 /// <remarks>43 /// If underlining object is a value, the object itself will be operated on.44 /// If underlining object is a variable, the variable will be operated on.45 /// </remarks>46 public object Value47 {48 get49 {50 PSVariable variable = _value as PSVariable;51 52 if (variable != null)53 {54 return variable.Value;55 }56 57 return _value;58 }59 60 set61 {62 PSVariable variable = _value as PSVariable;63 64 if (variable != null)65 {66 variable.Value = value;67 return;68 }69 70 _value = value;71 }72 }73 74 internal static readonly CallSite<Func<CallSite, object, object, object>> CreatePsReferenceInstance =75 CallSite<Func<CallSite, object, object, object>>.Create(PSCreateInstanceBinder.Get(new CallInfo(1), null));76 77 internal static PSReference CreateInstance(object value, Type typeOfValue)78 {79 Type psReferType = typeof(PSReference<>).MakeGenericType(typeOfValue);80 return (PSReference)CreatePsReferenceInstance.Target.Invoke(CreatePsReferenceInstance, psReferType, value);81 }82 }83 84 internal class PSReference<T> : PSReference85 {86 public PSReference(object value) : base(value)87 {88 }89 }90}91 