internal static bool TryGetInstanceMember(object value, string memberName, out PSMemberInfo memberInfo) { PSMemberInfoInternalCollection <PSMemberInfo> internals; memberInfo = PSObject.HasInstanceMembers(value, out internals) ? internals[memberName] : null; return(memberInfo != null); }
/// <summary> /// Add the member to the target object. /// </summary> /// <param name="member"></param> /// <returns></returns> private bool AddMemberToTarget(PSMemberInfo member) { PSMemberInfo previousMember = _inputObject.Members[member.Name]; if (previousMember != null) { if (!_force) { WriteError(NewError("MemberAlreadyExists", "MemberAlreadyExists", _inputObject, member.Name)); return(false); } else { if (previousMember.IsInstance) { _inputObject.Members.Remove(member.Name); } else { WriteError(NewError("CannotRemoveTypeDataMember", "CannotRemoveTypeDataMember", _inputObject, member.Name, previousMember.MemberType)); return(false); } } } _inputObject.Members.Add(member); return(true); }
internal static object GetAdaptedValue(object obj, string member) { ExecutionContext executionContextFromTLS = LocalPipeline.GetExecutionContextFromTLS(); PSMemberInfo memberInfo = null; if ((executionContextFromTLS != null) && (executionContextFromTLS.TypeTable != null)) { ConsolidatedString typeNames = PSObject.GetTypeNames(obj); memberInfo = executionContextFromTLS.TypeTable.GetMembers <PSMemberInfo>(typeNames)[member]; if (memberInfo != null) { memberInfo = CloneMemberInfo(memberInfo, obj); } } PSObject.AdapterSet mappedAdapter = PSObject.GetMappedAdapter(obj, (executionContextFromTLS != null) ? executionContextFromTLS.TypeTable : null); if (memberInfo == null) { memberInfo = mappedAdapter.OriginalAdapter.BaseGetMember <PSMemberInfo>(obj, member); } if ((memberInfo == null) && (mappedAdapter.DotNetAdapter != null)) { memberInfo = mappedAdapter.DotNetAdapter.BaseGetMember <PSMemberInfo>(obj, member); } if (memberInfo != null) { return(memberInfo.Value); } if ((executionContextFromTLS != null) && executionContextFromTLS.IsStrictVersion(2)) { throw new PropertyNotFoundException("PropertyNotFoundStrict", null, ParserStrings.PropertyNotFoundStrict, new object[] { LanguagePrimitives.ConvertTo <string>(member) }); } return(null); }
internal static PSMemberInfo ResolveAlias(PSAliasProperty alias, DynamicMetaObject target, HashSet <string> aliases, List <BindingRestrictions> aliasRestrictions) { bool flag; Type type; BindingRestrictions restrictions; if (aliases == null) { aliases = new HashSet <string> { alias.Name }; } else { if (aliases.Contains(alias.Name)) { throw new ExtendedTypeSystemException("CycleInAliasLookup", null, ExtendedTypeSystem.CycleInAlias, new object[] { alias.Name }); } aliases.Add(alias.Name); } PSGetMemberBinder binder = Get(alias.ReferencedMemberName, false); if (binder.HasInstanceMember) { return(null); } PSMemberInfo info = binder.GetPSMemberInfo(target, out restrictions, out flag, out type, aliases, aliasRestrictions); aliasRestrictions.Add(BinderUtils.GetVersionCheck(binder, binder._version)); return(info); }
internal string GetResultObjectDN(Collection <PSObject> result) { Log.WriteStart("GetResultObjectDN"); if (result == null) { throw new ArgumentNullException("result", "Execution result is not specified"); } if (result.Count < 1) { throw new ArgumentException("Execution result does not contain any object"); } if (result.Count > 1) { throw new ArgumentException("Execution result contains more than one object"); } PSMemberInfo info = result[0].Members["DistinguishedName"]; if (info == null) { throw new ArgumentException("Execution result does not contain DistinguishedName property", "result"); } string ret = info.Value.ToString(); Log.WriteEnd("GetResultObjectDN"); return(ret); }
/// <summary> /// Returns the identity of the object from the shell execution result /// </summary> /// <param name="result"></param> /// <returns></returns> internal string GetResultObjectIdentity(Collection <PSObject> result) { HostedSolutionLog.LogStart("GetResultObjectIdentity"); if (result == null) { throw new ArgumentNullException("result", "Execution result is not specified"); } if (result.Count < 1) { throw new ArgumentException("Execution result is empty", "result"); } if (result.Count > 1) { throw new ArgumentException("Execution result contains more than one object", "result"); } PSMemberInfo info = result[0].Members["Identity"]; if (info == null) { throw new ArgumentException("Execution result does not contain Identity property", "result"); } string ret = info.Value.ToString(); HostedSolutionLog.LogEnd("GetResultObjectIdentity"); return(ret); }
/// <summary> /// Checks the object from the shell execution result. /// </summary> /// <param name="result"></param> /// <returns>Distinguished name of the object if object exists or null otherwise.</returns> internal string CheckResultObjectDN(Collection <PSObject> result) { HostedSolutionLog.LogStart("CheckResultObjectDN"); if (result == null) { return(null); } if (result.Count < 1) { return(null); } PSMemberInfo info = result[0].Members["DistinguishedName"]; if (info == null) { throw new ArgumentException("Execution result does not contain DistinguishedName property", "result"); } string ret = info.Value.ToString(); HostedSolutionLog.LogEnd("CheckResultObjectDN"); return(ret); }
internal string GetResultObjectIdentity(Collection <PSObject> result) { Log.WriteStart("GetResultObjectIdentity", new object[0]); if (result == null) { throw new ArgumentNullException("result", "Execution result is not specified"); } if (result.Count < 1) { throw new ArgumentException("Execution result is empty", "result"); } if (result.Count > 1) { throw new ArgumentException("Execution result contains more than one object", "result"); } PSMemberInfo item = result[0].Members["Identity"]; if (item == null) { throw new ArgumentException("Execution result does not contain Identity property", "result"); } string str = item.Value.ToString(); Log.WriteEnd("GetResultObjectIdentity", new object[0]); return(str); }
/// <summary> /// ProcessRecord method. /// </summary> protected override void ProcessRecord() { if (Property != null) { if (InputObject == null) { WriteError(NewError("MissingInputObjectParameter", "MissingInputObjectParameter", null)); } else { if (_listModifier == null) { _listModifier = CreatePSListModifier(); } PSMemberInfo memberInfo = InputObject.Members[Property]; if (memberInfo != null) { try { _listModifier.ApplyTo(memberInfo.Value); WriteObject(InputObject); } catch (PSInvalidOperationException e) { WriteError(new ErrorRecord(e, "ApplyFailed", ErrorCategory.InvalidOperation, null)); } } else { WriteError(NewError("MemberDoesntExist", "MemberDoesntExist", InputObject, Property)); } } } }
private MshExpressionResult GetValue(PSObject target, bool eatExceptions) { try { object obj2; if (this._script != null) { obj2 = this._script.DoInvokeReturnAsIs(true, ScriptBlock.ErrorHandlingBehavior.WriteToExternalErrorPipe, target, AutomationNull.Value, AutomationNull.Value, new object[0]); } else { PSMemberInfo info = target.Properties[this._stringValue]; if (info == null) { return(new MshExpressionResult(null, this, null)); } obj2 = info.Value; } return(new MshExpressionResult(obj2, this, null)); } catch (RuntimeException exception) { if (!eatExceptions) { throw; } return(new MshExpressionResult(null, this, exception)); } }
internal static object GetProperty(PSObject so, string name) { PSMemberInfo info = so.Properties[name]; if (info == null) { return(null); } return(info.Value); }
private static object GetPropertyValue(PSObject mshObject, string propertyName) { PSMemberInfo info = mshObject.Properties[propertyName]; if (info == null) { return(null); } return(info.Value); }
/// <summary> /// Returns the value of property from the inputObject. /// </summary> /// <param name="inputObject">Object for which the property value is needed.</param> /// <param name="property">Name of the property for which the value is returned.</param> /// <returns> /// null if property is not found, /// value of the property otherwise. /// </returns> private object GetPropValue(PSObject inputObject, string property) { // This is an internal method. Validate input passed to this method. Debug.Assert(inputObject != null, "Cannot work with a null object."); Debug.Assert(!string.IsNullOrEmpty(property), "Property should not be null or Empty"); // We have no globbing: try an exact match, because this is quicker. PSMemberInfo x = inputObject.Members[property]; return(null == x ? null : x.Value); }
internal object GetPSObjectProperty(PSObject obj, string name) { PSMemberInfo info = obj.Members[name]; if (info == null) { throw new ArgumentException(String.Format("PSObject does not contain '{0}' property", name), "obj"); } return(info.Value); }
private void FilteredWriteObject(PSObject obj, List <PSNoteProperty> addedNoteProperties) { Diagnostics.Assert(obj != null, "This command should never write null"); if (!_unique) { if (obj != AutomationNull.Value) { SetPSCustomObject(obj, newPSObject: addedNoteProperties.Count > 0); WriteObject(obj); } return; } // if only unique is mentioned else if ((_unique)) { bool isObjUnique = true; foreach (UniquePSObjectHelper uniqueObj in _uniques) { ObjectCommandComparer comparer = new(true, CultureInfo.CurrentCulture, true); if ((comparer.Compare(obj.BaseObject, uniqueObj.WrittenObject.BaseObject) == 0) && (uniqueObj.NotePropertyCount == addedNoteProperties.Count)) { bool found = true; foreach (PSNoteProperty note in addedNoteProperties) { PSMemberInfo prop = uniqueObj.WrittenObject.Properties[note.Name]; if (prop == null || comparer.Compare(prop.Value, note.Value) != 0) { found = false; break; } } if (found) { isObjUnique = false; break; } } else { continue; } } if (isObjUnique) { SetPSCustomObject(obj, newPSObject: addedNoteProperties.Count > 0); _uniques.Add(new UniquePSObjectHelper(obj, addedNoteProperties.Count)); } } }
private string TryGetPropertyValue(PSMemberInfo psPropertyInfo) { try { return(_primitiveTypes.Contains(psPropertyInfo.TypeNameOfValue) ? psPropertyInfo.Value?.ToString() : null); } catch (GetValueInvocationException) { return(null); } }
internal static object GetProperty(PSObject so, string name) { PSMemberInfo member = so.Properties[name]; if (member == null) { return(null); } // NOTE: we do not distinguish between property not there and null property // if an exception is thrown, it would be considered an internal failure return(member.Value); }
internal static bool TryGetPropertyValue(this PSObject psObject, string propertyName, out object value) { PSMemberInfo property = psObject.Properties[propertyName]; if (property is null) { value = default; return(false); } value = property.Value; return(true); }
private void AddDynamicMemberAndScale(PSMemberInfo plotMember) { if (null == plotMember) { return; } SafeAddDynamicMember(plotMember); var name = plotMember.Name; AddDynamicScaleForProperty(name); }
private ReadOnlyPSMemberInfoCollection <PSMemberInfo> GetMatchMembers() { if (WildcardPattern.ContainsWildcardCharacters(this._property)) { return(this._inputObject.Members.Match(this._property, PSMemberTypes.All)); } PSMemberInfoInternalCollection <PSMemberInfo> members = new PSMemberInfoInternalCollection <PSMemberInfo>(); PSMemberInfo member = this._inputObject.Members[this._property]; if (member != null) { members.Add(member); } return(new ReadOnlyPSMemberInfoCollection <PSMemberInfo>(members)); }
internal static OneGetPackage convertPSOtoOGP(PSObject pso) { OneGetPackage result = new OneGetPackage(); PSMemberInfo count = pso.Properties["Status"]; result.status = count.Value.ToString() ?? " "; count = pso.Properties["Summary"]; result.summary = count.Value.ToString() ?? " "; count = pso.Properties["Name"]; result.name = count.Value.ToString() ?? " "; count = pso.Properties["Version"]; result.version = count.Value.ToString() ?? " "; return(result); }
/// <summary> /// This method tries to get name and value from <see cref="PSMemberInfo"/> and /// creates <see cref="ConnectorAttribute"/> out of it /// </summary> /// <param name="info">PSMemberInfo to get the data from</param> /// <returns>Created ConnectorAttribute or null if not possible to create it</returns> private static ConnectorAttribute GetAsAttribute(PSMemberInfo info) { Assertions.NullCheck(info, "param"); if (info.Value != null) { string value = info.Value.ToString(); // TODO: add type recognition, currently only string is supported if (value != info.Value.GetType().ToString() && !string.IsNullOrEmpty(value)) { return(ConnectorAttributeBuilder.Build(info.Name, value)); } } return(null); }
private void SendToOutputPipe(PSObject objectToWrite, List <PSNoteProperty> addedNoteProperties) { if (!unique) { if (objectToWrite != null) { WriteObject(objectToWrite); } return; } bool isObjUnique = true; foreach (UniquePSObjectHelper uniqueObj in uniques) { ObjectCommandComparer comparer = new ObjectCommandComparer(true, System.Threading.Thread.CurrentThread.CurrentCulture, true); if ((comparer.Compare(objectToWrite.BaseObject, uniqueObj.WrittenObject.BaseObject) == 0) && (uniqueObj.NotePropertyCount == addedNoteProperties.Count)) { bool found = true; foreach (PSNoteProperty note in addedNoteProperties) { PSMemberInfo prop = uniqueObj.WrittenObject.Properties[note.Name]; if (prop == null || comparer.Compare(prop.Value, note.Value) != 0) { found = false; break; } } if (found) { isObjUnique = false; break; } } else { continue; } } if (isObjUnique) { uniques.Add(new UniquePSObjectHelper(objectToWrite, addedNoteProperties.Count)); } }
private void SafeAddDynamicMember(PSMemberInfo plotMember) { using (_log.PushContext("SafeAddDynamicMember")) { if (null == plotMember) { _log.Debug("no plot member specified"); return; } if (!(from dm in _dynamicMembers where Name == plotMember.Name select dm).Any()) { _log.DebugFormat("adding dynamic member [{0}]", plotMember.Name); _dynamicMembers.Add(plotMember); } } }
private string GetMemberInfoAsString(PSMemberInfo member) { var memberValue = (float)member.Value; //Fix for bug 326: memberValue = (float)Math.Round((Decimal)memberValue, 1, MidpointRounding.AwayFromZero); var memberValueString = memberValue.ToString(); //Fix for bug 327: if (memberValueString.Length > 1 && !(memberValueString.Contains(",") || memberValueString.Contains("."))) { memberValueString = memberValueString[0] + "." + memberValueString[1]; } return(memberValueString); }
internal static object SetAdaptedValue(object obj, string member, object value) { object obj2; try { ExecutionContext executionContextFromTLS = LocalPipeline.GetExecutionContextFromTLS(); PSMemberInfo memberInfo = null; if ((executionContextFromTLS != null) && (executionContextFromTLS.TypeTable != null)) { ConsolidatedString typeNames = PSObject.GetTypeNames(obj); memberInfo = executionContextFromTLS.TypeTable.GetMembers <PSMemberInfo>(typeNames)[member]; if (memberInfo != null) { memberInfo = PSGetMemberBinder.CloneMemberInfo(memberInfo, obj); } } PSObject.AdapterSet mappedAdapter = PSObject.GetMappedAdapter(obj, (executionContextFromTLS != null) ? executionContextFromTLS.TypeTable : null); if (memberInfo == null) { memberInfo = mappedAdapter.OriginalAdapter.BaseGetMember <PSMemberInfo>(obj, member); } if ((memberInfo == null) && (mappedAdapter.DotNetAdapter != null)) { memberInfo = mappedAdapter.DotNetAdapter.BaseGetMember <PSMemberInfo>(obj, member); } if (memberInfo == null) { throw InterpreterError.NewInterpreterException(null, typeof(RuntimeException), null, "PropertyAssignmentException", ParserStrings.PropertyNotFound, new object[] { member }); } memberInfo.Value = value; obj2 = value; } catch (SetValueException) { throw; } catch (Exception exception) { ExceptionHandlingOps.ConvertToMethodInvocationException(exception, typeof(SetValueInvocationException), member, 0, null); throw; } return(obj2); }
private void FilteredWriteObject(PSObject obj, List <PSNoteProperty> addedNoteProperties) { if (!this.unique) { if (obj != AutomationNull.Value) { this.SetPSCustomObject(obj); base.WriteObject(obj); } } else if (this.unique) { bool flag = true; foreach (UniquePSObjectHelper helper in this.uniques) { ObjectCommandComparer comparer = new ObjectCommandComparer(true, Thread.CurrentThread.CurrentCulture, true); if ((comparer.Compare(obj.BaseObject, helper.WrittenObject.BaseObject) == 0) && (helper.NotePropertyCount == addedNoteProperties.Count)) { bool flag2 = true; foreach (PSNoteProperty property in addedNoteProperties) { PSMemberInfo info = helper.WrittenObject.Properties[property.Name]; if ((info == null) || (comparer.Compare(info.Value, property.Value) != 0)) { flag2 = false; break; } } if (flag2) { flag = false; break; } } } if (flag) { this.SetPSCustomObject(obj); this.uniques.Add(new UniquePSObjectHelper(obj, addedNoteProperties.Count)); } } }
/// <summary> /// Returns the identity of the PS object /// </summary> /// <param name="obj"></param> /// <returns></returns> internal string GetPSObjectIdentity(PSObject obj) { HostedSolutionLog.LogStart("GetPSObjectIdentity"); if (obj == null) { throw new ArgumentNullException("obj", "PSObject is not specified"); } PSMemberInfo info = obj.Members["Identity"]; if (info == null) { throw new ArgumentException("PSObject does not contain Identity property", "obj"); } string ret = info.Value.ToString(); HostedSolutionLog.LogEnd("GetPSObjectIdentity"); return(ret); }
private bool AddMemberToTarget(PSMemberInfo member) { PSMemberInfo info = this.inputObject.Members[member.Name]; if (info != null) { if (!this.force) { base.WriteError(this.NewError("MemberAlreadyExists", "MemberAlreadyExists", this.inputObject, new object[] { member.Name })); return(false); } if (!info.IsInstance) { base.WriteError(this.NewError("CannotRemoveTypeDataMember", "CannotRemoveTypeDataMember", this.inputObject, new object[] { member.Name, info.MemberType })); return(false); } this.inputObject.Members.Remove(member.Name); } this.inputObject.Members.Add(member); return(true); }
private MshExpressionResult GetValue(PSObject target, bool eatExceptions) { try { object result; if (Script != null) { result = Script.DoInvokeReturnAsIs( useLocalScope: true, errorHandlingBehavior: ScriptBlock.ErrorHandlingBehavior.WriteToExternalErrorPipe, dollarUnder: target, input: AutomationNull.Value, scriptThis: AutomationNull.Value, args: Utils.EmptyArray <object>()); } else { PSMemberInfo member = target.Properties[_stringValue]; if (member == null) { return(new MshExpressionResult(null, this, null)); } result = member.Value; } return(new MshExpressionResult(result, this, null)); } catch (RuntimeException e) { if (eatExceptions) { return(new MshExpressionResult(null, this, e)); } else { throw; } } }
// internals //internal void ReplicateInstance(PSObject particularInstance); //internal void SetValueNoConversion(object setValue); //internal bool IsHidden { get; } //internal bool IsReservedMember { get; } //internal bool ShouldSerialize { set; get; } //internal bool isHidden; //internal bool isInstance; //internal bool isReservedMember; //internal string name; //internal bool shouldSerialize; internal void CopyProperties(PSMemberInfo toObj) { toObj.Name = Name; toObj.IsInstance = IsInstance; }