MegaBites-AI/Windows-powershell
0308
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System.Collections.Generic;5using System.Collections.ObjectModel;6using System.Reflection;7 8namespace System.Management.Automation9{10 /// <summary>11 /// Internal wrapper for third-party adapters (PSPropertyAdapter)12 /// </summary>13 internal class ThirdPartyAdapter : PropertyOnlyAdapter14 {15 internal ThirdPartyAdapter(Type adaptedType, PSPropertyAdapter externalAdapter)16 {17 AdaptedType = adaptedType;18 _externalAdapter = externalAdapter;19 }20 21 /// <summary>22 /// The type this instance is adapting.23 /// </summary>24 internal Type AdaptedType { get; }25 26 /// <summary>27 /// The type of the external adapter.28 /// </summary>29 internal Type ExternalAdapterType30 {31 get32 {33 return _externalAdapter.GetType();34 }35 }36 37 /// <summary>38 /// Returns the TypeNameHierarchy out of an object.39 /// </summary>40 protected override IEnumerable<string> GetTypeNameHierarchy(object obj)41 {42 Collection<string> typeNameHierarchy = null;43 44 try45 {46 typeNameHierarchy = _externalAdapter.GetTypeNameHierarchy(obj);47 }48 catch (Exception exception)49 {50 throw new ExtendedTypeSystemException(51 "PSPropertyAdapter.GetTypeNameHierarchyError",52 exception,53 ExtendedTypeSystem.GetTypeNameHierarchyError, obj.ToString());54 }55 56 if (typeNameHierarchy == null)57 {58 throw new ExtendedTypeSystemException(59 "PSPropertyAdapter.NullReturnValueError",60 null,61 ExtendedTypeSystem.NullReturnValueError, "PSPropertyAdapter.GetTypeNameHierarchy");62 }63 64 return typeNameHierarchy;65 }66 67 /// <summary>68 /// Retrieves all the properties available in the object.69 /// </summary>70 protected override void DoAddAllProperties<T>(object obj, PSMemberInfoInternalCollection<T> members)71 {72 Collection<PSAdaptedProperty> properties = null;73 74 try75 {76 properties = _externalAdapter.GetProperties(obj);77 }78 catch (Exception exception)79 {80 throw new ExtendedTypeSystemException(81 "PSPropertyAdapter.GetProperties",82 exception,83 ExtendedTypeSystem.GetProperties, obj.ToString());84 }85 86 if (properties == null)87 {88 throw new ExtendedTypeSystemException(89 "PSPropertyAdapter.NullReturnValueError",90 null,91 ExtendedTypeSystem.NullReturnValueError, "PSPropertyAdapter.GetProperties");92 }93 94 foreach (PSAdaptedProperty property in properties)95 {96 InitializeProperty(property, obj);97 98 members.Add(property as T);99 }100 }101 102 /// <summary>103 /// Returns null if propertyName is not a property in the adapter or104 /// the corresponding PSProperty with its adapterData set to information105 /// to be used when retrieving the property.106 /// </summary>107 protected override PSProperty DoGetProperty(object obj, string propertyName)108 {109 PSAdaptedProperty property = null;110 111 try112 {113 property = _externalAdapter.GetProperty(obj, propertyName);114 }115 catch (Exception exception)116 {117 throw new ExtendedTypeSystemException(118 "PSPropertyAdapter.GetProperty",119 exception,120 ExtendedTypeSystem.GetProperty, propertyName, obj.ToString());121 }122 123 if (property != null)124 {125 InitializeProperty(property, obj);126 }127 128 return property;129 }130 131 protected override PSProperty DoGetFirstPropertyOrDefault(object obj, MemberNamePredicate predicate)132 {133 PSAdaptedProperty property = null;134 135 try136 {137 property = _externalAdapter.GetFirstPropertyOrDefault(obj, predicate);138 }139 catch (Exception exception)140 {141 throw new ExtendedTypeSystemException(142 "PSPropertyAdapter.GetProperty",143 exception,144 ExtendedTypeSystem.GetProperty, nameof(predicate), obj.ToString());145 }146 147 if (property != null)148 {149 InitializeProperty(property, obj);150 }151 152 return property;153 }154 155 /// <summary>156 /// Ensures that the adapter and base object are set in the given PSAdaptedProperty.157 /// </summary>158 private void InitializeProperty(PSAdaptedProperty property, object baseObject)159 {160 if (property.adapter == null)161 {162 property.adapter = this;163 property.baseObject = baseObject;164 }165 }166 167 /// <summary>168 /// Returns true if the property is settable.169 /// </summary>170 protected override bool PropertyIsSettable(PSProperty property)171 {172 PSAdaptedProperty adaptedProperty = property as PSAdaptedProperty;173 174 Diagnostics.Assert(adaptedProperty != null, "ThirdPartyAdapter should only receive PSAdaptedProperties");175 176 try177 {178 return _externalAdapter.IsSettable(adaptedProperty);179 }180 catch (Exception exception)181 {182 throw new ExtendedTypeSystemException(183 "PSPropertyAdapter.PropertyIsSettableError",184 exception,185 ExtendedTypeSystem.PropertyIsSettableError, property.Name);186 }187 }188 189 /// <summary>190 /// Returns true if the property is gettable.191 /// </summary>192 protected override bool PropertyIsGettable(PSProperty property)193 {194 PSAdaptedProperty adaptedProperty = property as PSAdaptedProperty;195 196 Diagnostics.Assert(adaptedProperty != null, "ThirdPartyAdapter should only receive PSAdaptedProperties");197 198 try199 {200 return _externalAdapter.IsGettable(adaptedProperty);201 }202 catch (Exception exception)203 {204 throw new ExtendedTypeSystemException(205 "PSPropertyAdapter.PropertyIsGettableError",206 exception,207 ExtendedTypeSystem.PropertyIsGettableError, property.Name);208 }209 }210 211 /// <summary>212 /// Returns the value from a property coming from a previous call to DoGetProperty.213 /// </summary>214 protected override object PropertyGet(PSProperty property)215 {216 PSAdaptedProperty adaptedProperty = property as PSAdaptedProperty;217 218 Diagnostics.Assert(adaptedProperty != null, "ThirdPartyAdapter should only receive PSAdaptedProperties");219 220 try221 {222 return _externalAdapter.GetPropertyValue(adaptedProperty);223 }224 catch (Exception exception)225 {226 throw new ExtendedTypeSystemException(227 "PSPropertyAdapter.PropertyGetError",228 exception,229 ExtendedTypeSystem.PropertyGetError, property.Name);230 }231 }232 233 /// <summary>234 /// Sets the value of a property coming from a previous call to DoGetProperty.235 /// </summary>236 protected override void PropertySet(PSProperty property, object setValue, bool convertIfPossible)237 {238 PSAdaptedProperty adaptedProperty = property as PSAdaptedProperty;239 240 Diagnostics.Assert(adaptedProperty != null, "ThirdPartyAdapter should only receive PSAdaptedProperties");241 242 try243 {244 _externalAdapter.SetPropertyValue(adaptedProperty, setValue);245 }246 catch (SetValueException) { throw; }247 catch (Exception exception)248 {249 throw new ExtendedTypeSystemException(250 "PSPropertyAdapter.PropertySetError",251 exception,252 ExtendedTypeSystem.PropertySetError, property.Name);253 }254 }255 256 /// <summary>257 /// Returns the name of the type corresponding to the property.258 /// </summary>259 protected override string PropertyType(PSProperty property, bool forDisplay)260 {261 PSAdaptedProperty adaptedProperty = property as PSAdaptedProperty;262 263 Diagnostics.Assert(adaptedProperty != null, "ThirdPartyAdapter should only receive PSAdaptedProperties");264 265 string propertyTypeName = null;266 267 try268 {269 propertyTypeName = _externalAdapter.GetPropertyTypeName(adaptedProperty);270 }271 catch (Exception exception)272 {273 throw new ExtendedTypeSystemException(274 "PSPropertyAdapter.PropertyTypeError",275 exception,276 ExtendedTypeSystem.PropertyTypeError, property.Name);277 }278 279 return propertyTypeName ?? "System.Object";280 }281 282 private readonly PSPropertyAdapter _externalAdapter;283 }284 285 /// <summary>286 /// User-defined property adapter.287 /// </summary>288 /// <remarks>289 /// This class is used to expose a simplified version of the type adapter API290 /// </remarks>291 public abstract class PSPropertyAdapter292 {293 /// <summary>294 /// Returns the type hierarchy for the given object.295 /// </summary>296 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1720:IdentifiersShouldNotContainTypeNames", MessageId = "object")]297 public virtual Collection<string> GetTypeNameHierarchy(object baseObject)298 {299 ArgumentNullException.ThrowIfNull(baseObject);300 301 Collection<string> types = new Collection<string>();302 303 for (Type type = baseObject.GetType(); type != null; type = type.BaseType)304 {305 types.Add(type.FullName);306 }307 308 return types;309 }310 311 /// <summary>312 /// Returns a list of the adapted properties.313 /// </summary>314 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1720:IdentifiersShouldNotContainTypeNames", MessageId = "object")]315 public abstract Collection<PSAdaptedProperty> GetProperties(object baseObject);316 317 /// <summary>318 /// Returns a specific property, or null if the base object does not contain the given property.319 /// </summary>320 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1720:IdentifiersShouldNotContainTypeNames", MessageId = "object")]321 public abstract PSAdaptedProperty GetProperty(object baseObject, string propertyName);322 323 /// <summary>324 /// Returns true if the given property is settable.325 /// </summary>326 public abstract bool IsSettable(PSAdaptedProperty adaptedProperty);327 328 /// <summary>329 /// Returns true if the given property is gettable.330 /// </summary>331 public abstract bool IsGettable(PSAdaptedProperty adaptedProperty);332 333 /// <summary>334 /// Returns the value of a given property.335 /// </summary>336 public abstract object GetPropertyValue(PSAdaptedProperty adaptedProperty);337 338 /// <summary>339 /// Sets the value of a given property.340 /// </summary>341 public abstract void SetPropertyValue(PSAdaptedProperty adaptedProperty, object value);342 343 /// <summary>344 /// Returns the type for a given property.345 /// </summary>346 public abstract string GetPropertyTypeName(PSAdaptedProperty adaptedProperty);347 348 /// <summary>349 /// Returns a property if it's name matches the specified <see cref="MemberNamePredicate"/>, otherwise null.350 /// </summary>351 /// <returns>An adapted property if the predicate matches, or <see langword="null"/>.</returns>352 public virtual PSAdaptedProperty GetFirstPropertyOrDefault(object baseObject, MemberNamePredicate predicate)353 {354 foreach (var property in GetProperties(baseObject))355 {356 if (predicate(property.Name))357 {358 return property;359 }360 }361 362 return null;363 }364 }365}366 