/********************************************************************++ Copyright (c) Microsoft Corporation. All rights reserved. --********************************************************************/ using System.Dynamic; using System.Management.Automation.Language; using System.Runtime.CompilerServices; namespace System.Management.Automation { /// /// Define type for a reference object in Monad scripting language. /// /// /// This class is used to describe both kinds of references: /// a. reference to a value: _value will be holding the value being referenced. /// b. reference to a variable: _value will be holding a PSVariable instance for the variable to be referenced. /// /// A reference is created in following ways, /// a. value reference /// $a = [ref] 3 /// [ref] $a = 3 /// [ref] $a = $b /// b. variable reference /// $a = [ref] $b /// /// public class PSReference { private object _value; /// /// Create an instance of PSReference. /// /// public PSReference(object value) { _value = value; } /// /// Get and set value of PSReference. /// /// /// If underlining object is a value, the object itself will be operated on. /// If underlining object is a variable, the variable will be operated on. /// public object Value { get { PSVariable variable = _value as PSVariable; if (variable != null) { return variable.Value; } return _value; } set { PSVariable variable = _value as PSVariable; if (variable != null) { variable.Value = value; return; } _value = value; } } internal static readonly CallSite> CreatePsReferenceInstance = CallSite>.Create(PSCreateInstanceBinder.Get(new CallInfo(1), null)); internal static PSReference CreateInstance(object value, Type typeOfValue) { Type psReferType = typeof(PSReference<>).MakeGenericType(typeOfValue); return (PSReference)CreatePsReferenceInstance.Target.Invoke(CreatePsReferenceInstance, psReferType, value); } } internal class PSReference : PSReference { public PSReference(object value) : base(value) { } } }