Instantly share code, notes, and snippets.

Embed
What would you like to do?
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; }
}
}
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