internal static T GetPropertyValue <T>(PSObject psObject, string propertyName) { using (RemotingDecoder._trace.TraceMethod()) { if (psObject == null) { throw RemotingDecoder._trace.NewArgumentNullException(nameof(psObject)); } if (propertyName == null) { throw RemotingDecoder._trace.NewArgumentNullException(nameof(propertyName)); } object propertyValue = RemotingDecoder.GetProperty(psObject, propertyName).Value; return(RemotingDecoder.ConvertPropertyValueTo <T>(propertyName, propertyValue)); } }
internal static IEnumerable <T> EnumerateListProperty <T>( PSObject psObject, string propertyName) { using (RemotingDecoder._trace.TraceMethod()) { if (psObject == null) { throw RemotingDecoder._trace.NewArgumentNullException(nameof(psObject)); } IEnumerable e = propertyName != null?RemotingDecoder.GetPropertyValue <IEnumerable>(psObject, propertyName) : throw RemotingDecoder._trace.NewArgumentNullException(nameof(propertyName)); if (e != null) { foreach (object propertyValue in e) { yield return(RemotingDecoder.ConvertPropertyValueTo <T>(propertyName, propertyValue)); } } } }
internal static IEnumerable <KeyValuePair <KeyType, ValueType> > EnumerateHashtableProperty <KeyType, ValueType>( PSObject psObject, string propertyName) { using (RemotingDecoder._trace.TraceMethod()) { if (psObject == null) { throw RemotingDecoder._trace.NewArgumentNullException(nameof(psObject)); } Hashtable h = propertyName != null?RemotingDecoder.GetPropertyValue <Hashtable>(psObject, propertyName) : throw RemotingDecoder._trace.NewArgumentNullException(nameof(propertyName)); if (h != null) { foreach (DictionaryEntry dictionaryEntry in h) { KeyType key = RemotingDecoder.ConvertPropertyValueTo <KeyType>(propertyName, dictionaryEntry.Key); ValueType value = RemotingDecoder.ConvertPropertyValueTo <ValueType>(propertyName, dictionaryEntry.Value); yield return(new KeyValuePair <KeyType, ValueType>(key, value)); } } } }
private static T ConvertPropertyValueTo <T>(string propertyName, object propertyValue) { using (RemotingDecoder._trace.TraceMethod()) { if (propertyName == null) { throw RemotingDecoder._trace.NewArgumentNullException(nameof(propertyName)); } if (typeof(T).IsEnum) { if (propertyValue is string) { try { return((T)Enum.Parse(typeof(T), (string)propertyValue, true)); } catch (ArgumentException ex) { throw new PSRemotingDataStructureException(PSRemotingErrorId.CantCastPropertyToExpectedType, new object[3] { (object)propertyName, (object)typeof(T).FullName, (object)propertyValue.GetType().FullName }); } } else { try { Type underlyingType = Enum.GetUnderlyingType(typeof(T)); return((T)LanguagePrimitives.ConvertTo(propertyValue, underlyingType, (IFormatProvider)CultureInfo.InvariantCulture)); } catch (InvalidCastException ex) { throw new PSRemotingDataStructureException(PSRemotingErrorId.CantCastPropertyToExpectedType, new object[3] { (object)propertyName, (object)typeof(T).FullName, (object)propertyValue.GetType().FullName }); } } } else { if (typeof(T).Equals(typeof(PSObject))) { return(propertyValue == null ? default(T) : (T)PSObject.AsPSObject(propertyValue)); } switch (propertyValue) { case null: if (!typeof(T).IsValueType) { return(default(T)); } if (typeof(T).IsGenericType && typeof(T).GetGenericTypeDefinition().Equals(typeof(Nullable <>))) { return(default(T)); } throw new PSRemotingDataStructureException(PSRemotingErrorId.CantCastPropertyToExpectedType, new object[3] { (object)propertyName, (object)typeof(T).FullName, propertyValue != null ? (object)propertyValue.GetType().FullName : (object)"null" }); case T obj: return(obj); case PSObject _: PSObject psObject = (PSObject)propertyValue; return(RemotingDecoder.ConvertPropertyValueTo <T>(propertyName, psObject.BaseObject)); case Hashtable _: if (typeof(T).Equals(typeof(PSPrimitiveDictionary))) { try { return((T) new PSPrimitiveDictionary((Hashtable)propertyValue)); } catch (ArgumentException ex) { throw new PSRemotingDataStructureException(PSRemotingErrorId.CantCastPropertyToExpectedType, new object[3] { (object)propertyName, (object)typeof(T).FullName, propertyValue != null ? (object)propertyValue.GetType().FullName : (object)"null" }); } } else { break; } } throw new PSRemotingDataStructureException(PSRemotingErrorId.CantCastPropertyToExpectedType, new object[3] { (object)propertyName, (object)typeof(T).FullName, (object)propertyValue.GetType().FullName }); } } }