ln.application/ln.application/PropertyArgument.cs

50 lines
1.8 KiB
C#

using System.Reflection;
using ln.type;
namespace ln.application
{
public class PropertyArgument : IArgument
{
public PropertyInfo PropertyInfo { get; }
public object Instance { get;}
public PropertyArgument(PropertyInfo propertyInfo)
:this(null, propertyInfo, propertyInfo.GetCustomAttribute<StaticArgumentAttribute>())
{}
public PropertyArgument(object instance, PropertyInfo propertyInfo)
:this(instance, propertyInfo, propertyInfo.GetCustomAttribute<StaticArgumentAttribute>())
{}
public PropertyArgument(PropertyInfo propertyInfo, StaticArgumentAttribute staticArgumentAttribute)
:this(null, propertyInfo, staticArgumentAttribute){}
public PropertyArgument(object instance,PropertyInfo propertyInfo, StaticArgumentAttribute staticArgumentAttribute)
{
PropertyInfo = propertyInfo;
OptionName = staticArgumentAttribute?.Option ?? (char)0;
LongOptionName = staticArgumentAttribute?.LongOption ?? propertyInfo.Name;
HelpString = staticArgumentAttribute?.HelpString ?? "";
HasArgument = propertyInfo.PropertyType != typeof(bool);
Instance = instance;
}
public char OptionName { get; }
public string LongOptionName { get; }
public bool HasArgument { get; }
public string HelpString { get; }
public string Value
{
get => PropertyInfo.GetValue(Instance)?.ToString();
set
{
if (HasArgument)
{
PropertyInfo.SetValue(Instance, Cast.To(value, PropertyInfo.PropertyType));
}
else
{
PropertyInfo.SetValue(Instance, true);
}
}
}
}
}