Last active
October 28, 2019 06:29
-
-
Save aetos382/067fbec25b4682bf49c484c39f953eb6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Management.Automation; | |
namespace MyTypeAdapter | |
{ | |
internal class CustomProperty : | |
PSAdaptedProperty | |
{ | |
public CustomProperty( | |
string name, | |
Type propertyType, | |
object tag) | |
: base(name, tag) | |
{ | |
if (propertyType is null) | |
{ | |
throw new ArgumentNullException(nameof(propertyType)); | |
} | |
this.PropertyType = propertyType; | |
} | |
public CustomProperty( | |
string name, | |
object value, | |
object tag) | |
: base(name, tag) | |
{ | |
if (value is null) | |
{ | |
throw new ArgumentNullException(nameof(value)); | |
} | |
this.Value = value; | |
this.PropertyType = value.GetType(); | |
} | |
public override bool IsGettable | |
{ | |
get | |
{ | |
return true; | |
} | |
} | |
public override bool IsSettable | |
{ | |
get | |
{ | |
return true; | |
} | |
} | |
public Type PropertyType { get; } | |
public override string TypeNameOfValue | |
{ | |
get | |
{ | |
return this.PropertyType.FullName; | |
} | |
} | |
public override object Value { get; set; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.ObjectModel; | |
using System.Linq; | |
using System.Management.Automation; | |
namespace MyTypeAdapter | |
{ | |
public class StringPropertyAdapter : | |
PSPropertyAdapter | |
{ | |
public override Collection<PSAdaptedProperty> GetProperties( | |
object baseObject) | |
{ | |
string strValue = LanguagePrimitives.ConvertTo<string>(baseObject); | |
var properties = new Collection<PSAdaptedProperty>(); | |
for (int i = 0; i < Math.Min(strValue.Length, 3); ++i) | |
{ | |
var property = new CustomProperty(i.ToString(), strValue[i], null); | |
properties.Add(property); | |
} | |
return properties; | |
} | |
public override PSAdaptedProperty GetProperty( | |
object baseObject, | |
string propertyName) | |
{ | |
var properties = this.GetProperties(baseObject); | |
var property = properties.SingleOrDefault(p => p.Name == propertyName); | |
return property; | |
} | |
public override string GetPropertyTypeName( | |
PSAdaptedProperty adaptedProperty) | |
{ | |
if (adaptedProperty is CustomProperty cp) | |
{ | |
return cp.TypeNameOfValue; | |
} | |
throw new NotSupportedException(); | |
} | |
public override bool IsGettable( | |
PSAdaptedProperty adaptedProperty) | |
{ | |
if (adaptedProperty is CustomProperty cp) | |
{ | |
return cp.IsGettable; | |
} | |
throw new NotSupportedException(); | |
} | |
public override bool IsSettable( | |
PSAdaptedProperty adaptedProperty) | |
{ | |
if (adaptedProperty is CustomProperty cp) | |
{ | |
return cp.IsSettable; | |
} | |
throw new NotSupportedException(); | |
} | |
public override object GetPropertyValue( | |
PSAdaptedProperty adaptedProperty) | |
{ | |
if (adaptedProperty is CustomProperty cp) | |
{ | |
return cp.Value; | |
} | |
throw new NotSupportedException(); | |
} | |
public override void SetPropertyValue( | |
PSAdaptedProperty adaptedProperty, | |
object value) | |
{ | |
if (adaptedProperty is CustomProperty cp) | |
{ | |
cp.Value = value; | |
} | |
throw new NotSupportedException(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment