Files
CliFx/CliFx/Schema/BindablePropertyDescriptor.cs
2021-04-22 15:28:33 +03:00

35 lines
1.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Reflection;
using CliFx.Utils.Extensions;
namespace CliFx.Schema
{
internal class BindablePropertyDescriptor : IPropertyDescriptor
{
private readonly PropertyInfo _property;
public Type Type => _property.PropertyType;
public BindablePropertyDescriptor(PropertyInfo property) => _property = property;
public object? GetValue(ICommand commandInstance) =>
_property.GetValue(commandInstance);
public void SetValue(ICommand commandInstance, object? value) =>
_property.SetValue(commandInstance, value);
public IReadOnlyList<object?> GetValidValues()
{
var underlyingType = Type.TryGetEnumerableUnderlyingType() ?? Type;
underlyingType = underlyingType.TryGetNullableUnderlyingType() ?? underlyingType;
// We can only get valid values for enums
if (underlyingType.IsEnum)
return Enum.GetNames(underlyingType);
return Array.Empty<object?>();
}
}
}